-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptLoader.js
More file actions
248 lines (237 loc) · 6.04 KB
/
scriptLoader.js
File metadata and controls
248 lines (237 loc) · 6.04 KB
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
/* ----- ScriptLoader -----
Randy Hoggard 2016
A constructor to create an object that can dynamically add script to document
------------------------ */
function ScriptLoader(config,callback){//doneCallback,errorCallback){
//***** Private Members ****************************************************
var scripts,
parent,
done,
oldOnload,
mode;
//***** Public Members *****************************************************
this.loadFile = loadFile;
this.loadText = loadText;
//***** Initialize *********************************************************
//attach init to document as soon as DOM is ready
attachInit();
//***** Private Methods ****************************************************
function init(){
scripts = [];
done = [];
if(config){
//config present
//use config to set member values
if(config.scripts){
if(isArray(config.scripts)){
scripts = config.scripts;
} else {
callback(new Error("Invalid config value 'scripts'. 'scripts' must be an array of strings (urls)."),"error" );
//need to throw Error here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
} else {
//use default
scripts = [];
}
if(config.appendTo){
switch(config.appendTo){
case "head":
//append to head
parent = document.getElementsByTagName('head')[0];
break;
case "body":
//append to body
parent = document.getElementsByTagName('body')[0];
break;
case "default":
//use default,attatch to parent of first script
parent = document.getElementsByTagName('script')[0].parentNode;
break;
default:
//invalid value, throw error
callback(new Error("invalid config value 'appendTo'. 'appendTo' must be a string that matches one of the following values: 'head','body','default'."),"error");
}
} else {
//use default,attatch to parent of first script
parent = document.getElementsByTagName('script')[0].parentNode;
}
if(config.mode){
if(config.mode === "async" || config.mode === "sync" || config.mode === "fastSync"){
//valid mode, set mode
mode = config.mode;
} else {
//invalid mode, throw error
callback(new Error("Invalid config value 'mode'. 'mode' must be a string that matches one of the following values: 'async','fastSync','sync'."),"error");
}
} else {
//use default
mode = "fastSync";
}
} else {
//config not present
//use defaults to set member values
scripts = [];
parent = document.getElementsByTagName('script')[0].parentNode;
;
mode = "fastSync";
}
//check to see if there are any scripts to load
if(scripts.length > 0){
//scripts need to be loaded
switch(mode){
case "sync":
syncLoad(0,callback);
break;
case "fastSync":
fastSyncLoad(callback);
break;
case "async":
asyncLoad(callback);
break;
default:
//error, invalid mode
callback(new Error(mode + " is not a valid mode. Valid modes are sync,fastSync,and async."),"error");
}
} else {
//no scripts to load
callback(null,"scriptLoader finished");
}
}
function attachInit(){
if(document.addEventListener){
//Modern
document.addEventListener("DOMContentLoaded",init);
} else {
//Old IE
if(document.attachEvent){
document.attachEvent("onreadystatechange",function(){
if (document.readyState === "interactive") {
document.detachEvent("onreadystatechange", arguments.callee);
init();
}
});
} else {
//use fallback
oldOnload = window.onload;
window.onload = function(){
oldOnload && oldOnload();
init();
};
}
}
}
function fastSyncLoad(callback){
var i,
max = scripts.length,
req = [],
done = -1,
script;
for(i = 0; i < max; i++){
req[i] = new XMLHttpRequest();
req[i].reqId = i;
req[i].addEventListener("load",function(){
load(this.reqId,this.responseText,callback);
});
req[i].open("GET",/*"/scriptLoader/"+ */scripts[i]);
req[i].send();
}
function load(index,text,callback){
var script;
if(index - 1 == done){
//create script
script = createScript();
script.text = text;
appendScript(script);
done++;
if(done +1 == max){
//done
callback(null,"loaded");
}
} else {
setTimeout(function(){
load(index,text,callback);
},10);
}
}
}
function asyncLoad(callback){
var max = scripts.length,
done = 0,
i,
script;
for(i = 0; i < max; i++){
script = createScript(scripts[i]);
addCallback(script,function(){
done++;
});
appendScript(script);
}
asyncCheckDone(callback);
function asyncCheckDone(callback){
if(done == max){
callback(null,"loaded");
} else {
setTimeout(function(){
asyncCheckDone(callback);
},5);
}
}
}
function syncLoad(start,callback){
var max = scripts.length,
doneMax = done.length,
i;
if(start >= max){
//all scripts done
callback(null,"loaded");
} else {
//script not loaded yet, load it
//create script element
var script = createScript(scripts[start]);
//add callback
addCallback(script, function(){
done.push(scripts[start]);
syncLoad(start + 1,callback);
});
//add script to dom
appendScript(script);
}
}
function loadFile(src,callback){
var script = createScript(src);
addCallback(script,callback);
appendScript(script);
}
function loadText(scriptText,callback){
var script = createScript();
script.text = scriptText;
addCallback(script, callback);
appendScript(script);
}
function createScript(src, id){
var script = document.createElement('script');
script.type = "text/javascript";
if(id){
script.id = id;
}
if(src){
script.src = src;
}
return script;
}
function addCallback(script,callback){
script.onload = function(){
callback(null,"loaded");
};
return true;
}
function appendScript(script){
parent.appendChild(script);
return true;
}
function isArray(obj){
return Object.prototype.toString.call(obj) === '[object Array]';
}
function isValidUrl(url){
}
}