1/*************************************/ 2/* ResourceManager */ 3/* use for, script, style, ... */ 4/*************************************/ 5 6var ResourceManager = (function () 7{ 8 // Static 9 var WorkerInterval = null; 10 var WorkerWorking = false; 11 var WorkerCallBack = function (CallBacks, Resources) 12 { 13 14 var mycallbacks = CallBacks.slice(); 15 16 // loop throw all callback 17 for (var i = 0; i <= mycallbacks.length - 1; i++) 18 { 19 for (var j = 0; j < mycallbacks[i]._WaitingFor.length; j++) 20 { 21 var resource = Utility.FindByName(mycallbacks[i]._WaitingFor[j], Resources) 22 if (resource && resource._Loaded) 23 { 24 mycallbacks[i]._WaitingFor.splice(j, 1); 25 j--; 26 } 27 } 28 29 if (mycallbacks[i]._WaitingFor.length === 0) 30 { 31 try 32 { 33 mycallbacks[i].Execute(); 34 } 35 catch (ex) 36 { 37 var error = (ex && typeof ex === "string") ? ex : "no error msg possible!"; 38 if (ex && ex.stack) error = ex.stack; 39 if (console && console.error) console.error(error); 40 } 41 CallBacks.remove(mycallbacks[i]); 42 } 43 } 44 45 // if all resources are loaded, stop the worker. 46 var NbLoaded = 0; 47 for (var i = 0; i <= Resources.length - 1; i++) { if (Resources[i]._Loaded) NbLoaded += 1; } 48 if (NbLoaded === Resources.length) 49 { 50 window.clearInterval(WorkerInterval); 51 WorkerWorking = false; 52 } 53 }; 54 55 // Class Resource 56 var Resource = (function () 57 { 58 var Class = function (Name, Loaded) 59 { 60 if (!Name) throw "Name cannot be null."; 61 62 this._Name = Name; 63 this._Loaded = Loaded; 64 }; 65 66 return Class; 67 })(); 68 69 // Class CallBack 70 var Callback = function (Handler, Context, WaitingFor) 71 { 72 if (!Handler) throw "Handler cannot be null."; 73 if (!Context) throw "Context cannot be null."; 74 75 this._Handler = Handler; 76 this._Context = Context || window; 77 this._WaitingFor = []; 78 if (WaitingFor) this._WaitingFor = WaitingFor; 79 }; 80 81 // Public Method(s) 82 (function (Methods) 83 { 84 Methods.Execute = function () 85 { 86 this._Handler.apply(this._Context, arguments); 87 }; 88 89 })(Callback.prototype); 90 91 92 // Constructor Resource manager 93 var Class = function () 94 { 95 this._Resources = []; 96 this._CallBack = []; 97 }; 98 99 // Public Method(s) 100 (function (Methods) 101 { 102 Methods.LoadFromUrl = function (Name, Url, Type) 103 { 104 if (!Utility.FindByName(Name, this._Resources)) // check if script is not already there! 105 { 106 var me = this; 107 var resource = new Resource(Name, false); 108 this._Resources.push(resource); 109 var s = null; 110 111 switch (Type) 112 { 113 case "Script": 114 115 s = document.createElement("script"); 116 s.type = "text/javascript"; 117 break; 118 119 case "Style": 120 121 s = document.createElement("link"); 122 s.rel = "stylesheet"; 123 s.type = "text/css"; 124 break; 125 126 default: 127 128 throw "LoadFromUrl : invalid type '" + type + "'!"; 129 break; 130 } 131 132 s.onload = s.onreadystatechange = function () 133 { 134 if ((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) 135 { 136 // Completed! 137 // on met loaded pour ce script et on execute 138 // les fonctions callback si lieu. 139 if (!resource._Loaded) 140 { 141 resource._Loaded = true; 142 143 // loop throw all callbacks and remove ref resource (waitingfor) 144 for (var i = 0; i <= me._CallBack.length - 1; i++) 145 { 146 if (me._CallBack[i]._WaitingFor.contains(resource._Name)) me._CallBack[i]._WaitingFor.remove(resource._Name); 147 } 148 } 149 } 150 }; 151 152 // Append Script, Style to header 153 if (Type == "Script") s.src = Url; 154 else s.href = Url; 155 $('head')[0].appendChild(s); 156 } 157 }; 158 159 Methods.Load = function (Script, elements, data) 160 { 161 // append script to header! 162 if (Script.startsWith("<script") && Script.endsWith("</script>")) throw "Load : remove scripttag from your script"; 163 eval(Script); 164 }; 165 166 Methods.RegisterCallBack = function (Name, Handler, Context) 167 { 168 var Names = []; 169 try 170 { 171 Names = $.parseJSON(Name); 172 } 173 catch (ex) { Names = [Name] } 174 175 var WaitingFor = []; 176 var NbLoaded = 0; 177 for (var i = 0; i <= Names.length - 1; i++) 178 { 179 WaitingFor.push(Names[i]); 180 var resource = Utility.FindByName(Names[i], this._Resources); 181 if (resource && resource._Loaded) NbLoaded += 1;
182 } 183 if (NbLoaded === Names.length) WaitingFor = []; 184 185 // push new callback and waitingfor list. 186 var callback = new Callback(Handler, Context, WaitingFor); 187 this._CallBack.push(callback); 188 189 var CallBackParam = this._CallBack; 190 var RessourceParam = this._Resources; 191 192 // start the worker 193 if (!WorkerWorking) 194 { 195 WorkerWorking = true; 196 WorkerInterval = window.setInterval( 197 function () { 198 WorkerCallBack(CallBackParam, RessourceParam); 199 }, 100); 200 } 201 }; 202 203 })(Class.prototype); 204 205 206 return new Class(); 207})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.