Spaces:
Sleeping
Sleeping
File size: 3,871 Bytes
4cadbaf |
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 |
const Script = function () {
this.loaded = {};
this.loading = {};
return this;
};
Script.prototype.add = function (config) {
const that = this;
if (typeof (config) === "string")
config = { src: config };
let srcs = config.srcs;
if (typeof (srcs) === "undefined") {
srcs = [{
src: config.src,
verify: config.verify,
}];
}
/// adding the elements to the head
const doc = document.getElementsByTagName("head")[0];
///
const testElement = function (element, test) {
if (that.loaded[element.src]) return;
if (test && typeof (window[test]) === "undefined") return;
that.loaded[element.src] = true;
//
if (that.loading[element.src]) that.loading[element.src]();
delete that.loading[element.src];
//
if (element.callback) element.callback();
if (typeof (getNext) !== "undefined") getNext();
};
///
const batchTest = [];
const addElement = function (element) {
if (typeof (element) === "string") {
element = {
src: element,
verify: config.verify,
};
}
if (/([\w\d.])$/.test(element.verify)) { // check whether its a variable reference
element.test = element.verify;
if (typeof (element.test) === "object") {
for (const key in element.test)
batchTest.push(element.test[key]);
}
else
batchTest.push(element.test);
}
if (that.loaded[element.src]) return;
const script = document.createElement("script");
script.onreadystatechange = function () {
if (this.readyState !== "loaded" && this.readyState !== "complete") return;
testElement(element);
};
script.onload = function () {
testElement(element);
};
script.onerror = function () {
};
script.setAttribute("type", "text/javascript");
script.setAttribute("src", element.src);
doc.appendChild(script);
that.loading[element.src] = function () {};
};
/// checking to see whether everything loaded properly
const onLoad = function (element) {
if (element)
testElement(element, element.test);
else {
for (let n = 0; n < srcs.length; n++)
testElement(srcs[n], srcs[n].test);
}
let istrue = true;
for (let n = 0; n < batchTest.length; n++) {
let test = batchTest[n];
if (test && test.indexOf(".") !== -1) {
test = test.split(".");
const level0 = window[test[0]];
if (typeof (level0) === "undefined") continue;
if (test.length === 2) { //- this is a bit messy and could handle more cases
if (typeof (level0[test[1]]) === "undefined")
istrue = false;
}
else if (test.length === 3) {
if (typeof (level0[test[1]][test[2]]) === "undefined")
istrue = false;
}
}
else {
if (typeof (window[test]) === "undefined")
istrue = false;
}
}
if (!config.strictOrder && istrue) { // finished loading all the requested scripts
if (config.callback) config.callback();
}
else { // keep calling back the function
setTimeout(function () { //- should get slower over time?
onLoad(element);
}, 10);
}
};
/// loading methods; strict ordering or loose ordering
if (config.strictOrder) {
let ID = -1;
const getNext = function () {
ID++;
if (!srcs[ID]) { // all elements are loaded
if (config.callback) config.callback();
}
else { // loading new script
const element = srcs[ID];
const src = element.src;
if (that.loading[src]) { // already loading from another call (attach to event)
that.loading[src] = function () {
if (element.callback) element.callback();
getNext();
};
}
else if (!that.loaded[src]) { // create script element
addElement(element);
onLoad(element);
}
else { // it's already been successfully loaded
getNext();
}
}
};
getNext();
}
else { // loose ordering
for (let ID = 0; ID < srcs.length; ID++)
addElement(srcs[ID]);
onLoad();
}
};
const script = new Script();
export default script;
|