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         })
102      * 
103      */
104     register : function(obj) {
105         this.modules.push(obj);
106         
107         
108     },
109     
110     
111     
112     /**
113      * move modules into their correct place in the tree..
114      * 
115      */
116     preBuild : function ()
117     {
118         var modules = this.modules;
119         this.modules = false;
120         
121         function toObject(str)
122         {
123             if (typeof(str) == 'object') {
124                 return str;
125             }
126             var ar = str.split('.');
127             var rt, o;
128             rt = ar.unshift();
129                 /** eval:var:o */
130             eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
131             if (o === false) {
132                 throw "Module not found : " + str;
133             }
134             Roo.each(ar, function(e) {
135                 if (typeof(o[e]) == 'undefined') {
136                     throw "Module not found : " + str;
137                 }
138                 o = o[e];
139             });
140             return o;
141             
142         }
143         
144         Roo.each(modules , function (obj)
145         {
146             
147             obj.parent = toObject(obj.parent);
148              
149             if (!obj.parent.modules) {
150                 obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.order + '' });
151             }
152             
153             obj.parent.modules.add(obj);
154         }
155     }
156     
157     
158      /**
159      * Build the registered modules.
160      * @param {Object} parent element.
161      * @param {Function} optional method to call after module has been added.
162      * 
163      */ 
164    
165     build : function(parent, onComplete) 
166     {
167         
168         var _this = this;
169         var cmp = function(a,b) {   
170             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
171         };
172         
173         if (!parent.modules) {
174             return;
175         }
176        
177         // make a flat list in order of modules to build.
178         var mods = [];
179         
180         
181         // add modules to their parents..
182         var addMod = function(m) {
183            // console.log(m.modKey);
184             
185             mods.push(m);
186             if (m.module.modules) {
187                 m.module.modules.keySort('ASC',  cmp );
188                 m.module.modules.each(addMod);
189             }
190             if (m.finalize) {
191                 m.finalize.name = m.name + " (clean up) ";
192                 mods.push(m.finalize);
193             }
194             
195         }
196         parent.modules.keySort('ASC',  cmp );
197         parent.modules.each(addMod);
198         //this.allmods = mods;
199         //console.log(mods);
200         //return;
201         if (!mods.length) {
202             if (onComplete) {
203                 onComplete.call(this);
204             }
205             return;
206         }
207         // flash it up as modal - so we store the mask!?
208         Ext.MessageBox.show({ title: 'loading' });
209         Ext.MessageBox.show({
210            title: "Please wait...",
211            msg: "Building Interface...",
212            width:450,
213            progress:true,
214            closable:false,
215            modal: false
216           
217         });
218         var n = 0;
219         var progressRun = function() {
220             
221             var m = mods[n];
222             
223             
224             Ext.MessageBox.updateProgress(
225                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
226                     " of " + mods.length + 
227                     (m.name ? (' - ' + m.name) : '')
228                     );
229             
230             
231             
232             if (typeof(m) == 'function') {
233                 m();
234                 
235             } else {
236                 if (m.parent.layout && !m.module.disabled) {
237                     m.module.add(m.parent.layout, m.region);    
238                 }
239                 
240             }
241             
242             
243             n++;
244             if (n >= mods.length) {
245                 onComplete();  
246                 return;
247             }
248                 
249             
250             progressRun.defer(10, Pman);    
251         }
252         progressRun.defer(1, Pman);
253      
254         
255         
256     },
257     
258 },
259    
260     
261     
262 });
263