oxjs/source/Ox.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2012-06-20 16:30:06 +00:00
(function(global) {
2011-04-27 19:39:56 +00:00
2012-06-20 16:30:06 +00:00
global.Ox = {
2011-04-27 19:39:56 +00:00
2012-06-20 16:30:06 +00:00
load: function() {
2011-04-27 19:39:56 +00:00
2012-06-20 16:30:06 +00:00
var args = arguments,
callback = args[args.length - 1],
path = getPath(),
time = +new Date();
2012-06-20 16:30:06 +00:00
loadJSON(function(data) {
loadScriptsSerial(data.files, function() {
Ox.VERSION = data.version;
Ox.load.apply(null, args);
2012-06-20 16:30:06 +00:00
});
2011-04-27 19:39:56 +00:00
});
2012-06-20 16:30:06 +00:00
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$/, '');
}
2011-04-27 19:39:56 +00:00
}
2012-06-20 16:30:06 +00:00
return path;
}
2011-05-06 23:30:32 +00:00
2012-06-20 16:30:06 +00:00
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));
}
}
2012-06-20 16:30:06 +00:00
};
request.send();
}
2012-06-20 16:30:06 +00:00
function loadScriptsSerial(scripts, callback) {
loadScriptsParallel(scripts.shift(), function() {
if (scripts.length) {
loadScriptsSerial(scripts, callback);
} else {
callback();
}
2011-09-17 13:05:16 +00:00
});
2012-06-20 16:30:06 +00:00
}
2012-06-20 16:30:06 +00:00
function loadScriptsParallel(scripts, callback) {
var counter = 0, length = scripts.length;
while (scripts.length) {
loadScript(scripts.shift(), function() {
++counter == length && callback();
});
}
}
2011-04-27 19:39:56 +00:00
2012-06-20 16:30:06 +00:00
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);
2012-05-25 16:28:05 +00:00
}
2012-06-20 16:30:06 +00:00
2011-04-27 19:39:56 +00:00
}
2012-06-20 16:30:06 +00:00
};
2011-04-27 19:39:56 +00:00
2012-06-20 16:30:06 +00:00
}(this));