Updates modulejs to 0.2.

This commit is contained in:
Lars Jung 2012-07-11 01:00:39 +02:00
parent 84a2ed582f
commit d47b996c9f
7 changed files with 270 additions and 317 deletions

View file

@ -21,7 +21,7 @@
"H5AI_CONFIG",
"jQuery",
"Modernizr",
"module",
"modulejs",
"moment",
"_"
]

View file

@ -9,11 +9,12 @@ modulejs.define('h5ai-main', ['jQuery', 'core/event', 'core/settings'], function
event.pub('beforeExt');
// _.each(modulejs.getIds(/^ext\/.+/), function (id) {
_.each(modulejs.state(), function (state, id) {
// modulejs.require(id);
// });
modulejs.require(/^ext\/.+/);
if (/^ext\/.+/.test(id)) {
modulejs.require(id);
}
});
event.pub('ready');
});

View file

@ -1,168 +0,0 @@
/*! modulejs 0.1 - //larsjung.de/qrcode - MIT License */
(function (global, _, name) {
'use strict';
// throws error
var err = function (condition, code, message) {
if (condition) {
throw {
code: code,
msg: message,
toString: function () {
return name + ' error: ' + message;
}
};
}
};
// make sure underscore is loaded
err(!_, 1, name + ' requires underscore');
// ModuleJs
// ========
var ModuleJs = function () {
var self = this;
// module definitions
self.definitions = {};
// module instances
self.instances = {};
// define
// ------
// Defines a module.
self.define = function (id, deps, fn) {
// sort arguments
if (_.isFunction(deps)) {
fn = deps;
deps = [];
}
// check arguments
err(!_.isString(id), 11, 'id must be a string "' + id + '"');
err(self.definitions[id], 12, 'id already defined "' + id + '"');
err(!_.isFunction(fn), 13, 'constructor for "' + id + '" must be a function "' + fn + '"');
err(!_.isArray(deps), 14, 'dependencies for "' + id + '" must be an array "' + deps + '"');
// map definition
self.definitions[id] = {
id: id,
deps: deps,
fn: fn
};
};
// predefined
// ----------
// Registers a predefined object.
self.predefined = function (id, instance, check) {
if (_.isFunction(check)) {
check = !!check();
}
if (!_.isBoolean(check)) {
check = instance !== undefined;
}
err(!check, 21, 'check for predefined "' + id + '" failed');
self.define(id, [], function () {
return instance;
});
};
// Returns an instance for `id`, checked against require-`stack` for
// cyclic dependencies.
self._require = function (id, stack) {
err(!_.isString(id), 31, 'id must be a string "' + id + '"');
if (_.has(self.instances, id)) {
return self.instances[id];
}
var def = self.definitions[id];
err(!def, 32, 'id not defined "' + id + '"');
stack = (stack || []).slice(0);
stack.push(id);
var deps = _.map(def.deps, function (depId) {
err(_.indexOf(stack, depId) >= 0, 33, 'cyclic dependencies: ' + stack + ' & ' + depId);
return self._require(depId, stack);
});
var obj = def.fn.apply(global, deps);
self.instances[id] = obj;
return obj;
};
// require
// -------
// Returns an instance for `id`.
self.require = function (arg) {
if (_.isArray(arg)) {
return _.map(arg, function (id) {
return self._require(id);
});
}
if (_.isRegExp(arg)) {
var res = {};
_.each(_.keys(self.definitions), function (id) {
if (arg.test(id)) {
res[id] = self._require(id);
}
});
return res;
}
return self._require(arg);
};
// Registers public API on the global object.
self.register = function (name) {
var previous = global[name],
api = {
define: self.define,
predefined: self.predefined,
require: self.require,
noConflict: function () {
if (global[name] === api) {
global[name] = previous;
}
return api;
}
};
global[name] = api;
};
};
var modulejs = new ModuleJs();
modulejs.register(name);
// debugger
// --------
var debugName = name.toUpperCase();
if (_.isFunction(global[debugName])) {
global[debugName] = new global[debugName](modulejs);
}
}(this, _, 'modulejs'));

View file

