lint.
[Plinn.git] / Products / Plinn / skins / ajax_scripts / plinn_script_loader.js
1 // (c) BenoƮt PIN 2006-2015
2 // http://plinn.org
3 // Licence GPL
4 //
5 //
6
7 var ScriptRegistry;
8 var globalScriptRegistry;
9
10 (function() {
11
12 function ScriptRegistry() {
13 this.loadedScripts = {};
14 this.pendingScripts = [];
15 this.HEAD = document.getElementsByTagName('head')[0];
16 this.isLoading = false;
17 }
18
19 ScriptRegistry.prototype.loadScript = function(scriptOb) {
20 var scriptUrl;
21 if (typeof(scriptOb) == 'string')
22 scriptUrl = scriptOb;
23 else
24 scriptUrl = scriptOb.getAttribute('src');
25
26 if (scriptUrl) {
27 if (!this.loadedScripts[scriptUrl])
28 this.pendingScripts.push(['url', scriptUrl]);
29 }
30 else {
31 this.pendingScripts.push(['code', scriptOb]);
32 }
33 if(!this.isLoading && this.pendingScripts.length)
34 this._loadNextScript();
35 };
36
37 ScriptRegistry.prototype._loadNextScript = function() {
38 var firstScript = this.pendingScripts[0];
39
40 switch (firstScript[0]) {
41 case 'url':
42 var script = document.createElement( "script" );
43 script.type = "text/javascript";
44 script.src = firstScript[1];
45 this.HEAD.appendChild(script);
46 this.loadedScripts[script.src] = true;
47 this.isLoading = true;
48 var thisRegistry = this;
49 if (browser.isIE)
50 script.onreadystatechange = function(){
51 if (script.readyState == 'complete' || script.readyState == 'loaded')
52 thisRegistry._removeScriptAfterLoad();
53 };
54 else
55 script.onload = function(){ thisRegistry._removeScriptAfterLoad(); };
56 break;
57 case 'code' :
58 try {
59 /* jshint ignore:start */
60 eval(firstScript[1].text);
61 /* jshint ignore:end */
62 }
63 catch(e) {
64 if (window.console) {
65 console.group('Embedded script error');
66 console.error(e);
67 console.info(firstScript[1]);
68 console.groupEnd();
69 }
70 }
71 this._removeScriptAfterLoad();
72 break;
73 }
74 };
75
76 ScriptRegistry.prototype._removeScriptAfterLoad = function() {
77 this.pendingScripts.shift();
78 if(this.pendingScripts.length)
79 this._loadNextScript();
80 else
81 this.isLoading = false;
82 };
83
84 globalScriptRegistry = new ScriptRegistry();
85
86 }());