Browse files

Working API explorer

  • Loading branch information...
1 parent 0fe202d commit 877d00e057fdf8e4dde5ebf73c06c393cc6b1c2d @autarch autarch committed Sep 2, 2012
View
6 share/api-explorer/css/explorer.css
@@ -17,3 +17,9 @@ ul.entry-points li {
list-style: none;
margin-bottom: 1em;
}
+
+pre.responseJSON {
+ background-color: #f9f9f9;
+ color: black;
+ font-size: 110%;
+}
View
BIN share/api-explorer/img/spinner.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
50 share/api-explorer/index.html
@@ -6,6 +6,8 @@
<link href="css/explorer.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery-1.8.1.min.js"></script>
+ <script src="js/jquery.mustache.js"></script>
+ <script src="js/underscore-min.js"></script>
<script src="js/Explorer.js"></script>
</head>
<body>
@@ -35,7 +37,28 @@
</div>
<div class="row-fluid">
- <div class="span3 offset2">
+ <div class="span5 offset2">
+ <h2>Request</h2>
+
+ <form action="#" id="request-form">
+ <label for="uri">URI</label>
+ <div class="input-prepend">
+ <span class="add-on base-uri">http://...</span><br>
+ <input type="text" class="input-xxlarge" name="uri" id="uri" value="/">
+ </div>
+
+ <label for="accept">Accept</label>
+ <input type="text" name="accept" id="accept" value="application/json">
+
+ <div class="controls">
+ <button type="submit" class="btn btn-primary">Submit</button>
+ </div>
+ </form>
+
+ <div id="request-display"></div>
+ </div>
+
+ <div class="span3">
<h2>Entry Points</h2>
<ul class="entry-points">
@@ -50,7 +73,7 @@
Ave S, Minneapolis, MN.
<br>
<a href="/search/by-lat-long/44.9617005%2C-93.2766566"
- class="entry-point-uri">/search/by-lat-long/4.9617005,-93.2766566</a>
+ class="entry-point-uri">/search/by-lat-long/44.9617005,-93.2766566</a>
</li>
<li>
A <a href="/api-docs#Geographical-Searches">geographical search by
@@ -62,28 +85,13 @@
</li>
</ul>
</div>
- <div class="span5">
- <h2>Request</h2>
-
- <form action="#">
- <label for="uri">URI</label>
- <div class="input-prepend">
- <span class="add-on base-uri">http://...</span><br>
- <input type="text" class="input-xxlarge" name="uri" id="uri" value="/">
- </div>
-
- <label for="accept">Accept</label>
- <input type="text" name="accept" id="accept" value="application/json">
-
- <div class="controls">
- <button type="submit" class="btn btn-primary">Submit</button>
- </div>
- </form>
+</div>
- <h2>Response</h2>
+<div class="row-fluid">
+ <div class="span8 offset2">
+ <div id="response-display"></div>
</div>
</div>
-
</body>
</html>
View
193 share/api-explorer/js/Explorer.js
@@ -1,27 +1,200 @@
(function () {
+ var Response = (
+ function () {
+ var R = function (explorer) {
+ this._explorer = explorer;
+ };
+
+ /* This just exists to preload the image */
+ {
+ var spinner = new Image ();
+ spinner.src = "img/spinner.gif";
+ }
+
+ var _title = '<h2>Response</h2>';
+
+ var _loadingHTML =
+ _title +
+ '<p>Waiting for response ... <img src="img/spinner.gif"></p>';
+
+ R.prototype.showLoading = function () {
+ $("#response-display").html(_loadingHTML);
+ };
+
+ var _responseTemplate =
+ _title +
+ '<dl>' +
+ '<dt>Content-Type:</dt>' +
+ '<dd>{{headers.content_type}}</dd>' +
+ '</dl>' +
+ '<pre class="responseJSON">{{&response}}</code></pre>';
+
+ R.prototype.displayResponse = function (response, xhr) {
+ var self = this;
+
+ var json =
+ JSON.stringify( response, null, 2 )
+ .replace( /&/g, '&amp;' )
+ .replace( />/g, '&gt;' )
+ .replace( /</g, '&lt;' )
+ .replace( /"/g, '&quot;' )
+ .replace(
+ /((&quot;|_)uri&quot;\s*:\s*&quot;)(.+)(&quot;,?)/g,
+ function (match, p1, p2, uri, p4) {
+ return p1 + self._makeAnchor(uri) + p4;
+ }
+ );
+
+ var view = {
+ response: json,
+ headers: { content_type: xhr.getResponseHeader('Content-Type') }
+ };
+ $("#response-display").html( $.mustache( _responseTemplate, view ) );
+
+ $("#response-display a").each(
+ function () {
+ self._explorer.instrumentAnchor( $(this) );
+ }
+ );
+ };
+
+ R.prototype._makeAnchor = function (uri) {
+ /* We need to create a container div because there is no way
+ * I can see to get the _outer_ HTML for an element with
+ * jQuery. The div is a container we can call .html() on to
+ * get the <a> tag. */
+ var div = $("<div/>");
+ var a = $("<a/>");
+ a.attr(
+ {
+ href: uri,
+ title: "Explore this URI (" + uri + ")"
+ }
+ );
+ a.append(uri);
+ div.append(a);
+
+ return div.html();
+ };
+
+ return R;
+ }
+ )();
+
+ var Request = (
+ function () {
+ var R = function (baseURI, uri, accept, explorer) {
+ uri = uri.replace( new RegExp ( "^" + baseURI ), "" );
+
+ this._displayURI = baseURI + uri;
+
+ this._requestURI = baseURI +
+ _.map(
+ uri.split("/"),
+ function (piece) {
+ if ( ! piece.length ) {
+ return "";
+ }
+ else {
+ return encodeURI(piece);
+ }
+ }
+ ).join("/");
+
+ this._accept = accept;
+ this._explorer = explorer;
+ };
+
+ var _displayTemplate =
+ '<dl>' +
+ '<dt>URI:</dt>' +
+ '<dd>{{_displayURI}}</dd>' +
+ '<dt>Accept:</dt>' +
+ '<dd>{{_accept}}</dd>' +
+ '</dl>';
+
+ R.prototype.display = function () {
+ $("#request-display").html( $.mustache( _displayTemplate, this ) );
+ };
+
+ R.prototype.submit = function () {
+ this._response = new Response ( this._explorer );
+ this._response.showLoading();
+
+ var self = this;
+ $.ajax(
+ {
+ url: this._requestURI,
+ accepts: this._accept,
+ dataType: "json"
+ }
+ ).done(
+ function (response, status, xhr) {
+ console.log(response);
+ self._response.displayResponse( response, xhr );
+ }
+ );
+ };
+
+ return R;
+ }
+ )();
+
var Explorer = (
function () {
var E = function () {
this._baseURI = window.location.protocol + '//' + window.location.host;
};
+ E.prototype.instrumentAnchor = function (a) {
+ var origText = a.text();
+ var re = new RegExp ( "^" + this._baseURI );
+
+ if ( ! re.test(origText) ) {
+ a.text( this._baseURI + origText );
+ }
+
+ a.click(
+ function () {
+ $("#uri").attr( "value", origText.replace( re, "" ) );
+ $("#request-form").submit();
+ return false;
+ }
+ );
+ };
+
+ E.prototype._instrumentRequestForm = function () {
+ var self = this;
+
+ $("#request-form").submit(
+ function () {
+ var request = new Request (
+ self._baseURI,
+ $("#uri").attr("value"),
+ $("#accept").attr("value"),
+ self
+ );
+
+ request.display();
+ request.submit();
+
+ return false;
+ }
+ );
+ };
+
E.prototype.initPage = function () {
- var baseURI = this._baseURI;
+ var self = this;
$("ul.entry-points a.entry-point-uri").each(
function () {
- var origText = $(this).text()
- $(this).text( baseURI + origText );
- $(this).click(
- function () {
- $("#uri").attr( "value", origText );
- return false;
- }
- );
+ self.instrumentAnchor( $(this) );
}
);
- $("span.base-uri").text(baseURI);
+ self._instrumentRequestForm();
+
+ $("span.base-uri").text( this._baseURI );
};
return E;
View
632 share/api-explorer/js/jquery.mustache.js
@@ -0,0 +1,632 @@
+/*
+Shameless port of a shameless port
+@defunkt => @janl => @aq
+
+See http://github.com/defunkt/mustache for more info.
+*/
+
+;(function($) {
+
+/*!
+ * mustache.js - Logic-less {{mustache}} templates with JavaScript
+ * http://github.com/janl/mustache.js
+ */
+
+/*global define: false*/
+
+var Mustache;
+
+(function (exports) {
+ if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
+ module.exports = exports; // CommonJS
+ } else if (typeof define === "function") {
+ define(exports); // AMD
+ } else {
+ Mustache = exports; // <script>
+ }
+}((function () {
+ var exports = {};
+
+ exports.name = "mustache.js";
+ exports.version = "0.6.0";
+ exports.tags = ["{{", "}}"];
+
+ exports.parse = parse;
+ exports.clearCache = clearCache;
+ exports.compile = compile;
+ exports.compilePartial = compilePartial;
+ exports.render = render;
+
+ exports.Scanner = Scanner;
+ exports.Context = Context;
+ exports.Renderer = Renderer;
+
+ // This is here for backwards compatibility with 0.4.x.
+ exports.to_html = function (template, view, partials, send) {
+ var result = render(template, view, partials);
+
+ if (typeof send === "function") {
+ send(result);
+ } else {
+ return result;
+ }
+ };
+
+ var whiteRe = /\s*/;
+ var spaceRe = /\s+/;
+ var nonSpaceRe = /\S/;
+ var eqRe = /\s*=/;
+ var curlyRe = /\s*\}/;
+ var tagRe = /#|\^|\/|>|\{|&|=|!/;
+
+ // Workaround for https://issues.apache.org/jira/browse/COUCHDB-577
+ // See https://github.com/janl/mustache.js/issues/189
+ function testRe(re, string) {
+ return RegExp.prototype.test.call(re, string);
+ }
+
+ function isWhitespace(string) {
+ return !testRe(nonSpaceRe, string);
+ }
+
+ var isArray = Array.isArray || function (obj) {
+ return Object.prototype.toString.call(obj) === "[object Array]";
+ };
+
+ // OSWASP Guidelines: Escape all non alphanumeric characters in ASCII space.
+ var jsCharsRe = /[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\xFF\u2028\u2029]/gm;
+
+ function quote(text) {
+ var escaped = text.replace(jsCharsRe, function (c) {
+ return "\\u" + ('0000' + c.charCodeAt(0).toString(16)).slice(-4);
+ });
+
+ return '"' + escaped + '"';
+ }
+
+ function escapeRe(string) {
+ return string.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
+ }
+
+ var entityMap = {
+ "&": "&amp;",
+ "<": "&lt;",
+ ">": "&gt;",
+ '"': '&quot;',
+ "'": '&#39;',
+ "/": '&#x2F;'
+ };
+
+ function escapeHtml(string) {
+ return String(string).replace(/[&<>"'\/]/g, function (s) {
+ return entityMap[s];
+ });
+ }
+
+ // Export the escaping function so that the user may override it.
+ // See https://github.com/janl/mustache.js/issues/244
+ exports.escape = escapeHtml;
+
+ function Scanner(string) {
+ this.string = string;
+ this.tail = string;
+ this.pos = 0;
+ }
+
+ /**
+ * Returns `true` if the tail is empty (end of string).
+ */
+ Scanner.prototype.eos = function () {
+ return this.tail === "";
+ };
+
+ /**
+ * Tries to match the given regular expression at the current position.
+ * Returns the matched text if it can match, the empty string otherwise.
+ */
+ Scanner.prototype.scan = function (re) {
+ var match = this.tail.match(re);
+
+ if (match && match.index === 0) {
+ this.tail = this.tail.substring(match[0].length);
+ this.pos += match[0].length;
+ return match[0];
+ }
+
+ return "";
+ };
+
+ /**
+ * Skips all text until the given regular expression can be matched. Returns
+ * the skipped string, which is the entire tail if no match can be made.
+ */
+ Scanner.prototype.scanUntil = function (re) {
+ var match, pos = this.tail.search(re);
+
+ switch (pos) {
+ case -1:
+ match = this.tail;
+ this.pos += this.tail.length;
+ this.tail = "";
+ break;
+ case 0:
+ match = "";
+ break;
+ default:
+ match = this.tail.substring(0, pos);
+ this.tail = this.tail.substring(pos);
+ this.pos += pos;
+ }
+
+ return match;
+ };
+
+ function Context(view, parent) {
+ this.view = view;
+ this.parent = parent;
+ this.clearCache();
+ }
+
+ Context.make = function (view) {
+ return (view instanceof Context) ? view : new Context(view);
+ };
+
+ Context.prototype.clearCache = function () {
+ this._cache = {};
+ };
+
+ Context.prototype.push = function (view) {
+ return new Context(view, this);
+ };
+
+ Context.prototype.lookup = function (name) {
+ var value = this._cache[name];
+
+ if (!value) {
+ if (name === ".") {
+ value = this.view;
+ } else {
+ var context = this;
+
+ while (context) {
+ if (name.indexOf(".") > 0) {
+ var names = name.split("."), i = 0;
+
+ value = context.view;
+
+ while (value && i < names.length) {
+ value = value[names[i++]];
+ }
+ } else {
+ value = context.view[name];
+ }
+
+ if (value != null) {
+ break;
+ }
+
+ context = context.parent;
+ }
+ }
+
+ this._cache[name] = value;
+ }
+
+ if (typeof value === "function") {
+ value = value.call(this.view);
+ }
+
+ return value;
+ };
+
+ function Renderer() {
+ this.clearCache();
+ }
+
+ Renderer.prototype.clearCache = function () {
+ this._cache = {};
+ this._partialCache = {};
+ };
+
+ Renderer.prototype.compile = function (tokens, tags) {
+ if (typeof tokens === "string") {
+ tokens = parse(tokens, tags);
+ }
+
+ var fn = compileTokens(tokens),
+ self = this;
+
+ return function (view) {
+ return fn(Context.make(view), self);
+ };
+ };
+
+ Renderer.prototype.compilePartial = function (name, tokens, tags) {
+ this._partialCache[name] = this.compile(tokens, tags);
+ return this._partialCache[name];
+ };
+
+ Renderer.prototype.render = function (template, view) {
+ var fn = this._cache[template];
+
+ if (!fn) {
+ fn = this.compile(template);
+ this._cache[template] = fn;
+ }
+
+ return fn(view);
+ };
+
+ Renderer.prototype._section = function (name, context, callback) {
+ var value = context.lookup(name);
+
+ switch (typeof value) {
+ case "object":
+ if (isArray(value)) {
+ var buffer = "";
+
+ for (var i = 0, len = value.length; i < len; ++i) {
+ buffer += callback(context.push(value[i]), this);
+ }
+
+ return buffer;
+ }
+
+ return value ? callback(context.push(value), this) : "";
+ case "function":
+ // TODO: The text should be passed to the callback plain, not rendered.
+ var sectionText = callback(context, this),
+ self = this;
+
+ var scopedRender = function (template) {
+ return self.render(template, context);
+ };
+
+ return value.call(context.view, sectionText, scopedRender) || "";
+ default:
+ if (value) {
+ return callback(context, this);
+ }
+ }
+
+ return "";
+ };
+
+ Renderer.prototype._inverted = function (name, context, callback) {
+ var value = context.lookup(name);
+
+ // Use JavaScript's definition of falsy. Include empty arrays.
+ // See https://github.com/janl/mustache.js/issues/186
+ if (!value || (isArray(value) && value.length === 0)) {
+ return callback(context, this);
+ }
+
+ return "";
+ };
+
+ Renderer.prototype._partial = function (name, context) {
+ var fn = this._partialCache[name];
+
+ if (fn) {
+ return fn(context);
+ }
+
+ return "";
+ };
+
+ Renderer.prototype._name = function (name, context, escape) {
+ var value = context.lookup(name);
+
+ if (typeof value === "function") {
+ value = value.call(context.view);
+ }
+
+ var string = (value == null) ? "" : String(value);
+
+ if (escape) {
+ return exports.escape(string);
+ }
+
+ return string;
+ };
+
+ /**
+ * Low-level function that compiles the given `tokens` into a
+ * function that accepts two arguments: a Context and a
+ * Renderer. Returns the body of the function as a string if
+ * `returnBody` is true.
+ */
+ function compileTokens(tokens, returnBody) {
+ var body = ['""'];
+ var token, method, escape;
+
+ for (var i = 0, len = tokens.length; i < len; ++i) {
+ token = tokens[i];
+
+ switch (token.type) {
+ case "#":
+ case "^":
+ method = (token.type === "#") ? "_section" : "_inverted";
+ body.push("r." + method + "(" + quote(token.value) + ", c, function (c, r) {\n" +
+ " " + compileTokens(token.tokens, true) + "\n" +
+ "})");
+ break;
+ case "{":
+ case "&":
+ case "name":
+ escape = token.type === "name" ? "true" : "false";
+ body.push("r._name(" + quote(token.value) + ", c, " + escape + ")");
+ break;
+ case ">":
+ body.push("r._partial(" + quote(token.value) + ", c)");
+ break;
+ case "text":
+ body.push(quote(token.value));
+ break;
+ }
+ }
+
+ // Convert to a string body.
+ body = "return " + body.join(" + ") + ";";
+
+ // Good for debugging.
+ // console.log(body);
+
+ if (returnBody) {
+ return body;
+ }
+
+ // For great evil!
+ return new Function("c, r", body);
+ }
+
+ function escapeTags(tags) {
+ if (tags.length !== 2) {
+ throw new Error("Invalid tags: " + tags.join(" "));
+ }
+
+ return [
+ new RegExp(escapeRe(tags[0]) + "\\s*"),
+ new RegExp("\\s*" + escapeRe(tags[1]))
+ ];
+ }
+
+ /**
+ * Forms the given linear array of `tokens` into a nested tree structure
+ * where tokens that represent a section have a "tokens" array property
+ * that contains all tokens that are in that section.
+ */
+ function nestTokens(tokens) {
+ var tree = [];
+ var collector = tree;
+ var sections = [];
+ var token, section;
+
+ for (var i = 0; i < tokens.length; ++i) {
+ token = tokens[i];
+
+ switch (token.type) {
+ case "#":
+ case "^":
+ token.tokens = [];
+ sections.push(token);
+ collector.push(token);
+ collector = token.tokens;
+ break;
+ case "/":
+ if (sections.length === 0) {
+ throw new Error("Unopened section: " + token.value);
+ }
+
+ section = sections.pop();
+
+ if (section.value !== token.value) {
+ throw new Error("Unclosed section: " + section.value);
+ }
+
+ if (sections.length > 0) {
+ collector = sections[sections.length - 1].tokens;
+ } else {
+ collector = tree;
+ }
+ break;
+ default:
+ collector.push(token);
+ }
+ }
+
+ // Make sure there were no open sections when we're done.
+ section = sections.pop();
+
+ if (section) {
+ throw new Error("Unclosed section: " + section.value);
+ }
+
+ return tree;
+ }
+
+ /**
+ * Combines the values of consecutive text tokens in the given `tokens` array
+ * to a single token.
+ */
+ function squashTokens(tokens) {
+ var lastToken;
+
+ for (var i = 0; i < tokens.length; ++i) {
+ var token = tokens[i];
+
+ if (lastToken && lastToken.type === "text" && token.type === "text") {
+ lastToken.value += token.value;
+ tokens.splice(i--, 1); // Remove this token from the array.
+ } else {
+ lastToken = token;
+ }
+ }
+ }
+
+ /**
+ * Breaks up the given `template` string into a tree of token objects. If
+ * `tags` is given here it must be an array with two string values: the
+ * opening and closing tags used in the template (e.g. ["<%", "%>"]). Of
+ * course, the default is to use mustaches (i.e. Mustache.tags).
+ */
+ function parse(template, tags) {
+ tags = tags || exports.tags;
+
+ var tagRes = escapeTags(tags);
+ var scanner = new Scanner(template);
+
+ var tokens = [], // Buffer to hold the tokens
+ spaces = [], // Indices of whitespace tokens on the current line
+ hasTag = false, // Is there a {{tag}} on the current line?
+ nonSpace = false; // Is there a non-space char on the current line?
+
+ // Strips all whitespace tokens array for the current line
+ // if there was a {{#tag}} on it and otherwise only space.
+ var stripSpace = function () {
+ if (hasTag && !nonSpace) {
+ while (spaces.length) {
+ tokens.splice(spaces.pop(), 1);
+ }
+ } else {
+ spaces = [];
+ }
+
+ hasTag = false;
+ nonSpace = false;
+ };
+
+ var type, value, chr;
+
+ while (!scanner.eos()) {
+ value = scanner.scanUntil(tagRes[0]);
+
+ if (value) {
+ for (var i = 0, len = value.length; i < len; ++i) {
+ chr = value.charAt(i);
+
+ if (isWhitespace(chr)) {
+ spaces.push(tokens.length);
+ } else {
+ nonSpace = true;
+ }
+
+ tokens.push({type: "text", value: chr});
+
+ if (chr === "\n") {
+ stripSpace(); // Check for whitespace on the current line.
+ }
+ }
+ }
+
+ // Match the opening tag.
+ if (!scanner.scan(tagRes[0])) {
+ break;
+ }
+
+ hasTag = true;
+ type = scanner.scan(tagRe) || "name";
+
+ // Skip any whitespace between tag and value.
+ scanner.scan(whiteRe);
+
+ // Extract the tag value.
+ if (type === "=") {
+ value = scanner.scanUntil(eqRe);
+ scanner.scan(eqRe);
+ scanner.scanUntil(tagRes[1]);
+ } else if (type === "{") {
+ var closeRe = new RegExp("\\s*" + escapeRe("}" + tags[1]));
+ value = scanner.scanUntil(closeRe);
+ scanner.scan(curlyRe);
+ scanner.scanUntil(tagRes[1]);
+ } else {
+ value = scanner.scanUntil(tagRes[1]);
+ }
+
+ // Match the closing tag.
+ if (!scanner.scan(tagRes[1])) {
+ throw new Error("Unclosed tag at " + scanner.pos);
+ }
+
+ tokens.push({type: type, value: value});
+
+ if (type === "name" || type === "{" || type === "&") {
+ nonSpace = true;
+ }
+
+ // Set the tags for the next time around.
+ if (type === "=") {
+ tags = value.split(spaceRe);
+ tagRes = escapeTags(tags);
+ }
+ }
+
+ squashTokens(tokens);
+
+ return nestTokens(tokens);
+ }
+
+ // The high-level clearCache, compile, compilePartial, and render functions
+ // use this default renderer.
+ var _renderer = new Renderer();
+
+ /**
+ * Clears all cached templates and partials.
+ */
+ function clearCache() {
+ _renderer.clearCache();
+ }
+
+ /**
+ * High-level API for compiling the given `tokens` down to a reusable
+ * function. If `tokens` is a string it will be parsed using the given `tags`
+ * before it is compiled.
+ */
+ function compile(tokens, tags) {
+ return _renderer.compile(tokens, tags);
+ }
+
+ /**
+ * High-level API for compiling the `tokens` for the partial with the given
+ * `name` down to a reusable function. If `tokens` is a string it will be
+ * parsed using the given `tags` before it is compiled.
+ */
+ function compilePartial(name, tokens, tags) {
+ return _renderer.compilePartial(name, tokens, tags);
+ }
+
+ /**
+ * High-level API for rendering the `template` using the given `view`. The
+ * optional `partials` object may be given here for convenience, but note that
+ * it will cause all partials to be re-compiled, thus hurting performance. Of
+ * course, this only matters if you're going to render the same template more
+ * than once. If so, it is best to call `compilePartial` before calling this
+ * function and to leave the `partials` argument blank.
+ */
+ function render(template, view, partials) {
+ if (partials) {
+ for (var name in partials) {
+ compilePartial(name, partials[name]);
+ }
+ }
+
+ return _renderer.render(template, view);
+ }
+
+ return exports;
+}())));
+
+ $.mustache = function (template, view, partials) {
+ return Mustache.render(template, view, partials);
+ };
+
+ $.fn.mustache = function (view, partials) {
+ return $(this).map(function (i, elm) {
+ var template = $(elm).html().trim();
+ var output = $.mustache(template, view, partials);
+ return $(output).get();
+ });
+ };
+
+})(jQuery);
View
32 share/api-explorer/js/underscore-min.js
@@ -0,0 +1,32 @@
+// Underscore.js 1.3.3
+// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
+// Underscore is freely distributable under the MIT license.
+// Portions of Underscore are inspired or borrowed from Prototype,
+// Oliver Steele's Functional, and John Resig's Micro-Templating.
+// For all details and documentation:
+// http://documentcloud.github.com/underscore
+(function(){function r(a,c,d){if(a===c)return 0!==a||1/a==1/c;if(null==a||null==c)return a===c;a._chain&&(a=a._wrapped);c._chain&&(c=c._wrapped);if(a.isEqual&&b.isFunction(a.isEqual))return a.isEqual(c);if(c.isEqual&&b.isFunction(c.isEqual))return c.isEqual(a);var e=l.call(a);if(e!=l.call(c))return!1;switch(e){case "[object String]":return a==""+c;case "[object Number]":return a!=+a?c!=+c:0==a?1/a==1/c:a==+c;case "[object Date]":case "[object Boolean]":return+a==+c;case "[object RegExp]":return a.source==
+c.source&&a.global==c.global&&a.multiline==c.multiline&&a.ignoreCase==c.ignoreCase}if("object"!=typeof a||"object"!=typeof c)return!1;for(var f=d.length;f--;)if(d[f]==a)return!0;d.push(a);var f=0,g=!0;if("[object Array]"==e){if(f=a.length,g=f==c.length)for(;f--&&(g=f in a==f in c&&r(a[f],c[f],d)););}else{if("constructor"in a!="constructor"in c||a.constructor!=c.constructor)return!1;for(var h in a)if(b.has(a,h)&&(f++,!(g=b.has(c,h)&&r(a[h],c[h],d))))break;if(g){for(h in c)if(b.has(c,h)&&!f--)break;
+g=!f}}d.pop();return g}var s=this,I=s._,o={},k=Array.prototype,p=Object.prototype,i=k.slice,J=k.unshift,l=p.toString,K=p.hasOwnProperty,y=k.forEach,z=k.map,A=k.reduce,B=k.reduceRight,C=k.filter,D=k.every,E=k.some,q=k.indexOf,F=k.lastIndexOf,p=Array.isArray,L=Object.keys,t=Function.prototype.bind,b=function(a){return new m(a)};"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=b),exports._=b):s._=b;b.VERSION="1.3.3";var j=b.each=b.forEach=function(a,
+c,d){if(a!=null)if(y&&a.forEach===y)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,f=a.length;e<f;e++){if(e in a&&c.call(d,a[e],e,a)===o)break}else for(e in a)if(b.has(a,e)&&c.call(d,a[e],e,a)===o)break};b.map=b.collect=function(a,c,b){var e=[];if(a==null)return e;if(z&&a.map===z)return a.map(c,b);j(a,function(a,g,h){e[e.length]=c.call(b,a,g,h)});if(a.length===+a.length)e.length=a.length;return e};b.reduce=b.foldl=b.inject=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(A&&
+a.reduce===A){e&&(c=b.bind(c,e));return f?a.reduce(c,d):a.reduce(c)}j(a,function(a,b,i){if(f)d=c.call(e,d,a,b,i);else{d=a;f=true}});if(!f)throw new TypeError("Reduce of empty array with no initial value");return d};b.reduceRight=b.foldr=function(a,c,d,e){var f=arguments.length>2;a==null&&(a=[]);if(B&&a.reduceRight===B){e&&(c=b.bind(c,e));return f?a.reduceRight(c,d):a.reduceRight(c)}var g=b.toArray(a).reverse();e&&!f&&(c=b.bind(c,e));return f?b.reduce(g,c,d,e):b.reduce(g,c)};b.find=b.detect=function(a,
+c,b){var e;G(a,function(a,g,h){if(c.call(b,a,g,h)){e=a;return true}});return e};b.filter=b.select=function(a,c,b){var e=[];if(a==null)return e;if(C&&a.filter===C)return a.filter(c,b);j(a,function(a,g,h){c.call(b,a,g,h)&&(e[e.length]=a)});return e};b.reject=function(a,c,b){var e=[];if(a==null)return e;j(a,function(a,g,h){c.call(b,a,g,h)||(e[e.length]=a)});return e};b.every=b.all=function(a,c,b){var e=true;if(a==null)return e;if(D&&a.every===D)return a.every(c,b);j(a,function(a,g,h){if(!(e=e&&c.call(b,
+a,g,h)))return o});return!!e};var G=b.some=b.any=function(a,c,d){c||(c=b.identity);var e=false;if(a==null)return e;if(E&&a.some===E)return a.some(c,d);j(a,function(a,b,h){if(e||(e=c.call(d,a,b,h)))return o});return!!e};b.include=b.contains=function(a,c){var b=false;if(a==null)return b;if(q&&a.indexOf===q)return a.indexOf(c)!=-1;return b=G(a,function(a){return a===c})};b.invoke=function(a,c){var d=i.call(arguments,2);return b.map(a,function(a){return(b.isFunction(c)?c||a:a[c]).apply(a,d)})};b.pluck=
+function(a,c){return b.map(a,function(a){return a[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a)&&a[0]===+a[0])return Math.max.apply(Math,a);if(!c&&b.isEmpty(a))return-Infinity;var e={computed:-Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b>=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a)&&a[0]===+a[0])return Math.min.apply(Math,a);if(!c&&b.isEmpty(a))return Infinity;var e={computed:Infinity};j(a,function(a,b,h){b=c?c.call(d,a,b,h):a;b<e.computed&&
+(e={value:a,computed:b})});return e.value};b.shuffle=function(a){var b=[],d;j(a,function(a,f){d=Math.floor(Math.random()*(f+1));b[f]=b[d];b[d]=a});return b};b.sortBy=function(a,c,d){var e=b.isFunction(c)?c:function(a){return a[c]};return b.pluck(b.map(a,function(a,b,c){return{value:a,criteria:e.call(d,a,b,c)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;return c===void 0?1:d===void 0?-1:c<d?-1:c>d?1:0}),"value")};b.groupBy=function(a,c){var d={},e=b.isFunction(c)?c:function(a){return a[c]};
+j(a,function(a,b){var c=e(a,b);(d[c]||(d[c]=[])).push(a)});return d};b.sortedIndex=function(a,c,d){d||(d=b.identity);for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?e=g+1:f=g}return e};b.toArray=function(a){return!a?[]:b.isArray(a)||b.isArguments(a)?i.call(a):a.toArray&&b.isFunction(a.toArray)?a.toArray():b.values(a)};b.size=function(a){return b.isArray(a)?a.length:b.keys(a).length};b.first=b.head=b.take=function(a,b,d){return b!=null&&!d?i.call(a,0,b):a[0]};b.initial=function(a,b,d){return i.call(a,
+0,a.length-(b==null||d?1:b))};b.last=function(a,b,d){return b!=null&&!d?i.call(a,Math.max(a.length-b,0)):a[a.length-1]};b.rest=b.tail=function(a,b,d){return i.call(a,b==null||d?1:b)};b.compact=function(a){return b.filter(a,function(a){return!!a})};b.flatten=function(a,c){return b.reduce(a,function(a,e){if(b.isArray(e))return a.concat(c?e:b.flatten(e));a[a.length]=e;return a},[])};b.without=function(a){return b.difference(a,i.call(arguments,1))};b.uniq=b.unique=function(a,c,d){var d=d?b.map(a,d):a,
+e=[];a.length<3&&(c=true);b.reduce(d,function(d,g,h){if(c?b.last(d)!==g||!d.length:!b.include(d,g)){d.push(g);e.push(a[h])}return d},[]);return e};b.union=function(){return b.uniq(b.flatten(arguments,true))};b.intersection=b.intersect=function(a){var c=i.call(arguments,1);return b.filter(b.uniq(a),function(a){return b.every(c,function(c){return b.indexOf(c,a)>=0})})};b.difference=function(a){var c=b.flatten(i.call(arguments,1),true);return b.filter(a,function(a){return!b.include(c,a)})};b.zip=function(){for(var a=
+i.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e<c;e++)d[e]=b.pluck(a,""+e);return d};b.indexOf=function(a,c,d){if(a==null)return-1;var e;if(d){d=b.sortedIndex(a,c);return a[d]===c?d:-1}if(q&&a.indexOf===q)return a.indexOf(c);d=0;for(e=a.length;d<e;d++)if(d in a&&a[d]===c)return d;return-1};b.lastIndexOf=function(a,b){if(a==null)return-1;if(F&&a.lastIndexOf===F)return a.lastIndexOf(b);for(var d=a.length;d--;)if(d in a&&a[d]===b)return d;return-1};b.range=function(a,b,d){if(arguments.length<=
+1){b=a||0;a=0}for(var d=arguments[2]||1,e=Math.max(Math.ceil((b-a)/d),0),f=0,g=Array(e);f<e;){g[f++]=a;a=a+d}return g};var H=function(){};b.bind=function(a,c){var d,e;if(a.bind===t&&t)return t.apply(a,i.call(arguments,1));if(!b.isFunction(a))throw new TypeError;e=i.call(arguments,2);return d=function(){if(!(this instanceof d))return a.apply(c,e.concat(i.call(arguments)));H.prototype=a.prototype;var b=new H,g=a.apply(b,e.concat(i.call(arguments)));return Object(g)===g?g:b}};b.bindAll=function(a){var c=
+i.call(arguments,1);c.length==0&&(c=b.functions(a));j(c,function(c){a[c]=b.bind(a[c],a)});return a};b.memoize=function(a,c){var d={};c||(c=b.identity);return function(){var e=c.apply(this,arguments);return b.has(d,e)?d[e]:d[e]=a.apply(this,arguments)}};b.delay=function(a,b){var d=i.call(arguments,2);return setTimeout(function(){return a.apply(null,d)},b)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(i.call(arguments,1)))};b.throttle=function(a,c){var d,e,f,g,h,i,j=b.debounce(function(){h=
+g=false},c);return function(){d=this;e=arguments;f||(f=setTimeout(function(){f=null;h&&a.apply(d,e);j()},c));g?h=true:i=a.apply(d,e);j();g=true;return i}};b.debounce=function(a,b,d){var e;return function(){var f=this,g=arguments;d&&!e&&a.apply(f,g);clearTimeout(e);e=setTimeout(function(){e=null;d||a.apply(f,g)},b)}};b.once=function(a){var b=false,d;return function(){if(b)return d;b=true;return d=a.apply(this,arguments)}};b.wrap=function(a,b){return function(){var d=[a].concat(i.call(arguments,0));
+return b.apply(this,d)}};b.compose=function(){var a=arguments;return function(){for(var b=arguments,d=a.length-1;d>=0;d--)b=[a[d].apply(this,b)];return b[0]}};b.after=function(a,b){return a<=0?b():function(){if(--a<1)return b.apply(this,arguments)}};b.keys=L||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var c=[],d;for(d in a)b.has(a,d)&&(c[c.length]=d);return c};b.values=function(a){return b.map(a,b.identity)};b.functions=b.methods=function(a){var c=[],d;for(d in a)b.isFunction(a[d])&&
+c.push(d);return c.sort()};b.extend=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};b.pick=function(a){var c={};j(b.flatten(i.call(arguments,1)),function(b){b in a&&(c[b]=a[b])});return c};b.defaults=function(a){j(i.call(arguments,1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a};b.clone=function(a){return!b.isObject(a)?a:b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};b.isEqual=function(a,b){return r(a,b,[])};b.isEmpty=
+function(a){if(a==null)return true;if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(b.has(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=p||function(a){return l.call(a)=="[object Array]"};b.isObject=function(a){return a===Object(a)};b.isArguments=function(a){return l.call(a)=="[object Arguments]"};b.isArguments(arguments)||(b.isArguments=function(a){return!(!a||!b.has(a,"callee"))});b.isFunction=function(a){return l.call(a)=="[object Function]"};
+b.isString=function(a){return l.call(a)=="[object String]"};b.isNumber=function(a){return l.call(a)=="[object Number]"};b.isFinite=function(a){return b.isNumber(a)&&isFinite(a)};b.isNaN=function(a){return a!==a};b.isBoolean=function(a){return a===true||a===false||l.call(a)=="[object Boolean]"};b.isDate=function(a){return l.call(a)=="[object Date]"};b.isRegExp=function(a){return l.call(a)=="[object RegExp]"};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.has=function(a,
+b){return K.call(a,b)};b.noConflict=function(){s._=I;return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e=0;e<a;e++)b.call(d,e)};b.escape=function(a){return(""+a).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")};b.result=function(a,c){if(a==null)return null;var d=a[c];return b.isFunction(d)?d.call(a):d};b.mixin=function(a){j(b.functions(a),function(c){M(c,b[c]=a[c])})};var N=0;b.uniqueId=
+function(a){var b=N++;return a?a+b:b};b.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var u=/.^/,n={"\\":"\\","'":"'",r:"\r",n:"\n",t:"\t",u2028:"\u2028",u2029:"\u2029"},v;for(v in n)n[n[v]]=v;var O=/\\|'|\r|\n|\t|\u2028|\u2029/g,P=/\\(\\|'|r|n|t|u2028|u2029)/g,w=function(a){return a.replace(P,function(a,b){return n[b]})};b.template=function(a,c,d){d=b.defaults(d||{},b.templateSettings);a="__p+='"+a.replace(O,function(a){return"\\"+n[a]}).replace(d.escape||
+u,function(a,b){return"'+\n_.escape("+w(b)+")+\n'"}).replace(d.interpolate||u,function(a,b){return"'+\n("+w(b)+")+\n'"}).replace(d.evaluate||u,function(a,b){return"';\n"+w(b)+"\n;__p+='"})+"';\n";d.variable||(a="with(obj||{}){\n"+a+"}\n");var a="var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n"+a+"return __p;\n",e=new Function(d.variable||"obj","_",a);if(c)return e(c,b);c=function(a){return e.call(this,a,b)};c.source="function("+(d.variable||"obj")+"){\n"+a+"}";return c};
+b.chain=function(a){return b(a).chain()};var m=function(a){this._wrapped=a};b.prototype=m.prototype;var x=function(a,c){return c?b(a).chain():a},M=function(a,c){m.prototype[a]=function(){var a=i.call(arguments);J.call(a,this._wrapped);return x(c.apply(b,a),this._chain)}};b.mixin(b);j("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=k[a];m.prototype[a]=function(){var d=this._wrapped;b.apply(d,arguments);var e=d.length;(a=="shift"||a=="splice")&&e===0&&delete d[0];return x(d,
+this._chain)}});j(["concat","join","slice"],function(a){var b=k[a];m.prototype[a]=function(){return x(b.apply(this._wrapped,arguments),this._chain)}});m.prototype.chain=function(){this._chain=true;return this};m.prototype.value=function(){return this._wrapped}}).call(this);

0 comments on commit 877d00e

Please sign in to comment.