Roo/Document.js
[roojs1] / Roo / Document.js
1 /*
2  * Original code for Roojs - LGPL
3  * <script type="text/javascript">
4  */
5  
6 /**
7  * @class Roo.Documents
8  * @extends Roo.data.Observable
9  * 
10  * Document and interface builder class..
11  * 
12  * MyApp = new Roo.Document({
13  *     loadingIndicator : 'loading',
14  *     listeners :  Roo.Login.onLoad();
15  * });
16  * 
17  * MyApp.on('beforeload', function() {
18  *      MyApp.register()
19  
20  * 
21  * 
22  * 
23  */
24 Roo.Document = function(cfg) {
25      
26     this.addEvents({ 
27         'ready' : true,
28         'beforeload' : true, // fired after page ready, before module building.
29         'load' : true, // fired after module building
30         'authrefreshed' : true // fire on auth updated?? - should be on Login?!?!?
31         
32     });
33     this.modules = [];
34     Roo.util.Observable.call(this,cfg);
35     var _this = this;
36     Roo.onReady(function() {
37         _this.fireEvent('ready');
38     },null,false);
39     
40     this.on('ready', onReady, this);
41     this.on('load', onLoad, this);  
42     
43     
44 }
45 Roo.extend(Roo.Document, Roo.util.Observable, {
46     /**
47      * @property  buildCompleted
48      * True when the builder has completed building the interface.
49      * @type Boolean
50      */
51     buildCompleted : false,
52      /**
53      * @property  loadingIndicator
54      * ID of loading indictor element.
55      * @type String
56      */
57     loadingIndicator : false,
58     /**
59      * @property  modules
60      * array of modules to be created by registration system.
61      * @type Array
62      */
63     
64     modules : false,
65       
66     // protected - on Ready handler.
67     onReady :   function()
68     {
69         // kludge to fix firebug debugger
70         if (typeof(console) == 'undefined') {
71             console = { log : function() {  } };
72         }
73          
74         // init cookies..
75         Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
76             
77         // link errors...
78          
79             
80     },
81      
82     
83     
84     /**
85      * Register components to be built later.
86      * @param {Object} details about module
87      *
88      * This solves the following issues
89      * - Building is not done on page load, but after an authentication process has occured.
90      * - Interface elements are registered on page load
91      * - Parent Interface elements may not be loaded before child, so this handles that..
92      * 
93      *
94      * example:
95      * 
96      * MyApp.register({
97           order : '000001',
98           module : 'Pman.Tab.projectMgr',
99           region : 'center',
100           parent : 'Pman.layout',
101           disabled : false,  // or use a function..
102         })
103      * 
104      */
105     register : function(obj) {
106         this.modules.push(obj);
107         
108         
109     },
110     
111     
112     
113     /**
114      * move modules into their correct place in the tree..
115      * 
116      */
117     preBuild : function ()
118     {
119         var modules = this.modules;
120         this.modules = false;
121         
122         function toObject(str)
123         {
124             if (typeof(str) == 'object') {
125                 return str;
126             }
127             var ar = str.split('.');
128             var rt, o;
129             rt = ar.unshift();
130                 /** eval:var:o */
131             eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
132             if (o === false) {
133                 throw "Module not found : " + str;
134             }
135             Roo.each(ar, function(e) {
136                 if (typeof(o[e]) == 'undefined') {
137                     throw "Module not found : " + str;
138                 }
139                 o = o[e];
140             });
141             return o;
142             
143         }
144         
145         Roo.each(modules , function (obj)
146         {
147             
148             obj.parent = toObject(obj.parent);
149              
150             if (!obj.parent.modules) {
151                 obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.order + '' });
152             }
153             
154             obj.parent.modules.add(obj);
155         }
156     }
157     
158     
159      /**
160      * Build the registered modules.
161      * @param {Object} parent element.
162      * @param {Function} optional method to call after module has been added.
163      * 
164      */ 
165    
166     build : function(parent, onComplete) 
167     {
168         
169         var _this = this;
170         var cmp = function(a,b) {   
171             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
172         };
173         
174         if (!parent.modules) {
175             return;
176         }
177        
178         // make a flat list in order of modules to build.
179         var mods = [];
180         
181         
182         // add modules to their parents..
183         var addMod = function(m) {
184            // console.log(m.modKey);
185             
186             mods.push(m);
187             if (m.module.modules) {
188                 m.module.modules.keySort('ASC',  cmp );
189                 m.module.modules.each(addMod);
190             }
191             if (m.finalize) {
192                 m.finalize.name = m.name + " (clean up) ";
193                 mods.push(m.finalize);
194             }
195             
196         }
197         parent.modules.keySort('ASC',  cmp );
198         parent.modules.each(addMod);
199         //this.allmods = mods;
200         //console.log(mods);
201         //return;
202         if (!mods.length) {
203             if (onComplete) {
204                 onComplete.call(this);
205             }
206             return;
207         }
208         // flash it up as modal - so we store the mask!?
209         Roo.MessageBox.show({ title: 'loading' });
210         Roo.MessageBox.show({
211            title: "Please wait...",
212            msg: "Building Interface...",
213            width:450,
214            progress:true,
215            closable:false,
216            modal: false
217           
218         });
219         var n = -1;
220         var _this = this;
221         var progressRun = function() {
222             n++;
223             if (n >= mods.length) {
224                 if (onComplete) {
225                     onComplete.call(this);
226                 }
227                 return;
228             }
229             
230             var m = mods[n];
231             
232             
233             Roo.MessageBox.updateProgress(
234                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
235                     " of " + mods.length + 
236                     (m.name ? (' - ' + m.name) : '')
237                     );
238             
239             if (typeof(m) == 'function') {
240                 m.call(this);
241                 progressRun.defer(10, _this);    
242                 return;
243             } 
244             var disabled = (typeof(m.module.disabled) == 'function') ?
245                 m.module.disabled.call(m.module.disabled) : m.module.disabled;
246                 
247             }
248             
249             if (m.parent.layout && !disabled) {
250                 m.module.add(m.parent.layout, m.region);    
251             }
252                  
253              
254             
255         }
256         progressRun.defer(1, _this);
257      
258         
259         
260     },
261     
262 },
263    
264     
265     
266 });
267