forked from aisuda/amis-editor-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
307 lines (255 loc) · 6.55 KB
/
mod.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* @file 模块加载器,改成私有化的了,慎用。
*/
/* eslint-disable */
(function (global) {
var require, define;
var amis = window.amis || {};
// 避免重复加载而导致已定义模块丢失
if (amis.require) {
return;
}
var head = document.getElementsByTagName('head')[0];
var loadingMap = {};
var factoryMap = {};
var modulesMap = {};
var scriptsMap = {};
var resMap = {};
var pkgMap = {};
var createScripts = function (queues, onerror) {
var docFrag = document.createDocumentFragment();
for (var i = 0, len = queues.length; i < len; i++) {
var id = queues[i].id;
var url = queues[i].url;
if (url in scriptsMap) {
continue;
}
scriptsMap[url] = true;
var script = document.createElement('script');
if (onerror) {
(function (script, id) {
var tid = setTimeout(function () {
onerror(id);
}, require.timeout);
script.onerror = function () {
clearTimeout(tid);
onerror(id);
};
var onload = function () {
clearTimeout(tid);
};
if ('onload' in script) {
script.onload = onload;
} else {
script.onreadystatechange = function () {
if (
this.readyState === 'loaded' ||
this.readyState === 'complete'
) {
onload();
}
};
}
})(script, id);
}
script.type = 'text/javascript';
script.src = url;
docFrag.appendChild(script);
}
head.appendChild(docFrag);
};
var loadScripts = function (ids, callback, onerror) {
var queues = [];
for (var i = 0, len = ids.length; i < len; i++) {
var id = ids[i];
var queue = loadingMap[id] || (loadingMap[id] = []);
queue.push(callback);
//
// resource map query
//
var res = resMap[id] || resMap[id + '.js'] || {};
var pkg = res.pkg;
var url;
if (pkg) {
url = pkgMap[pkg].url || pkgMap[pkg].uri;
} else {
url = res.url || res.uri || id;
}
queues.push({
id: id,
url: url
});
}
createScripts(queues, onerror);
};
var runQueue = function (id) {
var queue = loadingMap[id];
if (queue) {
for (var i = 0, n = queue.length; i < n; i++) {
queue[i]();
}
delete loadingMap[id];
}
};
define = function (id, factory) {
id = id.replace(/\.js$/i, '');
factoryMap[id] = factory;
if (~factory.toString().indexOf('__mod__async__load')) {
var mod = {exports: {}};
factoryMap[id] = {
deffer: true,
callbacks: [],
loaded: false,
load: function () {}
};
factory.apply(mod, [require, mod.exports, mod]);
var load = mod.exports.__mod__async__load;
factoryMap[id].load = function () {
if (this.loaded) {
return;
}
this.loaded = true;
load(function (ret) {
var callbacks = factoryMap[id].callbacks;
factoryMap[id] = function () {
return ret;
};
callbacks.forEach(function (fn) {
fn();
});
runQueue(id);
});
};
if (loadingMap[id] && loadingMap[id].length) {
factoryMap[id].load();
}
} else {
runQueue(id);
}
};
require = function (id) {
// compatible with require([dep, dep2...]) syntax.
if (id && id.splice) {
return require.async.apply(this, arguments);
}
id = require.alias(id);
var mod = modulesMap[id];
if (mod) {
return mod.exports;
}
//
// init module
//
var factory = factoryMap[id];
if (!factory) {
throw '[ModJS] Cannot find module `' + id + '`';
}
mod = modulesMap[id] = {
exports: {}
};
//
// factory: function OR value
//
var ret =
typeof factory === 'function'
? factory.apply(mod, [require, mod.exports, mod])
: factory;
if (ret) {
mod.exports = ret;
}
return mod.exports;
};
require.async = function (names, onload, onerror) {
if (typeof names === 'string') {
names = [names];
}
var needMap = {};
var needNum = 0;
var needLoad = [];
function findNeed(depArr) {
var child;
for (var i = 0, n = depArr.length; i < n; i++) {
//
// skip loading or loaded
//
var dep = require.alias(depArr[i]);
if (dep in needMap) {
continue;
}
needMap[dep] = true;
if (dep in factoryMap) {
if (factoryMap[dep] && factoryMap[dep].deffer) {
needNum++;
factoryMap[dep].callbacks.push(updateNeed);
factoryMap[dep].load();
}
// check whether loaded resource's deps is loaded or not
child = resMap[dep] || resMap[dep + '.js'];
if (child && 'deps' in child) {
findNeed(child.deps);
}
continue;
}
needLoad.push(dep);
needNum++;
child = resMap[dep] || resMap[dep + '.js'];
if (child && 'deps' in child) {
findNeed(child.deps);
}
}
}
function updateNeed() {
if (0 === needNum--) {
var args = [];
for (var i = 0, n = names.length; i < n; i++) {
args[i] = require(names[i]);
}
onload && onload.apply(global, args);
}
}
findNeed(names);
loadScripts(needLoad, updateNeed, onerror);
updateNeed();
};
require.ensure = function (names, callback) {
require.async(names, function () {
callback && callback.call(this, require);
});
};
require.resourceMap = function (obj) {
var k;
var col;
// merge `res` & `pkg` fields
col = obj.res;
for (k in col) {
if (col.hasOwnProperty(k)) {
resMap[k] = col[k];
}
}
col = obj.pkg;
for (k in col) {
if (col.hasOwnProperty(k)) {
pkgMap[k] = col[k];
}
}
};
require.loadJs = function (url) {
if (url in scriptsMap) {
return;
}
scriptsMap[url] = true;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);
return script;
};
require.alias = function (id) {
return id.replace(/\.js$/i, '');
};
require.timeout = 5000;
amis.require = require;
amis.define = define;
window.amis = amis;
window.require = window.require || require;
})(this);