/
head.loader.js
252 lines (189 loc) · 4.97 KB
/
head.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
/**
Head JS The only script in your <HEAD>
Copyright Tero Piirainen (tipiirai)
License MIT / http://bit.ly/mit-license
http://headjs.com
*/
(function(doc) {
var head = doc.documentElement,
ie = navigator.userAgent.toLowerCase().indexOf("msie") != -1,
ready = false, // is HEAD "ready"
queue = [], // if not -> defer execution
handlers = {}, // user functions waiting for events
scripts = {}; // loadable scripts in different states
/*** public API ***/
var head_var = window.head_conf && head_conf.head || "head",
api = window[head_var] = (window[head_var] || function() { api.ready.apply(null, arguments); });
api.js = function() {
var args = arguments,
rest = [].slice.call(args, 1),
next = rest[0];
if (!ready) {
queue.push(function() {
api.js.apply(null, args);
});
return api;
}
// multiple arguments
if (next) {
// preload the rest
if (!isFunc(next)) {
each(rest, function(el) {
if (!isFunc(el)) {
preload(getScript(el));
}
});
}
// load all recursively in order
load(getScript(args[0]), isFunc(next) ? next : function() {
api.js.apply(null, rest);
});
// single script
} else {
load(getScript(args[0]));
}
return api;
};
api.ready = function(key, fn) {
var script = scripts[key];
if (script && script.state == 'loaded') {
fn.call();
return api;
}
// shift arguments
if (isFunc(key)) {
fn = key;
key = "ALL";
}
var arr = handlers[key];
if (!arr) { arr = handlers[key] = [fn]; }
else { arr.push(fn); }
return api;
};
/*
api.dump = function() {
console.dir(scripts);
};
*/
/*** private functions ***/
function getScript(url) {
var script = scripts[url.name || url];
if (script) { return script; }
if (typeof url == 'object') {
for (var key in url) {
if (url[key]) {
script = { name: key, url: url[key] };
}
}
} else {
var els = url.split("/"),
name = els[els.length -1],
i = name.indexOf("?");
script = {
name: i != -1 ? name.substring(0, i) : name,
url: url
};
}
scripts[script.name] = script;
return script;
}
function each(arr, fn) {
if (!arr) { return; }
// arguments special type
if (typeof arr == 'object') { arr = [].slice.call(arr); }
// do the job
for (var i = 0; i < arr.length; i++) {
fn.call(arr, arr[i], i);
}
}
function isFunc(el) {
return Object.prototype.toString.call(el) == '[object Function]';
}
function onPreload(script) {
script.state = "preloaded";
each(script.onpreload, function(el) {
el.call();
});
}
function preload(script, callback) {
if (!script.state) {
script.state = "preloading";
script.onpreload = [];
/*
Browser detection required. Firefox does not support script.type = text/cache
http://www.phpied.com/preload-cssjavascript-without-execution/
*/
if (/Firefox/.test(navigator.userAgent)) {
var obj = doc.createElement('object');
obj.data = script.url;
obj.width = 0;
obj.height = 0;
obj.onload = function() {
onPreload(script);
// avoid spinning progress indicator with setTimeout
setTimeout(function() { head.removeChild(obj); }, 1);
};
head.appendChild(obj);
} else {
scriptTag({ src: script.url, type: 'cache'}, function() {
onPreload(script);
});
}
}
}
function load(script, callback) {
if (script.state == 'loaded') { return callback(); }
if (script.state == 'preloading') {
return script.onpreload.push(function() {
load(script, callback);
});
}
script.state = 'loading';
scriptTag(script.url, function() {
script.state = 'loaded';
if (callback) { callback.call(); }
// handlers for this script
each(handlers[script.name], function(fn) {
fn.call();
});
// TODO: do not run until DOM is loaded
var allLoaded = true;
for (var name in scripts) {
if (scripts[name].state != 'loaded') { allLoaded = false; }
}
if (allLoaded) {
each(handlers.ALL, function(fn) {
if (!fn.done) { fn.call(); }
fn.done = true;
});
}
});
}
// if callback == true --> preload
function scriptTag(src, callback) {
var elem = doc.createElement('script');
elem.type = 'text/' + (src.type || 'javascript');
elem.src = src.src || src;
elem.onreadystatechange = elem.onload = function() {
var state = elem.readyState;
if (!callback.done && (!state || /loaded|complete/.test(state))) {
callback.call();
callback.done = true;
}
// cleanup. IE runs into trouble
if (!ie) {
head.removeChild(elem);
}
};
head.appendChild(elem);
}
/*
Start after a small delay: guessing that the the head tag needs to be closed
*/
setTimeout(function() {
ready = true;
each(queue, function(fn) {
fn.call();
});
}, 200);
})(document);