summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/MochiKit/Base.js
Side-by-side diff
Diffstat (limited to 'frontend/gamma/js/MochiKit/Base.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/MochiKit/Base.js156
1 files changed, 102 insertions, 54 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
@@ -5,54 +5,65 @@ 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 = {};
}
@@ -237,12 +248,13 @@ MochiKit.Base.update(MochiKit.Base, {
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: {
@@ -348,13 +360,13 @@ MochiKit.Base.update(MochiKit.Base, {
} 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 */
@@ -672,12 +684,15 @@ MochiKit.Base.update(MochiKit.Base, {
}
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;
@@ -785,17 +800,20 @@ MochiKit.Base.update(MochiKit.Base, {
return o.__repr__();
} else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
return o.repr();
}
return MochiKit.Base.reprRegistry.match(o);
} catch (e) {
- if (typeof(o.NAME) == 'string' && (
- o.toString == Function.prototype.toString ||
- o.toString == Object.prototype.toString
- )) {
- return o.NAME;
+ 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) + "]";
@@ -837,22 +855,19 @@ MochiKit.Base.update(MochiKit.Base, {
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") {
@@ -891,12 +906,18 @@ MochiKit.Base.update(MochiKit.Base, {
}
// 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);
}
}
@@ -1097,13 +1118,13 @@ MochiKit.Base.update(MochiKit.Base, {
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];
}
@@ -1287,25 +1308,51 @@ MochiKit.Base.AdapterRegistry.prototype = {
}
}
return false;
}
};
-MochiKit.Base._exportSymbols = function (globals, module) {
- if (MochiKit.__export__ === false || module.__export__ === false) {
- return;
- }
- 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];
+/**
+ * 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 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
@@ -1318,13 +1365,13 @@ MochiKit.Base._exportSymbols = function (globals, module) {
* @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);
}
@@ -1346,48 +1393,47 @@ MochiKit.Base._deprecated = function (module, name, target, version, exportable)
}
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 + "()";
@@ -1406,12 +1452,14 @@ MochiKit.Base.__new__ = function () {
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);