This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
fallback.js
571 lines (445 loc) · 13 KB
/
fallback.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* fallback.js v1.1.9 | http://fallback.io/ | Salvatore Garbesi <[email protected]> | (c) 2015 Dolox, Inc. */
(function (window, document) {
'use strict';
var fallback = {
// Callback storage from .ready and inline callback.
callbacks: [],
// Libraries that have completed/exhausted.
completed: {},
completed_count: 0,
// Page `head` element.
head: document.getElementsByTagName('head')[0],
// Libraries that are imported are stored here.
libraries: {},
libraries_count: 0,
libraries_props: {},
// Spawned libraries, so they don't run over themselves.
spawned: [],
// Libraries that failed to load.
failed: {},
failed_count: 0,
// Shims that are imported are stored here.
shims: {},
// Libraries successfully loaded.
success: {},
success_count: 0
};
// Bootstrap our library.
fallback.bootstrap = function() {
var index;
for (index in fallback.utilities) {
if (fallback.utilities[index]) {
fallback.utility(fallback.utilities[index]);
}
}
};
// Utility functions to check against variables.
fallback.utilities = ['Array', 'Function', 'Object', 'String'];
// Setup individual utility function.
fallback.utility = function(type) {
fallback['is_' + type.toString().toLowerCase()] = function(variable) {
/*eslint-disable*/
return typeof variable !== 'undefined' && Object.prototype.toString.call(variable) == '[object ' + type + ']';
/*eslint-enable*/
};
};
// Check if our variable is a function, if not make it one.
fallback.callback = function(variable) {
var me = this;
if (!me.is_function(variable)) {
variable = function() {};
}
return variable;
};
// indexOf isn't supported on arrays in older versions of IE!
fallback.index_of = function(object, value) {
var index;
for (index in object) {
if (object[index] === value) {
return index;
}
}
return -1;
};
// Check if our variable is defined.
fallback.is_defined = function(variable) {
try {
/*eslint-disable*/
if (eval('window.' + variable)) {
return true;
}
/*eslint-enable*/
} catch (exception) {
return false;
}
return false;
};
fallback.add_sri_attributes = function(element, props) {
var me = this;
if (me.is_object(props)) {
if (props.integrity) {
element.setAttribute('integrity', props.integrity);
}
if (props.crossorigin) {
element.setAttribute('crossorigin', props.crossorigin);
}
}
};
// Import and cleanse libraries from user input.
fallback.importer = function(libraries, options) {
var me = this;
var current, index;
var library, library_props, urls;
var cleansed_shims, shim, shims;
var shim_libraries, shim_libraries_cleansed;
// Cleanse the libraries.
var cleansed_libraries = {};
for (library in libraries) {
// URL list for each library.
library_props = libraries[library];
// If `urls` is undefined, null or an empty string, skip, it's invalid.
if (!library_props) {
continue;
}
// If `urls` is a string, convert it to any array.
if (me.is_string(library_props)) {
urls = [library_props];
} else if (me.is_object(library_props)) {
urls = library_props.urls;
} else {
// Assume an array.
urls = library_props;
}
// If `urls` is not an array, skip, it's invalid.
if (!me.is_array(urls)) {
continue;
}
// Check to see if the library already exists, if it does, merge the new URLs.
current = [];
if (me.is_array(me.libraries[library])) {
current = me.libraries[library];
}
cleansed_libraries[library] = urls;
me.libraries[library] = current.concat(urls);
me.libraries_props[library] = library_props;
}
// Cleanse the shims.
cleansed_shims = {};
if (me.is_object(options)) {
// Shim aliases, `deps` and dependencies`.
if (!me.is_object(options.shim)) {
if (me.is_object(options.deps)) {
options.shim = options.deps;
} else if (me.is_object(options.dependencies)) {
options.shim = options.dependencies;
}
}
if (me.is_object(options.shim)) {
shims = options.shim;
for (shim in shims) {
shim_libraries = shims[shim];
// If `shim` doesn't exist in libraries, skip, it's invalid.
if (!me.libraries[shim]) {
continue;
}
// If `shim_libraries` is undefined, null or an empty string, skip, it's invalid.
if (!shim_libraries) {
continue;
}
// If `shim_libraries` is a string, convert it to any array.
if (me.is_string(shim_libraries)) {
shim_libraries = [shim_libraries];
}
// If `shim_libraries` is not an array, skip, it's invalid.
if (!me.is_array(shim_libraries)) {
continue;
}
// Check to make sure the libraries exist otherwise remove them.
shim_libraries_cleansed = [];
for (index in shim_libraries) {
library = shim_libraries[index];
// Make sure the library actually exists and that it's not itself.
if (me.libraries[library] && library !== shim) {
shim_libraries_cleansed.push(library);
}
}
// Check to see if the shim already exists, if it does, merge the new shims.
current = [];
if (me.is_array(me.shims[shim])) {
current = me.shims[shim];
}
cleansed_shims[shim] = shim_libraries_cleansed;
me.shims[shim] = current.concat(shim_libraries_cleansed);
}
}
}
return {
libraries: cleansed_libraries,
shims: cleansed_shims
};
};
// CSS check if selector exists.
fallback.css = {};
fallback.css.check = function(selector) {
var me = fallback;
if (!document.styleSheets) {
return false;
}
var index, stylesheet, found;
for (index in document.styleSheets) {
stylesheet = document.styleSheets[index];
if (stylesheet === 0) {
continue;
}
// Issues with CORS at times, don't let the script bomb.
try {
if (stylesheet.rules) {
found = me.css.scan(stylesheet.rules, selector);
if (found) {
return found;
}
}
if (stylesheet.cssRules) {
found = me.css.scan(stylesheet.cssRules, selector);
if (found) {
return found;
}
}
} catch (e) {
continue;
}
}
return false;
};
fallback.css.scan = function(ruleset, selector) {
var index, rule;
for (index in ruleset) {
rule = ruleset[index];
if (rule.selectorText === selector) {
return true;
}
}
return false;
};
// Spawn an instance of the library.
fallback.load = function(libraries, options, callback) {
var me = this;
var imported, library, urls;
// If `libraries` is not a object, die out.
if (!me.is_object(libraries)) {
return false;
}
// If `options` is a function, then it needs to become the callback.
if (me.is_function(options)) {
callback = options;
options = {};
}
// If `options` is not an object, convert it.
if (!me.is_object(options)) {
options = {};
}
// Import libraries.
imported = me.importer(libraries, options);
// Spawn library instances from user input.
for (library in imported.libraries) {
urls = imported.libraries[library];
if (!me.shims[library]) {
me.spawn(library, urls);
}
}
// Fork the callback over to the `ready` function.
if (me.is_function(options['callback'])) {
me.ready([], options['callback']);
}
if (me.is_function(callback)) {
me.ready([], callback);
}
};
// Callback array of objects.
fallback.ready = function(libraries, callback) {
var me = this;
var index, library;
if (me.is_function(libraries)) {
callback = libraries;
libraries = [];
} else {
if (!me.is_array(libraries) || me.is_string(libraries)) {
libraries = [libraries];
}
for (index in libraries) {
library = libraries[index];
if (me.libraries[library] && !me.shims[library]) {
me.spawn(library, me.libraries[library]);
}
}
}
me.callbacks.push({
callback: me.callback(callback),
libraries: libraries
});
return me.ready_invocation();
};
// Invoke any `ready` callbacks.
fallback.ready_invocation = function() {
var me = this, index, count, library, wipe, payload, processed = [], callbacks = [];
for (index in me.callbacks) {
// If callback is not an object, skip and remove it;
payload = me.callbacks[index];
if (!me.is_object(payload) || !me.is_array(payload.libraries) || !me.is_function(payload.callback)) {
continue;
}
wipe = false;
if (payload.libraries.length > 0) {
count = 0;
for (library in me.success) {
if (me.index_of(payload.libraries, library) >= 0) {
count++;
}
}
if (count === payload.libraries.length) {
wipe = true;
}
} else if (me.libraries_count === me.success_count + me.failed_count) {
wipe = true;
}
if (wipe) {
callbacks.push(payload.callback);
} else {
processed.push(me.callbacks[index]);
}
}
me.callbacks = processed;
// We need to process the callbacks here that way they can run in parallel as well in nested callbacks and not get caught in a endless loop.
for (index in callbacks) {
callbacks[index](me.success, me.failed);
}
};
// Invoke any `shim` dependencies.
fallback.shim_invocation = function() {
var me = this;
var count, index, shim, shimming;
for (shim in me.shims) {
shimming = me.shims[shim];
// If there are no shims, or the if the shim is already loaded, skip it.
if (!shimming || me.success[shim]) {
continue;
}
// Reset our counter back to 0.
count = 0;
// Iterate through shim dependencies and find out of all dependencies for shim were loaded.
for (index in shimming) {
if (me.success[shimming[index]]) {
count++;
}
}
// If all dependencies were loaded, spawn the shim.
if (count === shimming.length) {
me.spawn(shim, me.libraries[shim]);
// Remove the shim from shim list that way it doesn't try to load it again.
delete me.shims[shim];
}
}
};
// Initialize the spawning of a library.
fallback.spawn = function(library, urls) {
var me = this;
// Library is already attempting to be loaded.
if (me.index_of(me.spawned, library) !== -1) {
return false;
}
me.libraries_count++;
me.spawned.push(library);
return me.spawn.instance(library, urls);
};
// Spawn a url from the library.
fallback.spawn.instance = function(library, urls) {
var me = fallback;
var element;
var type = 'js';
var payload = {
loaded: false,
library: library,
library_props: me.libraries_props[library],
spawned: true,
url: urls.shift(),
urls: urls
};
if (payload.url.indexOf('.css') > -1) {
type = 'css';
// CSS selector already exists, do not attempt to spawn library.
if (me.css.check(library)) {
payload.spawned = false;
return me.spawn.success(payload);
}
element = document.createElement('link');
element.crossorigin = true;
element.rel = 'stylesheet';
element.href = payload.url;
} else {
// JavaScript variable already exists, do not attempt to spawn library
if (me.is_defined(library)) {
payload.spawned = false;
return me.spawn.success(payload);
}
element = document.createElement('script');
element.src = payload.url;
}
me.add_sri_attributes(element, payload.library_props);
element.onload = function() {
// Checks for JavaScript library.
if (type === 'js' && !me.is_defined(library)) {
return me.spawn.failed(payload);
}
// Needed for IE11 especially. `onload` is fired even when there's a 404 for `link` elements.
if (type !== 'js' && !me.css.check(library) && Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) {
return me.spawn.failed(payload);
}
return me.spawn.success(payload);
};
element.onreadystatechange = function() {
if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
this.onreadystatechange = null;
if (type === 'js' && !me.is_defined(library)) {
return me.spawn.failed(payload);
}
return me.spawn.success(payload);
}
};
element.onerror = function() {
return me.spawn.failed(payload);
};
return me.head.appendChild(element);
};
// Spawn failure callback.
fallback.spawn.failed = function(payload) {
var me = fallback;
payload.spawned = false;
if (!me.failed[payload.library]) {
me.failed[payload.library] = [];
}
me.failed[payload.library].push(payload.url);
// All URLs for the library have exhausted.
if (!payload.urls.length) {
me.failed_count++;
return me.ready_invocation();
}
// Attempt to spawn another URL.
return me.spawn.instance(payload.library, payload.urls);
};
// Spawn success callback.
fallback.spawn.success = function(payload) {
var me = fallback;
// Mark the library and url as successful.
if (!payload.loaded) {
payload.loaded = true;
me.success[payload.library] = payload.url;
me.success_count++;
}
// Invoke any `shim` dependencies.
me.shim_invocation();
// Invoke any `ready` callbacks.
return me.ready_invocation();
};
fallback.bootstrap();
window.fallback = window.fbk = fallback;
}(window, document));