'use strict'; // Ox.js Developer Version /* Usage: Ox.load(callback) Ox.load(module, callback) Ox.load(module, options, callback) Ox.load({module: options, module: options}, callback) */ window.Ox = { load: function() { var args = arguments, callback = args[args.length - 1], path = getPath(); loadJSON(function(data) { loadScripts(data, function() { 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?' + Ox.random(1000000), true); req.onreadystatechange = function() { if (req.readyState == 4) { if (req.status == 200) { callback(JSON.parse(req.responseText)); } else { // ... } } }; req.send(); } function loadScripts(scripts, callback) { loadScriptsOrdered(scripts[0], function() { loadScriptsUnordered(scripts[1], function() { loadScriptsUnordered(scripts[2], callback); }); }); } function loadScriptsOrdered(scripts, callback) { loadScript(scripts.shift(), function() { if (scripts.length) { loadScriptsOrdered(scripts, callback); } else { callback(); } }); } function loadScriptsUnordered(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; element.onload = callback; element.src = path + script + '?' + parseInt(Math.random() * 1000000); element.type = 'text/javascript'; head.appendChild(element); } } };