-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
165 lines (134 loc) · 3.26 KB
/
module.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function (window, undefined) {
'use strict';
if (typeof Array.prototype.indexOf === 'undefined') {
Array.prototype.indexOf = function (o) {
for (var i = 0, j = this.length; i < j; i++) {
if (this[i] === o) return i;
}
}
return -1;
}
// module state: 0 loading, 1 loaded, 2 waiting, 3 ready 4 runned
var Module = {
use: use,
mods: {}
};
function define (id, des, fn) {
var mod = Module.mods[id];
if (!mod) {
mod = Module.mods[id] = {};
}
mod['state'] = 1;
if (arguments.length === 2) { // if dependency parameter is passed in, we won't scan the factory code
mod['factory'] = des;
mod['des'] = getDes(des.toString());
} else {
mod['factory'] = fn;
mod['des'] = des;
}
};
function getDes (fnStr) {
var m, des = [], r = /require\((['"])([^\1]+?)\1\)/mg;
while (m = r.exec(fnStr)) {
des.push(m[2]);
}
return des;
}
function use (ids, fn) {
if (typeof ids === 'string') {
if (arguments.length === 1) {
require(ids);
return;
}
ids = [ids];
}
var id = 'main' + new Date().getTime();
define(id, ids, function (require, exports, module) {
var args = [];
for (var i = 0, j = ids.length; i < j; i++) {
args.push(require(ids));
}
fn && fn.apply(null, args);
});
loadMod(id, function () {
require(id);
});
}
function require (id) {
var mod = Module.mods[id];
if (typeof mod === 'undefined' || typeof mod['factory'] === 'undefined') { return undefined; }
if (mod.state !== 4) {
var exports = {}, module = {}, o;
if (typeof mod.factory === 'function') {
o = mod.factory.call(null, require, exports, module);
mod.factory = module.exports || o || exports;
}
mod.state = 4;
}
return mod.factory;
}
function load (url, callback, charset) {
var script = document.createElement('script'),
head = document.head ||
document.getElementsByTagName('head')[0] ||
document.documentElement;
script.async = true;
if (charset) { script.charset = charset; }
if (!/.js$/i.test(url)) {
url += '.js';
}
script.src = url;
script.onerror = script.onload = script.onreadystatechange = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
// Handle memory leak in IE
script.onerror = script.onload = script.onreadystatechange = null;
// Remove the script
if ( script.parentNode ) {
script.parentNode.removeChild( script );
}
// Dereference the script
script = null;
callback();
}
};
head.insertBefore(script, head.firstChild);
}
function loadMod (id, callback) {
var mod = Module.mods[id];
if (!mod) {
mod = Module.mods[id] = { state: 0};
load(id, function () {
resolve(id, callback);
})
} else if (mod['state'] === 1) {
resolve(id, callback);
} else {
callback(id);
}
}
function resolve (id, callback) {
var mod = Module.mods[id];
if (typeof mod === 'undefined') {
Module.mods[id] = { state: 4 };
callback(id);
return;
}
var des = mod['des'];
if (!des || des.length === 0) {
mod['state'] = 3;
callback(id);
} else {
mod['state'] = 2; des = des.slice(0);
for (var i = 0, j = des.length; i < j; i++) {
loadMod(mod['des'][i], function (desId) {
des.splice(des.indexOf(desId), 1);
if (des.length === 0) {
callback(id);
}
});
}
}
}
window['define'] = define;
window['Module'] = Module;
})(window);