| /** |
| * MathQuill v0.10.1 http://mathquill.com |
| * by Han, Jeanine, and Mary maintainers@mathquill.com |
| * |
| * This Source Code Form is subject to the terms of the |
| * Mozilla Public License, v. 2.0. If a copy of the MPL |
| * was not distributed with this file, You can obtain |
| * one at http://mozilla.org/MPL/2.0/. |
| */ |
| |
| (function() { |
| |
| var jQuery = window.jQuery, |
| undefined, |
| mqCmdId = 'mathquill-command-id', |
| mqBlockId = 'mathquill-block-id', |
| min = Math.min, |
| max = Math.max; |
| |
| if (!jQuery) throw 'MathQuill requires jQuery 1.4.3+ to be loaded first'; |
| |
| function noop() {} |
| |
| /** |
| * A utility higher-order function that makes defining variadic |
| * functions more convenient by letting you essentially define functions |
| * with the last argument as a splat, i.e. the last argument "gathers up" |
| * remaining arguments to the function: |
| * var doStuff = variadic(function(first, rest) { return rest; }); |
| * doStuff(1, 2, 3); // => [2, 3] |
| */ |
| var __slice = [].slice; |
| function variadic(fn) { |
| var numFixedArgs = fn.length - 1; |
| return function() { |
| var args = __slice.call(arguments, 0, numFixedArgs); |
| var varArg = __slice.call(arguments, numFixedArgs); |
| return fn.apply(this, args.concat([ varArg ])); |
| }; |
| } |
| |
| /** |
| * A utility higher-order function that makes combining object-oriented |
| * programming and functional programming techniques more convenient: |
| * given a method name and any number of arguments to be bound, returns |
| * a function that calls it's first argument's method of that name (if |
| * it exists) with the bound arguments and any additional arguments that |
| * are passed: |
| * var sendMethod = send('method', 1, 2); |
| * var obj = { method: function() { return Array.apply(this, arguments); } }; |
| * sendMethod(obj, 3, 4); // => [1, 2, 3, 4] |
| * // or more specifically, |
| * var obj2 = { method: function(one, two, three) { return one*two + three; } }; |
| * sendMethod(obj2, 3); // => 5 |
| * sendMethod(obj2, 4); // => 6 |
| */ |
| var send = variadic(function(method, args) { |
| return variadic(function(obj, moreArgs) { |
| if (method in obj) return obj[method].apply(obj, args.concat(moreArgs)); |
| }); |
| }); |
| |
| /** |
| * A utility higher-order function that creates "implicit iterators" |
| * from "generators": given a function that takes in a sole argument, |
| * a "yield_" function, that calls "yield_" repeatedly with an object as |
| * a sole argument (presumably objects being iterated over), returns |
| * a function that calls it's first argument on each of those objects |
| * (if the first argument is a function, it is called repeatedly with |
| * each object as the first argument, otherwise it is stringified and |
| * the method of that name is called on each object (if such a method |
| * exists)), passing along all additional arguments: |
| * var a = [ |
| * { method: function(list) { list.push(1); } }, |
| * { method: function(list) { list.push(2); } }, |
| * { method: function(list) { list.push(3); } } |
| * ]; |
| * a.each = iterator(function(yield_) { |
| * for (var i in this) yield_(this[i]); |
| * }); |
| * var list = []; |
| * a.each('method', list); |
| * list; // => [1, 2, 3] |
| * // Note that the for-in loop will yield 'each', but 'each' maps to |
| * // the function object created by iterator() which does not have a |
| * // .method() method, so that just fails silently. |
| */ |
| function iterator(generator) { |
| return variadic(function(fn, args) { |
| if (typeof fn !== 'function') fn = send(fn); |
| var yield_ = function(obj) { return fn.apply(obj, [ obj ].concat(args)); }; |
| return generator.call(this, yield_); |
| }); |
| } |
| |
| /** |
| * sugar to make defining lots of commands easier. |
| * TODO: rethink this. |
| */ |
| function bind(cons /*, args... */) { |
| var args = __slice.call(arguments, 1); |
| return function() { |
| return cons.apply(this, args); |
| }; |
| } |
| |
| /** |
| * a development-only debug method. This definition and all |
| * calls to `pray` will be stripped from the minified |
| * build of mathquill. |
| * |
| * This function must be called by name to be removed |
| * at compile time. Do not define another function |
| * with the same name, and only call this function by |
| * name. |
| */ |
| function pray(message, cond) { |
| if (!cond) throw new Error('prayer failed: '+message); |
| } |