Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function require (lib, obj, libs_obj) {
- var lib_is_list = typeof lib === 'object';
- if (typeof obj === 'function') obj = { callback: obj, count: lib_is_list ? lib.length : 1}
- if (lib_is_list) { // this is list of libs
- for (var i in lib) require(lib[i], obj, libs_obj);
- return;
- }
- var lib = libs_obj[lib];
- if (!lib.callbacks) lib.callbacks = [];
- if (lib.check()) { if (obj.callback) obj.callback(); return; }
- lib.callbacks.push(obj);
- if (lib.pending) { return; }
- lib.pending = true;
- function ready() {
- function script_downloaded() {
- lib.pending = false;
- var obj;
- while (obj = lib.callbacks.pop()) {
- obj.count--; if (obj.count == 0) obj.callback();
- }
- }
- download_script(lib.link, script_downloaded);
- }
- function download_script(src, callback) {
- var script = document.createElement('script');
- script.type = 'text/javascript';
- script.async = 'async';
- script.src = src;
- // Based on jQuery jsonp trick
- if (callback) {
- script.onload = script.onreadystatechange = function() {
- if (!script.readyState || /loaded|complete/.test(script.readyState)) {
- script.onload = script.onreadystatechange = null;
- callback();
- }
- };
- }
- document.getElementsByTagName('head')[0].appendChild(script);
- }
- var deps_count = lib.deps ? lib.deps.length : 0;
- if (deps_count < 1) { ready(); return; }
- var new_obj = { callback: ready, count: deps_count };
- require(lib.deps, new_obj, libs_obj);
- }
Add Comment
Please, Sign In to add comment