2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
// Ox.js Developer Version
|
2011-04-27 19:39:56 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*
|
|
|
|
Usage:
|
|
|
|
Ox.load(callback)
|
|
|
|
Ox.load(module, callback)
|
|
|
|
Ox.load(module, options, callback)
|
|
|
|
Ox.load({module: options, module: options}, callback)
|
|
|
|
*/
|
2011-04-27 19:39:56 +00:00
|
|
|
|
2011-11-05 16:46:53 +00:00
|
|
|
window.Ox = {
|
2011-04-27 19:39:56 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
load: function() {
|
2011-10-01 02:21:55 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
var args = arguments,
|
|
|
|
callback = args[args.length - 1],
|
|
|
|
path = getPath();
|
2011-04-27 19:39:56 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
loadJSON(function(data) {
|
|
|
|
loadScripts(data, function() {
|
|
|
|
if (args.length == 1) {
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
Ox.load.apply(null, args);
|
|
|
|
}
|
2011-04-27 19:39:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
function getPath() {
|
2011-10-23 14:26:37 +00:00
|
|
|
var i, path, scripts = document.getElementsByTagName('script');
|
2011-10-07 01:04:47 +00:00
|
|
|
for (i = 0; i < scripts.length; i++) {
|
|
|
|
if (/Ox\.js$/.test(scripts[i].src)) {
|
2011-10-23 14:26:37 +00:00
|
|
|
path = scripts[i].src.replace(/Ox\.js$/, '');
|
2011-04-27 19:39:56 +00:00
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
}
|
2011-10-23 14:26:37 +00:00
|
|
|
return path;
|
2011-04-27 19:39:56 +00:00
|
|
|
}
|
2011-05-06 23:30:32 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
function loadJSON(callback) {
|
|
|
|
var req = new XMLHttpRequest();
|
2012-04-18 09:01:42 +00:00
|
|
|
req.open(
|
|
|
|
'GET',
|
|
|
|
path + 'Ox/json/Ox.json?' + Math.floor(Math.random() * 1000000),
|
|
|
|
true
|
|
|
|
);
|
2011-10-07 01:04:47 +00:00
|
|
|
req.onreadystatechange = function() {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
|
|
|
callback(JSON.parse(req.responseText));
|
2011-05-07 15:56:29 +00:00
|
|
|
} else {
|
2011-10-07 01:04:47 +00:00
|
|
|
// ...
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
req.send();
|
2011-09-20 00:11:16 +00:00
|
|
|
}
|
2011-05-08 12:14:07 +00:00
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
function loadScripts(scripts, callback) {
|
2012-05-25 22:20:52 +00:00
|
|
|
loadScriptsParallel(scripts[0], function() {
|
|
|
|
loadScriptsParallel(scripts[1], function() {
|
|
|
|
loadScriptsParallel(scripts[2], callback);
|
2011-09-17 13:05:16 +00:00
|
|
|
});
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2011-09-17 13:05:16 +00:00
|
|
|
}
|
2011-05-07 15:56:29 +00:00
|
|
|
|
2012-05-25 22:20:52 +00:00
|
|
|
// fixme: unused
|
|
|
|
function loadScriptsSerial(scripts, callback) {
|
2011-10-07 01:04:47 +00:00
|
|
|
loadScript(scripts.shift(), function() {
|
|
|
|
if (scripts.length) {
|
2012-05-25 22:20:52 +00:00
|
|
|
loadScriptsSerial(scripts, callback);
|
2011-05-07 15:56:29 +00:00
|
|
|
} else {
|
2011-10-07 01:04:47 +00:00
|
|
|
callback();
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
2011-04-28 18:34:19 +00:00
|
|
|
|
2012-05-25 22:20:52 +00:00
|
|
|
function loadScriptsParallel(scripts, callback) {
|
2011-10-07 01:04:47 +00:00
|
|
|
var counter = 0, i, length = scripts.length;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
loadScript(scripts[i], function() {
|
|
|
|
++counter == length && callback();
|
2011-05-19 07:06:42 +00:00
|
|
|
});
|
|
|
|
}
|
2011-04-27 19:39:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
function loadScript(script, callback) {
|
2012-02-17 10:24:42 +00:00
|
|
|
var element = document.createElement('script'),
|
|
|
|
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
|
2012-05-25 16:28:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-10-08 17:19:18 +00:00
|
|
|
element.src = path + script + '?' + parseInt(Math.random() * 1000000);
|
2011-10-07 01:04:47 +00:00
|
|
|
element.type = 'text/javascript';
|
2012-02-17 10:24:42 +00:00
|
|
|
head.appendChild(element);
|
2011-04-27 19:39:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-07 15:56:29 +00:00
|
|
|
}
|
2011-04-27 19:39:56 +00:00
|
|
|
|
2012-02-17 10:24:42 +00:00
|
|
|
};
|