-
Notifications
You must be signed in to change notification settings - Fork 1
/
req.js
84 lines (71 loc) · 2.59 KB
/
req.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function define(name, dependencies, moduleFunc) {
var modules = define.modules = define.modules || [],
map = define.modulemap = define.modulemap || {};
function load(name) {
if (!name) throw "Module must have a name!";
modules.push(map[name] = { name: name });
if (name.indexOf('.') < 0) name += '.js';
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.type = 'text/javascript';
script.async = 'async';
script.src = require.baseUrl + name;
head.appendChild(script);
}
function exec(modCfg) {
// check if all dependencies are there
var dep = modCfg.dep, values = [], len = dep.length,
depMod, i = 0;
for (; i < len; ++i) {
depMod = map[dep[i]];
if (depMod) {
if (depMod.value) values.push(depMod.value);
} else load(dep[i]);
}
if (values.length == len) {
modCfg.value = modCfg.exec.apply(this, values);
delete modCfg.exec;
var name = modCfg.name,
deps = [];
// check if there are other modules depending on this
for (len = modules.length, i = 0; i < len;) if ((depMod = modules[i]) == modCfg) {
modules[i] = modules[--len];
modules.length = len;
if (!name) break;
} else {
if (name && !depMod.value && (dep = depMod.dep)) {
var j = dep.length-1;
while (j >= 0 && dep[j] != name) --j;
if (j >= 0) deps.push(depMod);
}
++i;
}
for (len = deps.length, i = 0; i < len; ++i) if (!deps[i].value) exec(deps[i]);
}
}
var cfg = map[name];
if (!cfg) {
cfg = { name: name };
if (name) map[name] = cfg;
modules.push(cfg);
}
cfg.dep = dependencies;
cfg.exec = moduleFunc;
exec(cfg);
}
function require(dependencies, execute) {
if (typeof dependencies === 'string' && !execute) {
var modCfg = define.modulemap[dependencies];
return modCfg ? modCfg.value : null;
}
define(null, dependencies, execute);
}
require.defined = function (module) { return !!define.modulemap[module]; };
require.config = function (cfg) {
require.baseUrl = cfg.baseUrl;
};
require.baseUrl = (function () {
var scripts = document.getElementsByTagName('script'),
src = scripts[scripts.length-1].src;
return src.substr(0, src.lastIndexOf('/')+1);
})();