Last active
August 29, 2015 13:56
-
-
Save Cortys/9058807 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Modular.js 1.0 | |
* 2013 - Clemens Damke | |
* License: WTFPL - Have fun! Credits only if you want to. | |
*/ | |
var Modular=(function(){var h=window,f=function(i,k,l){var j=this;h[i]=this;j.init=l;f.loadModules(k,function(m){Object.getPrototypeOf(j).run.call(j,m)},i)};f.loaded={};f.modules={};f.path="";f.requiredBy={};f.isOk=function(i){return i%2==1};f.status=function(i){return this.status.codes[i]};f.status.codes={1:"Module already loaded",2:"Module could not be loaded",3:"Module loaded",4:"Module loaded but invalidly defined",5:"Circular requirement loop had to be interrupted at first link-back (may cause problems in one modules 'init()')"};f.setModulePath=function(i){if(i!==undefined){this.path=i}return this};f.setModules=function(i){if(i!==undefined){this.modules=i}return this};f.addModules=function(i){for(var j in i){this.modules[j]=i[j]}return this};var a=function(l,j){for(var k=0;k<l.length;k++){l[k].apply(f,j)}},e=function(i,j){if(!f.requiredBy[i]){f.requiredBy[i]={}}f.requiredBy[i][j]=true},d=function(k,l,i){if(!i){i={}}i[k]=true;if(f.requiredBy[k]){if(f.requiredBy[k][l]){return true}for(var j in f.requiredBy[k]){if(!i[j]&&d(j,l,i)){return true}}}return false},b=document.getElementsByTagName("head")[0]||document.head||document,g=function(k){if(k){try{var j=document.createElement("script");j.type="text/javascript";j.text=k;b.appendChild(j);b.removeChild(j)}catch(i){return i}}},c=function(j,l,i){var k=new XMLHttpRequest();if(!k){i()}k.open("GET",j,true);k.overrideMimeType("text/javascript");k.send();k.ontimeout=i;k.onreadystatechange=function(){if(this.readyState==4){if(this.status==200||this.status==304){var m;if(m=g(this.responseText)){i(m)}else{l()}}else{i()}}}};f.loadModule=function(l,m,j){var k=this;if(j){e(l,j)}if(!k.loaded[l]){k.loaded[l]=typeof m==="function"?[m]:[];if(!k.modules[l]){k.modules[l]=l+".js"}var i=function(n,o){a(k.loaded[l],[n,l]);k.loaded[l]=o};c(k.path+k.modules[l],function(){if(h[l]){k.prototype.onDone.call(h[l],function(){i(3,true)})}else{i(4,true)}},function(n){if(n){i(4,true);var o=new Error();o.message="In module '"+l+"': "+n.message;if(document.baseURI){o.sourceURL=document.baseURI+k.path+k.modules[l]}o.name=n.name;throw o}else{i(2,false)}})}else{if(m){if(k.loaded[l]===true||(j&&h[l]&&d(j,l))){setTimeout(function(){m(k.loaded[l]===true?1:5,l)},0)}else{k.loaded[l].push(m)}}}return k};f.loadModules=function(m,o,l){var k=0,j={};if(m&&m.length){for(var n=0;n<m.length;n++){this.loadModule(m[n],function(i,p){k++;j[p]=i;if(k>=m.length&&o){o(j)}},l)}}else{if(o){setTimeout(function(){o(j)},0)}}return this};f.prototype={run:function(i){if(this.init){this.init(i)}if(this.doneCallback){this.doneCallback()}if(!this.hasOwnProperty("run")){this.run=null}},onDone:function(i){this.doneCallback=i}};return f})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment