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