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