Diffstat (limited to 'frontend/gamma/js/MochiKit/Base.js') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/Base.js | 140 |
1 files changed, 94 insertions, 46 deletions
diff --git a/frontend/gamma/js/MochiKit/Base.js b/frontend/gamma/js/MochiKit/Base.js index d33c269..ca1734c 100644 --- a/frontend/gamma/js/MochiKit/Base.js +++ b/frontend/gamma/js/MochiKit/Base.js @@ -3,58 +3,69 @@ MochiKit.Base 1.5 See <http://mochikit.com/> for documentation, downloads, license, etc. (c) 2005 Bob Ippolito. All rights Reserved. ***/ -if (typeof(MochiKit) == 'undefined') { - MochiKit = {}; -} + +// MochiKit module (namespace) +var MochiKit = MochiKit || {}; if (typeof(MochiKit.__export__) == "undefined") { MochiKit.__export__ = true; } -if (typeof(MochiKit.Base) == 'undefined') { - MochiKit.Base = {}; -} +MochiKit.NAME = "MochiKit"; +MochiKit.VERSION = "1.5"; +MochiKit.__repr__ = function () { + return "[" + this.NAME + " " + this.VERSION + "]"; +}; +MochiKit.toString = function () { + return this.__repr__(); +}; + + +// MochiKit.Base module +MochiKit.Base = MochiKit.Base || {}; /** - * Registers a new MochiKit module. This function will insert a new - * property into the "MochiKit" object, making sure that all - * dependency modules have already been inserted. It will also make - * sure that the appropriate properties and default module functions - * are defined. + * Creates a new module in a parent namespace. This function will + * create a new empty module object with "NAME", "VERSION", + * "toString" and "__repr__" properties. This object will be inserted into the parent object + * using the specified name (i.e. parent[name] = module). It will + * also verify that all the dependency modules are defined in the + * parent, or an error will be thrown. * + * @param {Object} parent the parent module (use "this" or "window" for + * a global module) * @param {String} name the module name, e.g. "Base" * @param {String} version the module version, e.g. "1.5" - * @param {Array} deps the array of module dependencies (as strings) + * @param {Array} [deps] the array of module dependencies (as strings) */ -MochiKit.Base._module = function (name, version, deps) { - if (!(name in MochiKit)) { - MochiKit[name] = {}; - } - var module = MochiKit[name]; - module.NAME = "MochiKit." + name; +MochiKit.Base.module = function (parent, name, version, deps) { + var module = parent[name] = parent[name] || {}; + var prefix = (parent.NAME ? parent.NAME + "." : ""); + module.NAME = prefix + name; module.VERSION = version; module.__repr__ = function () { return "[" + this.NAME + " " + this.VERSION + "]"; }; module.toString = function () { return this.__repr__(); }; - for (var i = 0; i < deps.length; i++) { - if (!(deps[i] in MochiKit)) { - throw 'MochiKit.' + name + ' depends on MochiKit.' + deps[i] + '!'; - } + for (var i = 0; deps != null && i < deps.length; i++) { + if (!(deps[i] in parent)) { + throw module.NAME + ' depends on ' + prefix + deps[i] + '!'; } } + return module; +}; -MochiKit.Base._module("Base", "1.5", []); +MochiKit.Base.module(MochiKit, "Base", "1.5", []); /** @id MochiKit.Base.update */ MochiKit.Base.update = function (self, obj/*, ... */) { if (self === null || self === undefined) { self = {}; } for (var i = 1; i < arguments.length; i++) { var o = arguments[i]; @@ -235,16 +246,17 @@ MochiKit.Base.update(MochiKit.Base, { rval.push([prop, v]); } return rval; }, _newNamedError: function (module, name, func) { func.prototype = new MochiKit.Base.NamedError(module.NAME + "." + name); + func.prototype.constructor = func; module[name] = func; }, /** @id MochiKit.Base.operator */ operator: { // unary logic operators /** @id MochiKit.Base.truth */ @@ -346,17 +358,17 @@ MochiKit.Base.update(MochiKit.Base, { if (typeof(value) === "boolean" || value instanceof Boolean) { return value.valueOf(); } else if (typeof(value) === "string" || value instanceof String) { return value.length > 0 && value != "false" && value != "null" && value != "undefined" && value != "0"; } else if (typeof(value) === "number" || value instanceof Number) { return !isNaN(value) && value != 0; } else if (value != null && typeof(value.length) === "number") { - return value.length !== 0 + return value.length !== 0; } else { return value != null; } }, /** @id MochiKit.Base.typeMatcher */ typeMatcher: function (/* typ */) { var types = {}; @@ -670,16 +682,19 @@ MochiKit.Base.update(MochiKit.Base, { if (!self) { self = this; } return me.im_func.apply(self, args); }; newfunc.im_self = im_self; newfunc.im_func = im_func; newfunc.im_preargs = im_preargs; + if (typeof(im_func.NAME) == 'string') { + newfunc.NAME = "bind(" + im_func.NAME + ",...)"; + } return newfunc; }, /** @id MochiKit.Base.bindLate */ bindLate: function (func, self/* args... */) { var m = MochiKit.Base; var args = arguments; if (typeof(func) === "string") { @@ -783,22 +798,25 @@ MochiKit.Base.update(MochiKit.Base, { try { if (typeof(o.__repr__) == 'function') { return o.__repr__(); } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) { return o.repr(); } return MochiKit.Base.reprRegistry.match(o); } catch (e) { + try { if (typeof(o.NAME) == 'string' && ( o.toString == Function.prototype.toString || o.toString == Object.prototype.toString )) { return o.NAME; } + } catch (ignore) { + } } try { var ostring = (o + ""); } catch (e) { return "[" + typeof(o) + "]"; } if (typeof(o) == "function") { ostring = ostring.replace(/^\s+/, "").replace(/\s+/g, " "); @@ -835,26 +853,23 @@ MochiKit.Base.update(MochiKit.Base, { /** @id MochiKit.Base.registerJSON */ registerJSON: function (name, check, wrap, /* optional */override) { MochiKit.Base.jsonRegistry.register(name, check, wrap, override); }, /** @id MochiKit.Base.evalJSON */ - evalJSON: function () { - return eval("(" + MochiKit.Base._filterJSON(arguments[0]) + ")"); + evalJSON: function (jsonText) { + return eval("(" + MochiKit.Base._filterJSON(jsonText) + ")"); }, _filterJSON: function (s) { var m = s.match(/^\s*\/\*(.*)\*\/\s*$/); - if (m) { - return m[1]; - } - return s; + return (m) ? m[1] : s; }, /** @id MochiKit.Base.serializeJSON */ serializeJSON: function (o) { var objtype = typeof(o); if (objtype == "number" || objtype == "boolean") { return o + ""; } else if (o === null) { @@ -889,16 +904,22 @@ MochiKit.Base.update(MochiKit.Base, { } return '"' + res + '"'; } // recurse var me = arguments.callee; // short-circuit for objects that support "json" serialization // if they return "self" then just pass-through... var newObj; + if (typeof(o.toJSON) == "function") { + newObj = o.toJSON(); + if (o !== newObj) { + return me(newObj); + } + } if (typeof(o.__json__) == "function") { newObj = o.__json__(); if (o !== newObj) { return me(newObj); } } if (typeof(o.json) == "function") { newObj = o.json(); @@ -1095,17 +1116,17 @@ MochiKit.Base.update(MochiKit.Base, { /** @id MochiKit.Base.median */ median: function(/* lst... */) { /* http://www.nist.gov/dads/HTML/median.html */ var data = MochiKit.Base.flattenArguments(arguments); if (data.length === 0) { throw new TypeError('median() requires at least one argument'); } - data.sort(compare); + data.sort(MochiKit.Base.compare); if (data.length % 2 == 0) { var upper = data.length / 2; return (data[upper] + data[upper - 1]) / 2; } else { return data[(data.length - 1) / 2]; } }, @@ -1285,29 +1306,55 @@ MochiKit.Base.AdapterRegistry.prototype = { this.pairs.splice(i, 1); return true; } } return false; } }; -MochiKit.Base._exportSymbols = function (globals, module) { - if (MochiKit.__export__ === false || module.__export__ === false) { - return; - } +/** + * Exports all symbols from one or more modules into the specified + * namespace (or scope). This is similar to MochiKit.Base.update(), + * except for special handling of the "__export__" flag, contained + * sub-modules (exported recursively), and names starting with "_". + * + * @param {Object} namespace the object or scope to modify + * @param {Object} module the module to export + */ +MochiKit.Base.moduleExport = function (namespace, module/*, ...*/) { + var SKIP = { toString: true, NAME: true, VERSION: true }; + var mods = MochiKit.Base.extend([], arguments, 1); + while ((module = mods.shift()) != null) { for (var k in module) { var v = module[k]; if (v != null) { - var okName = (k[0] !== "_" && k !== "toString"); - if (v.__export__ === true || (v.__export__ !== false && okName)) { - globals[k] = module[k]; + var flagSet = (typeof(v.__export__) == 'boolean'); + var nameValid = (k[0] !== "_" && !SKIP[k]); + if (flagSet ? v.__export__ : nameValid) { + if (typeof(v) == 'object' && v.NAME && v.VERSION) { + mods.push(v); + } else { + namespace[k] = module[k]; + } + } } } } + return namespace; +}; + +/** + * Identical to moduleExport, but also considers the global and + * module-specific "__export__" flag. + */ +MochiKit.Base._exportSymbols = function (namespace, module) { + if (MochiKit.__export__ !== false && module.__export__ !== false) { + MochiKit.Base.moduleExport(namespace, module); + } }; /** * Creates a deprecated function alias in the specified module. The * deprecated function will forward all calls and arguments to a * target function, while also logging a debug message on the first * call (if MochiKit.Logging is loaded). The destination function may * be located in another module, which must be loaded, or an @@ -1316,17 +1363,17 @@ MochiKit.Base._exportSymbols = function (globals, module) { * @param {Object/String} module the source module or module name * (e.g. 'DOM' or 'MochiKit.DOM') * @param {String} name the deprecated function name (e.g. 'getStyle') * @param {String} target the fully qualified name of the target * function (e.g. 'MochiKit.Style.getStyle') * @param {String} version the first version when the source function * was deprecated (e.g. '1.4') * @param {Boolean} [exportable] the exportable function flag, - * defaults to true + * defaults to false */ MochiKit.Base._deprecated = function (module, name, target, version, exportable) { if (typeof(module) === 'string') { if (module.indexOf('MochiKit.') === 0) { module = module.substring(9); } module = MochiKit[module]; } @@ -1344,52 +1391,51 @@ MochiKit.Base._deprecated = function (module, name, target, version, exportable) console.log(msg); } } if (!MochiKit[targetModule]) { throw new Error(msg); } return MochiKit[targetModule][targetName].apply(this, arguments); }; - if (exportable === false) { - func.__export__ = false; - } + func.__export__ = (exportable === true); module[name] = func; -} +}; MochiKit.Base.__new__ = function () { var m = this; /** @id MochiKit.Base.noop */ m.noop = m.operator.identity; // Backwards compat - m._deprecated(m, 'forward', 'MochiKit.Base.forwardCall', '1.3', false); - m._deprecated(m, 'find', 'MochiKit.Base.findValue', '1.3', false); + m._deprecated(m, 'forward', 'MochiKit.Base.forwardCall', '1.3'); + m._deprecated(m, 'find', 'MochiKit.Base.findValue', '1.3'); if (typeof(encodeURIComponent) != "undefined") { /** @id MochiKit.Base.urlEncode */ m.urlEncode = function (unencoded) { return encodeURIComponent(unencoded).replace(/\'/g, '%27'); }; } else { m.urlEncode = function (unencoded) { return escape(unencoded ).replace(/\+/g, '%2B' ).replace(/\"/g,'%22' - ).rval.replace(/\'/g, '%27'); + ).replace(/\'/g, '%27'); }; } /** @id MochiKit.Base.NamedError */ m.NamedError = function (name) { this.message = name; this.name = name; }; m.NamedError.prototype = new Error(); + m.NamedError.prototype.constructor = m.NamedError; m.update(m.NamedError.prototype, { repr: function () { if (this.message && this.message != this.name) { return this.name + "(" + m.repr(this.message) + ")"; } else { return this.name + "()"; } }, @@ -1404,16 +1450,18 @@ MochiKit.Base.__new__ = function () { m.listMax = m.partial(m.listMinMax, 1); /** @id MochiKit.Base.listMin */ m.listMin = m.partial(m.listMinMax, -1); /** @id MochiKit.Base.isCallable */ m.isCallable = m.typeMatcher('function'); /** @id MochiKit.Base.isUndefined */ m.isUndefined = m.typeMatcher('undefined'); + /** @id MochiKit.Base.isValue */ + m.isValue = m.typeMatcher('boolean', 'number', 'string'); /** @id MochiKit.Base.merge */ m.merge = m.partial(m.update, null); /** @id MochiKit.Base.zip */ m.zip = m.partial(m.map, null); /** @id MochiKit.Base.average */ m.average = m.mean; |