-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
521 lines (416 loc) · 9.76 KB
/
loader.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* 定义一种统一的方式来书写js
* 使用统一的方式管理模块的依赖,
* 并提供异步加载机制
* 类似于amd loader api,但提供更加实用的特性:如匿名模块马上进行初始化
*
* @author qijun.weiqj
*/
(function(window) {
var may = {
version: '1.0'
};
// utility
var noop = function() {},
toString = Object.prototype.toString,
isArray = function(o) {
return toString.apply(o) === '[object Array]';
},
extend = function(des, src) {
for (var k in src) {
var v = src[k];
if (v !== null && v !== undefined) {
des[k] = v;
}
}
return des;
};
/**
* simple log support
* may.log.handler can be overwrite by other module
*/
var logLevel = { none: 0, error: 1, warn: 2, info: 3, debug: 4 },
isLogEnabled = function(level) {
return logLevel[level] <= may.log.level;
};
may.log = function(msg, level, type) {
return may.log.handler(msg, level, type);
};
may.log.handler = window.console ? function(msg, level, type) {
level = level || 'info';
if (isLogEnabled(level)) {
msg = (type ? '[' + type + '] ' : '') + msg;
if (console[level]) {
console[level](msg);
} else if (console.log) {
console.log(msg);
}
}
} : noop;
/**
* log level
* support set log level in query string,
* exp: '?debug=true' or '?may-log-level=error'
*/
var getLevel = function() {
var search = window.location.search,
level = /\bdebug=true\b/.test(search) ? 'debug' : false;
level = level || (/debug-log-level=(\w+)\b/.exec(search) || {})[1] || 'error';
return logLevel[level];
};
may.log.isEnabled = isLogEnabled;
may.log.level = getLevel();
may.isDebug = may.log.level === logLevel['debug'];
/**
* error handler, can be overwrite by other module
*/
may.error = function(e) {
e = typeof e === 'string' ? new Error(e) : e;
throw e;
};
// used for loader
var log = function(msg, level) {
return may.log(msg, level, 'may:Loader');
};
var assert = function(bool, message) {
bool || may.error(message);
};
var cache = {}, // module cache
EMPTY_DEPENDS = [],
FAIL = {};
may._guid = 1;
may._current = null;
/**
* define a module
* unlike amd loader, anonymous module will require immediately
*/
var define = function(config, id, depends, factory) {
var args = regularArgs(id, depends, factory),
id = args.id,
mods = config.modules;
if (mods[id]) {
log(getId(config, id) + ' already configured, ignore', 'warn');
return;
} else {
mods[id] = args;
}
// if id begin with !, that it is an anonymouse module, we require it immediately
if (id.indexOf('!') === 0) {
require(config, [id]);
}
};
/**
* define(id, depends, factory)
* define(id, factory{not array})
* define(id, depends{array})
* define(depends{array}, factory)
* define(factory{function})
*/
var regularArgs = function(id, depends, factory) {
// define(a, b) -> define(a, [], b)
if (factory === undefined && !isArray(depends)) {
factory = depends;
depends = EMPTY_DEPENDS;
}
if (typeof id === 'function') {
factory = id;
depends = EMPTY_DEPENDS;
id = null;
} else if (isArray(id)) {
depends = id;
id = null;
}
assert(isArray(depends), 'arguments error, depends should be an array');
id = id || '!anonymous' + may._guid++;
return { id: id, depends: depends, factory: factory };
};
//~ define
var require = function(config, depends, callback) {
depends = depends ?
isArray(depends) ? depends : [depends] :
[];
var list = [],
i = 0,
n = depends.length,
depend = null,
check = function() {
for (var j = 0; j < n; j++) {
if (!list[j]) {
return;
}
}
callback && callback.apply(null, list);
},
load = function(index) {
var depend = depends[index];
loadModule(config, depend, function(o) {
assert(o !== FAIL, 'load ' + getId(config, depend) + ' error');
list[index] = o;
check();
});
};
for (; i < n; i++) {
load(i);
}
return list[0];
};
var loadModule = function(config, id, callback) {
if (id === 'require') {
callback(config.require);
return;
}
var alias = null,
pos = null,
o = null,
otherConfig = null;
if (config.alias &&
(alias = config.alias[id])) {
id = alias;
}
// may be require other project, exp may:class, offer:widget.Tabs
// this feature can shorten module id
pos = id.indexOf(':');
if (pos !== -1 &&
(otherConfig = cache[id.substr(0, pos)])) {
return loadModule(otherConfig, id.substr(pos + 1), callback);
}
o = config.modules[id];
if (o) {
loadModuleFromDef(config, o, callback);
} else {
loadModuleFromScript(config, id, callback);
}
};
var loadModuleFromDef = function(config, o, callback) {
var id = o.id,
longId = getId(config, id);
if (o.load) {
o.load++;
log(longId + ' is loaded [' + o.load + ']');
callback(o.data);
return;
}
o.loadList = o.loadList || [];
o.loadList.push(callback);
if (o.loadList.length > 1) {
return;
}
var depends = o.depends.length ? o.depends : ['require'];
require(config, depends, function() {
var factory = o.factory,
loadList = o.loadList;
if (typeof factory === 'function') {
factory = factory.apply(null, arguments);
log('initialize ' + longId + ' complete!');
}
o.data = factory;
log(longId + ' is loaded');
for (var i = 0, c = loadList.length; i < c; i++) {
loadList[i](factory);
}
o.load = loadList.length;
o.loadList = null;
});
};
var rAbs = /^https?:\/\//,
rFirst = /^([^\/]+)/,
rLastSlash = /\/$/,
loadList = {};
var loadModuleFromScript = function(config, id, callback) {
var list = loadList[id] = loadList[id] || [],
path = null,
isAbsPath = false;
list.push(callback);
if (list.length > 1) {
return;
}
if (rAbs.test(id)) {
path = id;
isAbsPath = true;
} else {
path = getPath(config, id);
}
log('load module from : ' + path);
config.load(path, {
success: function() {
// if id is AbsPath, we define an module manually
if (isAbsPath) {
define(id, EMPTY_DEPENDS, noop);
}
var o = config.modules[id];
if (!o) {
log('load module error ' + getId(id));
return;
}
o.async = true;
loadModuleFromDef(config, o, function(factory) {
for (var i = 0, c = list.length; i < c; i++) {
list[i](factory);
}
});
delete loadList[id];
},
error: function() {
log('file not found, load module error ' + getId(id));
}
});
};
var getId = function(config, id) {
return config.id + ':' + id;
};
var getPath = function(config, id) {
var base = config.baseUrl,
pathes = config.pathes,
first = null,
path = null;
id = id.replace(/\./g, '/')
.replace(/([a-z])([A-Z])/g, function(s, m1, m2) {
return m1 + '-' + m2;
}).toLowerCase();
if (pathes) {
first = (rFirst.exec(id) || {})[1];
if (first &&
(path = pathes[first])) {
id = id.replace(rFirst, path.replace(rLastSlash, ''));
}
}
if (base) {
id = base.replace(rLastSlash, '') + '/' + id;
}
return id + '.js';
};
//~ require
var config = function(cfg) {
if (!cfg) {
// clear current
may._current = null;
return;
}
// set current
if (typeof cfg === 'string') {
log('set current loader: ' + cfg);
may._current = cache[cfg];
assert(may._current, 'config for ' + cfg + ' is not exist');
return may._current.facade;
}
// new config
assert(cfg.id, 'please specify config id');
assert(!cache[cfg.id], 'config for ' + cfg.id + ' is already exist');
log('config loader ' + cfg.id);
cfg = extend({
load: loadResource
}, cfg);
cfg.require = function(depends, callback) {
if (typeof depends === 'function') {
callback = depends;
depends = [];
}
return require(cfg, depends, callback);
};
cfg.define = function(id, depends, factory) {
return define(cfg, id, depends, factory);
};
cfg.modules = {};
cache[cfg.id] = cfg;
// if current empty, set ccurrent
if (!may._current) {
log('set current loader ' + cfg.id);
may._current = cfg;
}
may._current = may._current || cfg;
cfg.facade = { define: cfg.define, require: cfg.require };
return cfg.facade;
};
var globalDefine = function(id, depends, factory) {
var config = may._current;
assert(config, 'current config not exist');
return config.define(id, depends, factory);
};
//~
//jQuery support
var loadScript = function(url, success, error) {
var flag = false,
timer = null;
jQuery.ajax(url, {
dataType: 'script',
cache: true,
success: function() {
if (flag) {
return;
}
clearTimeout(timer);
success();
},
error: function() {
if (flag) {
return;
}
clearTimeout(timer);
error();
}
});
timer = setTimeout(function() {
flag = true;
error();
}, 10000);
};
//~ loadScript
var loadCss = function(url, success, error) {
var link = document.createElement('link'),
timeout = false,
timer = null,
img = null;
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
document.getElementsByTagName('head')[0].appendChild(link);
img = document.createElement('img');
img.onerror = function() {
if (timeout) {
return;
}
clearTimeout(timer);
log('load css success ' + url);
success && success();
}
img.src = url;
timer = setTimeout(function() {
timeout = true;
log('load css timeout ' + url);
error && error();
}, 10000);
return link;
};
var rCss = /\.css(\?[-\w]*)?$/;
var loadResource = function(url, options) {
if (rCss.test(url)) {
loadCss(url, options.success, options.error);
} else {
loadScript(url, options.success, options.error);
}
};
//~
// define system loader
config({ id: 'url' }); // for wrap normal script
config({ id: 'may' });
config('may'); // set current loader
may.config = config;
// packing
var _may = window.may,
_define = window.define;
// for debug
may._cache = cache;
may.noConflict = function(deep) {
if (deep) {
window.define = _define;
}
window.may = _may;
return may;
};
window.may = may;
window.define = globalDefine;
// define an css loader
window.define('CssLoader', function() {
return { load: loadCss };
});
})(window);