OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@1020 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_TrackBack / trunk / trackback / js / rico / rico.js
1 /**
2   *
3   *  Copyright 2005 Sabre Airline Solutions
4   *
5   *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6   *  file except in compliance with the License. You may obtain a copy of the License at
7   *
8   *         http://www.apache.org/licenses/LICENSE-2.0
9   *
10   *  Unless required by applicable law or agreed to in writing, software distributed under the
11   *  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12   *  either express or implied. See the License for the specific language governing permissions
13   *  and limitations under the License.
14   **/
15
16
17 // This module does NOT depend on prototype.js
18
19 var Rico = {
20   Version: '2.0 beta2',
21   loadRequested: 1,
22   loadComplete: 2,
23   init : function() {
24     try {  // fix IE background image flicker (credit: www.mister-pixel.com)
25       document.execCommand("BackgroundImageCache", false, true);
26     } catch(err) {}
27     this.preloadMsgs='';
28     var elements = document.getElementsByTagName('script');
29     this.baseHref= location.protocol + "//" + location.host;
30     this.loadedFiles={};
31     this.loadQueue=[];    
32     this.windowIsLoaded=false;
33     this.onLoadCallbacks=[];
34     for (var i=0; i<elements.length; i++) {
35       if (!elements[i].src) continue;
36       var src = elements[i].src;
37       var slashIdx = src.lastIndexOf('/');
38       var path = src.substring(0, slashIdx+1);
39       var filename = src.substring(slashIdx+1);
40       this.loadedFiles[filename]=this.loadComplete;
41       if (filename == 'rico.js') {
42         this.jsDir = path;
43         this.cssDir= path+'css/';
44         this.imgDir= path+'images/';
45         this.htmDir= path;
46         this.xslDir= path;
47       }
48     }
49     if (typeof Prototype=='undefined')
50       this.include('prototype.js');
51     this.include('ricoCommon.js');
52     var func=function() { Rico.windowLoaded(); };
53     if (window.addEventListener)
54       window.addEventListener('load', func, false);
55     else if (window.attachEvent)
56       window.attachEvent('onload', func);
57     this.onLoad(function() { Rico.writeDebugMsg('Pre-load messages:\n'+Rico.preloadMsgs); });
58   },
59   
60   // Array entries can reference a javascript file or css stylesheet
61   // A dependency on another module can be indicated with a plus-sign prefix: '+DependsOnModule'
62   moduleDependencies : {
63     Accordion  : ['ricoBehaviors.js','ricoEffects.js','ricoComponents.js'],
64     Color      : ['ricoColor.js'],
65     Corner     : ['ricoStyles.js'],
66     DragAndDrop: ['ricoDragDrop.js'],
67     Effect     : ['ricoEffects.js'],
68     Calendar   : ['ricoCalendar.js', 'ricoCalendar.css'],
69     Tree       : ['ricoTree.js', 'ricoTree.css'],
70     ColorPicker: ['ricoColorPicker.js', 'ricoStyles.js'],
71     SimpleGrid : ['ricoCommon.js', 'ricoGridCommon.js', 'ricoGrid.css', 'ricoSimpleGrid.js'],
72     LiveGrid   : ['ricoCommon.js', 'ricoGridCommon.js', 'ricoGrid.css', 'ricoBehaviors.js', 'ricoLiveGrid.js'],
73     CustomMenu : ['ricoMenu.js', 'ricoMenu.css'],
74     LiveGridMenu : ['+CustomMenu', 'ricoLiveGridMenu.js'],
75     LiveGridAjax : ['+LiveGrid', 'ricoLiveGridAjax.js'],
76     LiveGridForms: ['+LiveGridAjax', '+LiveGridMenu', '+Accordion', '+Corner', 'ricoLiveGridForms.js', 'ricoLiveGridForms.css'],
77     SpreadSheet  : ['+SimpleGrid', 'ricoSheet.js']
78   },
79   
80   // not reliable when used with XSLT
81   loadModule : function(name) {
82     var dep=this.moduleDependencies[name];
83     if (!dep) return;
84     for (var i=0; i<dep.length; i++)
85       if (dep[i].substring(0,1)=='+')
86         this.loadModule(dep[i].slice(1));
87       else
88         this.include(dep[i]);
89   },
90   
91   // not reliable when used with XSLT
92   include : function(filename) {
93     if (this.loadedFiles[filename]) return;
94     this.addPreloadMsg('include: '+filename);
95     var ext = filename.substr(filename.lastIndexOf('.')+1);
96     switch (ext.toLowerCase()) {
97       case 'js':
98         this.loadQueue.push(filename);
99         this.loadedFiles[filename]=this.loadRequested;
100         this.checkLoadQueue();
101         return;
102       case 'css':
103         var el = document.createElement('link');
104         el.type = 'text/css';
105         el.rel = 'stylesheet'
106         el.href = this.cssDir+filename;
107         this.loadedFiles[filename]=this.loadComplete;
108         document.getElementsByTagName('head')[0].appendChild(el);
109         return;
110     }
111   },
112   
113   checkLoadQueue: function() {
114     if (this.loadQueue.length==0) return;
115     if (this.inProcess) return;  // seems to only be required by IE, but applied to all browsers just to be safe
116     this.addScriptToDOM(this.loadQueue.shift());
117   },
118   
119   addScriptToDOM: function(filename) {
120     this.addPreloadMsg('addScriptToDOM: '+filename);
121     var js = document.createElement('script');
122     js.type = 'text/javascript';
123     js.src = this.jsDir+filename;
124     this.loadedFiles[filename]=this.loadRequested;
125     this.inProcess=filename;
126     var head=document.getElementsByTagName('head')[0];
127     if (filename.substring(0,4)=='rico') {
128       head.appendChild(js);
129     } else if(/WebKit|Khtml/i.test(navigator.userAgent)) {
130       head.appendChild(js);
131       this.includeLoaded(filename);
132     } else {
133       js.onload = js.onreadystatechange = function() {
134         if (js.readyState && js.readyState != 'loaded' && js.readyState != 'complete') return;
135         js.onreadystatechange = js.onload = null;
136         Rico.includeLoaded(filename);
137       };
138       head.appendChild(js);
139     }
140   },
141   
142   // called after a script file has finished loading
143   includeLoaded: function(filename) {
144     this.addPreloadMsg('loaded: '+filename);
145     this.loadedFiles[filename]=this.loadComplete;
146     if (filename==this.inProcess) {
147       this.inProcess=null;
148       this.checkLoadQueue();
149       this.checkIfComplete();
150     }
151   },
152
153   // called by the document onload event
154   windowLoaded: function() {
155     this.windowIsLoaded=true;
156     this.checkIfComplete();
157   },
158   
159   checkIfComplete: function() {
160     var waitingFor=this.windowIsLoaded ? '' : 'window';
161     for(var filename in  this.loadedFiles) {
162       if (this.loadedFiles[filename]==this.loadRequested)
163         waitingFor+=' '+filename;
164     }
165     //window.status='waitingFor: '+waitingFor;
166     this.addPreloadMsg('waitingFor: '+waitingFor);
167     if (waitingFor.length==0) {
168       this.addPreloadMsg('Processing callbacks');
169       while (this.onLoadCallbacks.length > 0) {
170         var callback=this.onLoadCallbacks.pop();
171         if (callback) callback();
172       }
173     }
174   },
175   
176   onLoad: function(callback) {
177     this.onLoadCallbacks.push(callback);
178     this.checkIfComplete();
179   },
180
181   isKonqueror : navigator.userAgent.toLowerCase().indexOf("konqueror") >= 0,
182
183   // logging funtions
184    
185   startTime : new Date(),
186
187   timeStamp: function() {
188     var stamp = new Date();
189     return (stamp.getTime()-this.startTime.getTime())+": ";
190   },
191   
192   setDebugArea: function(id, forceit) {
193     if (!this.debugArea || forceit) {
194       var newarea=document.getElementById(id);
195       if (!newarea) return;
196       this.debugArea=newarea;
197       newarea.value='';
198     }
199   },
200
201   addPreloadMsg: function(msg) {
202     this.preloadMsgs+=Rico.timeStamp()+msg+"\n";
203   },
204
205   writeDebugMsg: function(msg, resetFlag) {
206     if (this.debugArea) {
207       if (resetFlag) this.debugArea.value='';
208       this.debugArea.value+=this.timeStamp()+msg+"\n";
209     }
210   }
211
212 }
213
214 Rico.init();