mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-31 15:28:19 -04:00
Update modulejs.
This commit is contained in:
parent
c777a9f949
commit
80d0952d1f
3 changed files with 94 additions and 46 deletions
|
@ -14,6 +14,7 @@ modulejs.define('view/ensure', ['$', 'config', 'core/event'], function ($, confi
|
|||
|
||||
if (
|
||||
$(selr).text() !== sequence ||
|
||||
$(sela).attr('href') !== url ||
|
||||
$(sela).filter(isVisible).length !== 1 ||
|
||||
$(selr).filter(isVisible).length !== 1 ||
|
||||
$(selb).filter(isVisible).length !== 1
|
||||
|
|
|
@ -1,35 +1,53 @@
|
|||
/*! modulejs 0.2 - //larsjung.de/modulejs - MIT License */
|
||||
/*! modulejs 0.4.5 - //larsjung.de/modulejs/ - MIT License */
|
||||
|
||||
(function (global, name) {
|
||||
'use strict';
|
||||
|
||||
|
||||
// Helpers
|
||||
// -------
|
||||
|
||||
// References.
|
||||
var objProto = Object.prototype,
|
||||
arrayForEach = Array.prototype.forEach,
|
||||
isType = function (arg, type) {
|
||||
|
||||
return objProto.toString.call(arg) === '[object ' + type + ']';
|
||||
},
|
||||
isString = function (arg) {
|
||||
// Returns a function that returns `true` if `arg` is of the correct `type`, otherwise `false`.
|
||||
createIsTypeFn = function (type) {
|
||||
|
||||
return isType(arg, 'String');
|
||||
},
|
||||
isFunction = function (arg) {
|
||||
return function (arg) {
|
||||
|
||||
return isType(arg, 'Function');
|
||||
return objProto.toString.call(arg) === '[object ' + type + ']';
|
||||
};
|
||||
},
|
||||
isArray = Array.isArray || function (arg) {
|
||||
|
||||
return isType(arg, 'Array');
|
||||
},
|
||||
// ### isString
|
||||
// Returns `true` if argument is a string, otherwise `false`.
|
||||
isString = createIsTypeFn('String'),
|
||||
|
||||
// ### isFunction
|
||||
// Returns `true` if argument is a function, otherwise `false`.
|
||||
isFunction = createIsTypeFn('Function'),
|
||||
|
||||
// ### isArray
|
||||
// Returns `true` if argument is an array, otherwise `false`.
|
||||
isArray = Array.isArray || createIsTypeFn('Array'),
|
||||
|
||||
// ### isObject
|
||||
// Returns `true` if argument is an object, otherwise `false`.
|
||||
isObject = function (arg) {
|
||||
|
||||
return arg === new Object(arg);
|
||||
},
|
||||
|
||||
// ### has
|
||||
// Short cut for `hasOwnProperty`.
|
||||
has = function (arg, id) {
|
||||
|
||||
return objProto.hasOwnProperty.call(arg, id);
|
||||
},
|
||||
|
||||
// ### each
|
||||
// Iterates over all elements af an array or all own keys of an object.
|
||||
each = function (obj, iterator, context) {
|
||||
|
||||
if (arrayForEach && obj.forEach === arrayForEach) {
|
||||
|
@ -46,15 +64,21 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
contains = function (array, item) {
|
||||
|
||||
// ### contains
|
||||
// Returns `true` if array contains element, otherwise `false`.
|
||||
contains = function (array, element) {
|
||||
|
||||
for (var i = 0, l = array.length; i < l; i += 1) {
|
||||
if (array[i] === item) {
|
||||
if (array[i] === element) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
// ### uniq
|
||||
// Returns an new array containing no duplicates. Preserves first occurence and order.
|
||||
uniq = function (array) {
|
||||
|
||||
var elements = {},
|
||||
|
@ -70,57 +94,84 @@
|
|||
|
||||
return result;
|
||||
},
|
||||
|
||||
// ### err
|
||||
// Throws an error if `condition` is `true`.
|
||||
err = function (condition, code, message) {
|
||||
|
||||
if (condition) {
|
||||
throw {
|
||||
// machine readable
|
||||
code: code,
|
||||
|
||||
// human readable
|
||||
msg: message,
|
||||
|
||||
// let it be helpful in consoles
|
||||
toString: function () {
|
||||
|
||||
return name + ' error ' + code + ': ' + message;
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
// Private
|
||||
// -------
|
||||
|
||||
// ### definitions
|
||||
// Module definitions.
|
||||
definitions = {},
|
||||
|
||||
// ### instances
|
||||
// Module instances.
|
||||
instances = {},
|
||||
|
||||
resolve = function (id, cold, stack) {
|
||||
// ### resolve
|
||||
// Resolves an `id` to an object, or if `onlyDepIds` is `true` only returns dependency-ids.
|
||||
// `stack` is used internal to check for circular dependencies.
|
||||
resolve = function (id, onlyDepIds, stack) {
|
||||
|
||||
// check arguments
|
||||
err(!isString(id), 31, 'id must be a string "' + id + '"');
|
||||
|
||||
if (!cold && has(instances, id)) {
|
||||
// if a module is required that was already created return that object
|
||||
if (!onlyDepIds && has(instances, id)) {
|
||||
return instances[id];
|
||||
}
|
||||
|
||||
// check if `id` is defined
|
||||
var def = definitions[id];
|
||||
err(!def, 32, 'id not defined "' + id + '"');
|
||||
|
||||
// copy resolve stack and add this `id`
|
||||
stack = (stack || []).slice(0);
|
||||
stack.push(id);
|
||||
|
||||
// if onlyDepIds this will hold the dependency-ids, otherwise it will hold the dependency-objects
|
||||
var deps = [];
|
||||
|
||||
each(def.deps, function (depId, idx) {
|
||||
each(def.deps, function (depId) {
|
||||
|
||||
err(contains(stack, depId), 33, 'cyclic dependencies: ' + stack + ' & ' + depId);
|
||||
// check for circular dependencies
|
||||
err(contains(stack, depId), 33, 'circular dependencies: ' + stack + ' & ' + depId);
|
||||
|
||||
if (cold) {
|
||||
deps = deps.concat(resolve(depId, cold, stack));
|
||||
if (onlyDepIds) {
|
||||
deps = deps.concat(resolve(depId, onlyDepIds, stack));
|
||||
deps.push(depId);
|
||||
} else {
|
||||
deps[idx] = resolve(depId, cold, stack);
|
||||
deps.push(resolve(depId, onlyDepIds, stack));
|
||||
}
|
||||
});
|
||||
|
||||
if (cold) {
|
||||
// if `onlyDepIds` return only dependency-ids in right order
|
||||
if (onlyDepIds) {
|
||||
return uniq(deps);
|
||||
}
|
||||
|
||||
// create, memorize and return object
|
||||
var obj = def.fn.apply(global, deps);
|
||||
instances[id] = obj;
|
||||
return obj;
|
||||
|
@ -128,8 +179,8 @@
|
|||
|
||||
|
||||
|
||||
// Public methods
|
||||
// --------------
|
||||
// Public
|
||||
// ------
|
||||
|
||||
// ### define
|
||||
// Defines a module for `id: String`, optional `deps: Array[String]`,
|
||||
|
@ -222,13 +273,8 @@
|
|||
define: define,
|
||||
require: require,
|
||||
state: state,
|
||||
log: log
|
||||
};
|
||||
|
||||
// Uncomment to run internal tests.
|
||||
/*
|
||||
if (global[name.toUpperCase()] === true) {
|
||||
global[name.toUpperCase()] = {
|
||||
log: log,
|
||||
_private: {
|
||||
isString: isString,
|
||||
isFunction: isFunction,
|
||||
isArray: isArray,
|
||||
|
@ -241,7 +287,7 @@
|
|||
definitions: definitions,
|
||||
instances: instances,
|
||||
resolve: resolve
|
||||
};
|
||||
} // */
|
||||
}
|
||||
};
|
||||
|
||||
}(this, 'modulejs'));
|
Loading…
Add table
Add a link
Reference in a new issue