update Ox.js loader

This commit is contained in:
rolux 2012-06-20 18:30:06 +02:00
parent e88f622449
commit 367a8227c9

View file

@ -1,105 +1,88 @@
'use strict'; 'use strict';
// Ox.js Developer Version (function(global) {
/* global.Ox = {
Usage:
Ox.load(callback)
Ox.load(module, callback)
Ox.load(module, options, callback)
Ox.load({module: options, module: options}, callback)
*/
window.Ox = { load: function() {
load: function() { var args = arguments,
callback = args[args.length - 1],
path = getPath(),
time = +new Date();
var args = arguments, loadJSON(function(data) {
callback = args[args.length - 1], loadScriptsSerial(data.files, function() {
path = getPath(); Ox.VERSION = data.version;
if (args.length == 1) {
loadJSON(function(data) { console.log(document.body);
loadScripts(data.files, function() { callback()
Ox.VERSION = data.version;
if (args.length == 1) {
callback()
} else {
Ox.load.apply(null, args);
}
});
});
function getPath() {
var i, path, scripts = document.getElementsByTagName('script');
for (i = 0; i < scripts.length; i++) {
if (/Ox\.js$/.test(scripts[i].src)) {
path = scripts[i].src.replace(/Ox\.js$/, '');
}
}
return path;
}
function loadJSON(callback) {
var req = new XMLHttpRequest();
req.open(
'GET',
path + 'Ox/json/Ox.json?' + Math.floor(Math.random() * 1000000),
true
);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
callback(JSON.parse(req.responseText));
} else { } else {
// ... Ox.load.apply(null, args);
}
});
});
function getPath() {
var i, path, scripts = document.getElementsByTagName('script');
for (i = 0; i < scripts.length; i++) {
if (/Ox\.js$/.test(scripts[i].src)) {
path = scripts[i].src.replace(/Ox\.js$/, '');
} }
} }
}; return path;
req.send(); }
}
function loadScripts(scripts, callback) { function loadJSON(callback) {
loadScriptsParallel(scripts[0], function() { var request = new XMLHttpRequest();
loadScriptsParallel(scripts[1], function() { request.open('GET', path + 'Ox/json/Ox.json?' + time, true);
loadScriptsParallel(scripts[2], callback); request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
callback(JSON.parse(request.responseText));
}
}
};
request.send();
}
function loadScriptsSerial(scripts, callback) {
loadScriptsParallel(scripts.shift(), function() {
if (scripts.length) {
loadScriptsSerial(scripts, callback);
} else {
callback();
}
}); });
}); }
}
// fixme: unused function loadScriptsParallel(scripts, callback) {
function loadScriptsSerial(scripts, callback) { var counter = 0, length = scripts.length;
loadScript(scripts.shift(), function() { while (scripts.length) {
if (scripts.length) { loadScript(scripts.shift(), function() {
loadScriptsSerial(scripts, callback); ++counter == length && callback();
} else { });
callback();
} }
});
}
function loadScriptsParallel(scripts, callback) {
var counter = 0, i, length = scripts.length;
for (i = 0; i < length; i++) {
loadScript(scripts[i], function() {
++counter == length && callback();
});
} }
}
function loadScript(script, callback) { function loadScript(script, callback) {
var element = document.createElement('script'), var element = document.createElement('script'),
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement; head = document.head
if (/MSIE/.test(navigator.userAgent)) { || document.getElementsByTagName('head')[0]
// fixme: find a way to check if css/js have loaded in msie || document.documentElement;
setTimeout(callback, 2500); if (/MSIE/.test(navigator.userAgent)) {
} else { // FIXME: find a way to check if css/js have loaded in MSIE
element.onload = callback; setTimeout(callback, 2500);
} else {
element.onload = callback;
}
element.src = path + script + '?' + time;
element.type = 'text/javascript';
head.appendChild(element);
} }
element.src = path + script + '?' + Math.floor(Math.random() * 1000000);
element.type = 'text/javascript';
head.appendChild(element);
} }
} };
}; }(this));