-
Notifications
You must be signed in to change notification settings - Fork 71
/
do.js
402 lines (333 loc) · 8.62 KB
/
do.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
/* Do version 2.0 pre
* creator: kejun (listenpro@gmail.com)
* 最新更新:2011-7-12
*/
(function(win, doc) {
// 已加载模块
var loaded = {},
// 已加载列表
loadList = {},
// 加载中的模块
loadingFiles = {},
// 内部配置文件
config = {
// 是否自动加载核心库
autoLoad: true,
// 加载延迟
timeout: 6000,
// 核心库
coreLib: ['http://t.douban.com/js/jquery.min.js'],
/* 模块依赖
* {
* moduleName: {
* path: 'URL',
* type:'js|css',
* requires:['moduleName1', 'fileURL']
* }
* }
*/
mods: {}
},
jsSelf = (function() {
var files = doc.getElementsByTagName('script');
return files[files.length - 1];
})(),
// 全局模块
globalList = [],
// 外部参数
extConfig,
// domready回调堆栈
readyList = [],
// DOM Ready
isReady = false,
// 模块间的公共数据
publicData = {},
// 公共数据回调堆栈
publicDataStack = {},
isArray = function(e) {
return e.constructor === Array;
},
getMod = function(e) {
var mods = config.mods, mod;
if (typeof e === 'string') {
mod = (mods[e])? mods[e] : { path: e };
} else {
mod = e;
}
return mod;
},
load = function(url, type, charset, cb) {
var wait, n, t, img,
done = function() {
loaded[url] = 1;
cb && cb(url);
cb = null;
win.clearTimeout(wait);
};
if (!url) {
return;
}
if (loaded[url]) {
loadingFiles[url] = false;
if (cb) {
cb(url);
}
return;
}
if (loadingFiles[url]) {
setTimeout(function() {
load(url, type, charset, cb);
}, 10);
return;
}
loadingFiles[url] = true;
wait = win.setTimeout(function() {
/* 目前延时回调处理,超时后如果有延时回调,执行回调,然后继续等
* 延时回调的意义是log延时长的URI,这个处理不属于加载器本身的功能移到外部
* 没有跳过是为了避免错误。
*/
if (config.timeoutCallback) {
try {
config.timeoutCallback(url);
} catch(ex) {}
}
}, config.timeout);
t = type || url.toLowerCase().split(/\./).pop().replace(/[\?#].*/, '');
if (t === 'js') {
n = doc.createElement('script');
n.setAttribute('type', 'text/javascript');
n.setAttribute('src', url);
n.setAttribute('async', true);
} else if (t === 'css') {
n = doc.createElement('link');
n.setAttribute('type', 'text/css');
n.setAttribute('rel', 'stylesheet');
n.setAttribute('href', url);
}
if (charset) {
n.charset = charset;
}
if (t === 'css') {
img = new Image();
img.onerror = function() {
done();
img.onerror = null;
img = null;
}
img.src = url;
} else {
// firefox, safari, chrome, ie9下加载失败触发
// 如果文件是404, 会比timeout早触发onerror。目前不处理404,只处理超时
n.onerror = function() {
done();
n.onerror = null;
};
// ie6~8通过创建vbscript可以识别是否加载成功。
// 但这样需先测试性加载再加载影响性能。即使没成功加载而触发cb,顶多报错,没必要杜绝这种报错
// ie6~9下加载成功或失败,firefox, safari, opera下加载成功触发
n.onload = n.onreadystatechange = function() {
var url;
if (!this.readyState ||
this.readyState === 'loaded' ||
this.readyState === 'complete') {
done();
n.onload = n.onreadystatechange = null;
}
};
}
jsSelf.parentNode.insertBefore(n, jsSelf);
},
// 加载依赖论文件(顺序)
loadDeps = function(deps, cb) {
var mods = config.mods,
id, m, mod, i = 0, len;
id = deps.join('');
len = deps.length;
if (loadList[id]) {
cb();
return;
}
function callback() {
if(!--len) {
loadList[id] = 1;
cb();
}
}
for (; m = deps[i++]; ) {
mod = getMod(m);
if (mod.requires) {
loadDeps(mod.requires, (function(mod){
return function(){
load(mod.path, mod.type, mod.charset, callback);
};
})(mod));
} else {
load(mod.path, mod.type, mod.charset, callback);
}
}
},
/*!
* contentloaded.js
*
* Author: Diego Perini (diego.perini at gmail.com)
* Summary: cross-browser wrapper for DOMContentLoaded
* Updated: 20101020
* License: MIT
* Version: 1.2
*
* URL:
* http://javascript.nwbox.com/ContentLoaded/
* http://javascript.nwbox.com/ContentLoaded/MIT-LICENSE
*
*/
// @win window reference
// @fn function reference
contentLoaded = function(fn) {
var done = false, top = true,
doc = win.document,
root = doc.documentElement,
add = doc.addEventListener ? 'addEventListener' : 'attachEvent',
rem = doc.addEventListener ? 'removeEventListener' : 'detachEvent',
pre = doc.addEventListener ? '' : 'on',
init = function(e) {
if (e.type == 'readystatechange' && doc.readyState != 'complete') return;
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
if (!done && (done = true)) fn.call(win, e.type || e);
},
poll = function() {
try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
init('poll');
};
if (doc.readyState == 'complete') fn.call(win, 'lazy');
else {
if (doc.createEventObject && root.doScroll) {
try { top = !win.frameElement; } catch(e) { }
if (top) {
poll();
}
}
doc[add](pre + 'DOMContentLoaded', init, false);
doc[add](pre + 'readystatechange', init, false);
win[add](pre + 'load', init, false);
}
},
fireReadyList = function() {
var i = 0, list;
if (readyList.length) {
for(; list = readyList[i++]; ) {
d.apply(this, list);
}
}
},
d = function() {
var args = [].slice.call(arguments), fn, id;
// 加载核心库
if (config.autoLoad &&
!loadList[config.coreLib.join('')]) {
loadDeps(config.coreLib, function(){
d.apply(null, args);
});
return;
}
// 加载全局库
if (globalList.length > 0 &&
!loadList[globalList.join('')]) {
loadDeps(globalList, function(){
d.apply(null, args);
});
return;
}
if (typeof args[args.length - 1] === 'function' ) {
fn = args.pop();
}
id = args.join('');
if ((args.length === 0 || loadList[id]) && fn) {
fn();
return;
}
loadDeps(args, function() {
loadList[id] = 1;
fn && fn();
});
};
d.add = function(sName, oConfig) {
if (!sName || !oConfig || !oConfig.path) {
return;
}
config.mods[sName] = oConfig;
};
d.delay = function() {
var args = [].slice.call(arguments), delay = args.shift();
win.setTimeout(function() {
d.apply(this, args);
}, delay);
};
d.global = function() {
var args = isArray(arguments[0])? arguments[0] : [].slice.call(arguments);
globalList = globalList.concat(args);
};
d.ready = function() {
var args = [].slice.call(arguments);
if (isReady) {
return d.apply(this, args);
}
readyList.push(args);
};
d.css = function(s) {
var css = doc.getElementById('do-inline-css');
if (!css) {
css = doc.createElement('style');
css.type = 'text/css';
css.id = 'do-inline-css';
jsSelf.parentNode.insertBefore(css, jsSelf);
}
if (css.styleSheet) {
css.styleSheet.cssText = css.styleSheet.cssText + s;
} else {
css.appendChild(doc.createTextNode(s));
}
};
d.setData = d.setPublicData = function(prop, value) {
var cbStack = publicDataStack[prop];
publicData[prop] = value;
if (!cbStack) {
return;
}
while (cbStack.length > 0) {
(cbStack.pop()).call(this, value);
}
};
d.getData = d.getPublicData = function(prop, cb) {
if (publicData[prop]) {
cb(publicData[prop]);
return;
}
if (!publicDataStack[prop]) {
publicDataStack[prop] = [];
}
publicDataStack[prop].push(function(value){
cb(value);
});
};
d.setConfig = function(n, v) {
config[n] = v;
return d;
};
d.getConfig = function(n) {
return config[n];
};
win.Do = d;
contentLoaded(function() {
isReady = true;
fireReadyList();
});
// 初始外部配置
extConfig = jsSelf.getAttribute('data-cfg-autoload');
if (extConfig) {
config.autoLoad = (extConfig.toLowerCase() === 'true') ? true : false;
}
extConfig = jsSelf.getAttribute('data-cfg-corelib');
if (extConfig) {
config.coreLib = extConfig.split(',');
}
})(window, document);