@ -0,0 +1,247 @@
/*! modulejs 0.2 - //larsjung.de/modulejs - MIT License */
(function (global, name) {
'use strict';
var objProto = Object.prototype,
arrayForEach = Array.prototype.forEach,
isType = function (arg, type) {
return objProto.toString.call(arg) === '[object ' + type + ']';
},
isString = function (arg) {
return isType(arg, 'String');
},
isFunction = function (arg) {
return isType(arg, 'Function');
},
isArray = Array.isArray || function (arg) {
return isType(arg, 'Array');
},
isObject = function (arg) {
return arg === new Object(arg);
},
has = function (arg, id) {
return objProto.hasOwnProperty.call(arg, id);
},
each = function (obj, iterator, context) {
if (arrayForEach && obj.forEach === arrayForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i += 1) {
iterator.call(context, obj[i], i, obj);
}
} else {
for (var key in obj) {
if (has(obj, key)) {
iterator.call(context, obj[key], key, obj);
}
}
}
},
contains = function (array, item) {
for (var i = 0, l = array.length; i < l; i += 1) {
if (array[i] === item) {
return true;
}
}
return false;
},
uniq = function (array) {
var elements = {},
result = [];
each(array, function (el) {
if (!has(elements, el)) {
result.push(el);
elements[el] = 1;
}
});
return result;
},
err = function (condition, code, message) {
if (condition) {
throw {
code: code,
msg: message,
toString: function () {
return name + ' error ' + code + ': ' + message;
}
};
}
},
// Module definitions.
definitions = {},
// Module instances.
instances = {},
resolve = function (id, cold, stack) {
err(!isString(id), 31, 'id must be a string "' + id + '"');
if (!cold && has(instances, id)) {
return instances[id];
}
var def = definitions[id];
err(!def, 32, 'id not defined "' + id + '"');
stack = (stack || []).slice(0);
stack.push(id);
var deps = [];
each(def.deps, function (depId, idx) {
err(contains(stack, depId), 33, 'cyclic dependencies: ' + stack + ' & ' + depId);
if (cold) {
deps = deps.concat(resolve(depId, cold, stack));
deps.push(depId);
} else {
deps[idx] = resolve(depId, cold, stack);
}
});
if (cold) {
return uniq(deps);
}
var obj = def.fn.apply(global, deps);
instances[id] = obj;
return obj;
},
// Public methods
// --------------
// ### define
// Defines a module for `id: String`, optional `deps: Array[String]`,
// `arg: Object/function`.
define = function (id, deps, arg) {
// sort arguments
if (arg === undefined) {
arg = deps;
deps = [];
}
// check arguments
err(!isString(id), 11, 'id must be a string "' + id + '"');
err(definitions[id], 12, 'id already defined "' + id + '"');
err(!isArray(deps), 13, 'dependencies for "' + id + '" must be an array "' + deps + '"');
err(!isObject(arg) && !isFunction(arg), 14, 'arg for "' + id + '" must be object or function "' + arg + '"');
// accept definition
definitions[id] = {
id: id,
deps: deps,
fn: isFunction(arg) ? arg : function () { return arg; }
};
},
// ### require
// Returns an instance for `id`.
require = function (id) {
return resolve(id);
},
// ### state
// Returns an object that holds infos about the current definitions and dependencies.
state = function () {
var res = {};
each(definitions, function (def, id) {
res[id] = {
// direct dependencies
deps: def.deps.slice(0),
// transitive dependencies
reqs: resolve(id, true),
// already initiated/required
init: has(instances, id)
};
});
each(definitions, function (def, id) {
var inv = [];
each(definitions, function (def2, id2) {
if (contains(res[id2].reqs, id)) {
inv.push(id2);
}
});
// all inverse dependencies
res[id].reqd = inv;
});
return res;
},
// ### log
// Returns a string that displays module dependencies.
log = function (inv) {
var out = '\n';
each(state(), function (st, id) {
var list = inv ? st.reqd : st.reqs;
out += (st.init ? '* ' : ' ') + id + ' -> [ ' + list.join(', ') + ' ]\n';
});
return out;
};
// Register Public API
// -------------------
global[name] = {
define: define,
require: require,
state: state,
log: log
};
// Uncomment to run internal tests.
/*
if (global[name.toUpperCase()] === true) {
global[name.toUpperCase()] = {
isString: isString,
isFunction: isFunction,
isArray: isArray,
isObject: isObject,
has: has,
each: each,
contains: contains,
uniq: uniq,
err: err,
definitions: definitions,
instances: instances,
resolve: resolve
};
} // */
}(this, 'modulejs'));

