update Ox.js loader
This commit is contained in:
parent
e88f622449
commit
367a8227c9
1 changed files with 69 additions and 86 deletions
155
source/Ox.js
155
source/Ox.js
|
@ -1,105 +1,88 @@
|
|||
'use strict';
|
||||
|
||||
// Ox.js Developer Version
|
||||
(function(global) {
|
||||
|
||||
/*
|
||||
Usage:
|
||||
Ox.load(callback)
|
||||
Ox.load(module, callback)
|
||||
Ox.load(module, options, callback)
|
||||
Ox.load({module: options, module: options}, callback)
|
||||
*/
|
||||
global.Ox = {
|
||||
|
||||
window.Ox = {
|
||||
load: function() {
|
||||
|
||||
load: function() {
|
||||
var args = arguments,
|
||||
callback = args[args.length - 1],
|
||||
path = getPath(),
|
||||
time = +new Date();
|
||||
|
||||
var args = arguments,
|
||||
callback = args[args.length - 1],
|
||||
path = getPath();
|
||||
|
||||
loadJSON(function(data) {
|
||||
loadScripts(data.files, function() {
|
||||
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));
|
||||
loadJSON(function(data) {
|
||||
loadScriptsSerial(data.files, function() {
|
||||
Ox.VERSION = data.version;
|
||||
if (args.length == 1) {
|
||||
console.log(document.body);
|
||||
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$/, '');
|
||||
}
|
||||
}
|
||||
};
|
||||
req.send();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
function loadScripts(scripts, callback) {
|
||||
loadScriptsParallel(scripts[0], function() {
|
||||
loadScriptsParallel(scripts[1], function() {
|
||||
loadScriptsParallel(scripts[2], callback);
|
||||
function loadJSON(callback) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('GET', path + 'Ox/json/Ox.json?' + time, true);
|
||||
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 loadScriptsSerial(scripts, callback) {
|
||||
loadScript(scripts.shift(), function() {
|
||||
if (scripts.length) {
|
||||
loadScriptsSerial(scripts, callback);
|
||||
} else {
|
||||
callback();
|
||||
function loadScriptsParallel(scripts, callback) {
|
||||
var counter = 0, length = scripts.length;
|
||||
while (scripts.length) {
|
||||
loadScript(scripts.shift(), function() {
|
||||
++counter == length && 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) {
|
||||
var element = document.createElement('script'),
|
||||
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
|
||||
if (/MSIE/.test(navigator.userAgent)) {
|
||||
// fixme: find a way to check if css/js have loaded in msie
|
||||
setTimeout(callback, 2500);
|
||||
} else {
|
||||
element.onload = callback;
|
||||
function loadScript(script, callback) {
|
||||
var element = document.createElement('script'),
|
||||
head = document.head
|
||||
|| document.getElementsByTagName('head')[0]
|
||||
|| document.documentElement;
|
||||
if (/MSIE/.test(navigator.userAgent)) {
|
||||
// FIXME: find a way to check if css/js have loaded in MSIE
|
||||
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));
|
||||
|
|
Loading…
Reference in a new issue