Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function require (lib, obj, libs_obj) {
  2. var lib_is_list = typeof lib === 'object';
  3. if (typeof obj === 'function') obj = { callback: obj, count: lib_is_list ? lib.length : 1}
  4. if (lib_is_list) { // this is list of libs
  5. for (var i in lib) require(lib[i], obj, libs_obj);
  6. return;
  7. }
  8. var lib = libs_obj[lib];
  9. if (!lib.callbacks) lib.callbacks = [];
  10. if (lib.check()) { if (obj.callback) obj.callback(); return; }
  11. lib.callbacks.push(obj);
  12. if (lib.pending) { return; }
  13. lib.pending = true;
  14.  
  15. function ready() {
  16. function script_downloaded() {
  17. lib.pending = false;
  18. var obj;
  19. while (obj = lib.callbacks.pop()) {
  20. obj.count--; if (obj.count == 0) obj.callback();
  21. }
  22. }
  23.  
  24. download_script(lib.link, script_downloaded);
  25. }
  26.  
  27. function download_script(src, callback) {
  28. var script = document.createElement('script');
  29. script.type = 'text/javascript';
  30. script.async = 'async';
  31. script.src = src;
  32.  
  33. // Based on jQuery jsonp trick
  34. if (callback) {
  35. script.onload = script.onreadystatechange = function() {
  36. if (!script.readyState || /loaded|complete/.test(script.readyState)) {
  37. script.onload = script.onreadystatechange = null;
  38. callback();
  39. }
  40. };
  41. }
  42. document.getElementsByTagName('head')[0].appendChild(script);
  43. }
  44.  
  45. var deps_count = lib.deps ? lib.deps.length : 0;
  46. if (deps_count < 1) { ready(); return; }
  47. var new_obj = { callback: ready, count: deps_count };
  48.  
  49. require(lib.deps, new_obj, libs_obj);
  50. }
Add Comment
Please, Sign In to add comment