View file

@ -1,133 +0,0 @@
/*! modulejs-debug 0.1 - //larsjung.de/qrcode - MIT License */
(function (global, _, name) {
'use strict';
var Debugger = function (modulejs) {
var self = this;
self.modulejs = modulejs;
self.clear = function () {
modulejs.definitions = {};
modulejs.instances = {};
};
self.isDefined = function (id) {
return _.isString(id) && !!modulejs.definitions[id];
};
self.ids = function (regexp) {
var ids = _.map(modulejs.definitions, function (def) {
return def.id;
});
if (!_.isRegExp(regexp)) {
return ids;
}
return _.filter(ids, function (id) {
return regexp.test(id);
});
};
var _deps = function (id, stack) {
var deps = [];
var def = modulejs.definitions[id];
if (def) {
stack = (stack || []).slice(0);
stack.push(id);
_.each(def.deps, function (depId) {
if (_.indexOf(stack, depId) >= 0) {
deps = deps.concat([false, def.id]);
return deps;
}
deps = deps.concat(_deps(depId, stack));
deps.push(depId);
});
}
return _.uniq(deps);
};
self.deps = function (ids) {
if (_.isString(ids)) {
return _deps(ids);
} else if (_.isArray(ids)) {
var deps = [];
_.each(ids, function (id) {
deps = deps.concat(_deps(id));
});
return _.uniq(deps);
}
var res = {};
_.each(modulejs.definitions, function (def, id) {
res[id] = _deps(id);
});
return res;
};
self.log = function (showInvDeps) {
var allDeps = self.deps(),
allInvDeps = {},
out = '\n';
if (!showInvDeps) {
_.each(allDeps, function (deps, id) {
out += (_.has(modulejs.instances, id) ? '* ' : ' ') + id + ' -> [ ' + deps.join(', ') + ' ]\n';
});
} else {
_.each(modulejs.definitions, function (def) {
var invDeps = [];
_.each(allDeps, function (depIds, id) {
if (_.indexOf(depIds, def.id) >= 0) {
invDeps.push(id);
}
});
allInvDeps[def.id] = invDeps;
});
_.each(allInvDeps, function (invDeps, id) {
out += (_.has(modulejs.instances, id) ? '* ' : ' ') + id + ' <- [ ' + invDeps.join(', ') + ' ]\n';
});
}
return out;
};
};
global[name.toUpperCase()] = Debugger;
}(this, _, 'modulejs'));

View file

@ -2,11 +2,6 @@
(function ($) {
'use strict';
modulejs.predefined('jQuery', jQuery);
modulejs.predefined('amplify', amplify);
modulejs.predefined('moment', moment);
modulejs.predefined('H5AI_CONFIG', H5AI_CONFIG);
// @include "core/entry.js"
// @include "core/event.js"
@ -48,6 +43,21 @@
$(function () {
// define it on doc ready, so the script order in the doc doesn't matter
modulejs.define('H5AI_CONFIG', H5AI_CONFIG);
modulejs.define('amplify', amplify);
// `jQuery` and `moment` are itself functions, so they have to be wrapped
// to not be taken as a constructor
modulejs.define('jQuery', function () {
return jQuery;
});
modulejs.define('moment', function () {
return moment;
});
modulejs.require($('body').attr('id'));
});

View file

@ -7,15 +7,11 @@
// @include "inc/lib/jquery.qrcode-0.2.min.js"
// @include "inc/lib/jquery.scrollpanel-0.1.min.js"
// underscore libs
// ---------------
// @include "inc/lib/underscore-1.3.1.min.js"
// @-include "inc/lib/modulejs-debug-0.1.js"
// @include "inc/lib/modulejs-0.1.js"
// other libs
// ----------
// @include "inc/lib/underscore-1.3.1.min.js"
// @include "inc/lib/amplify-1.1.0.min.js"
// @include "inc/lib/modulejs-0.2.js"
// @include "inc/lib/moment-1.5.0.min.js"
// @include "inc/lib/json2.js"
// @include "inc/lib/base64.js"