MIDI.js/js/DOMLoader.script.js

131 lines
3.3 KiB
JavaScript
Raw Normal View History

2012-02-16 05:46:03 +01:00
/*
DOMLoader : 0.2 : mudcu.be
---------------------------
DOMLoader.script.add({
strictOrder: true,
srcs: [
{
src: "../js/jszip/jszip.js",
verify: "JSZip",
callback: function() {
console.log(1)
}
},
{
src: "../inc/downloadify/js/swfobject.js",
verify: "swfobject",
callback: function() {
console.log(2)
}
}
],
callback: function() {
console.log(3)
}
});
*/
if (typeof(DOMLoader) === "undefined") DOMLoader = {};
DOMLoader.script = function() {
this.loaded = {};
this.loading = {};
return this;
};
DOMLoader.script.prototype.add = function(config) {
var that = this;
var srcs = config.srcs;
if (typeof(srcs) === "undefined") {
srcs = [{
src: config.src,
verify: config.verify
}];
}
/// adding the elements to the head
var doc = document.getElementsByTagName("head")[0];
///
var testElement = function(element, test) {
if (that.loaded[element.src]) return;
if (test && !eval(test)) 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();
};
///
var batchTest = [];
var addElement = function(element) {
if (/([\w\d.\[\]])$/.test(element.verify)) { // check whether its a variable reference
element.test = "(typeof(" + element.verify + ") !== \"undefined\")";
batchTest.push(element.test);
}
var script = document.createElement("script");
script.onreadystatechange = function() {
if (this.readyState !== "loaded" && this.readyState !== "complete") return;
testElement(element);
};
script.onload = function() {
testElement(element);
};
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
var onLoad = function(element) {
if (element) {
testElement(element, element.test);
} else {
for (var n = 0; n < srcs.length; n ++) {
testElement(srcs[n], srcs[n].test);
}
}
if (!config.strictOrder && eval(batchTest.join(" && "))) { // 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) {
var ID = -1;
var getNext = function() {
ID ++;
if (!srcs[ID]) { // all elements are loaded
if (config.callback) config.callback();
} else { // loading new script
var element = srcs[ID];
var 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 (var ID = 0; ID < srcs.length; ID ++) {
if (that.loaded[srcs[ID].src]) return;
addElement(srcs[ID]);
}
onLoad();
}
};
DOMLoader.script = (new DOMLoader.script());