Roo/XComponent.js
[roojs1] / Roo / XComponent.js
1 /*
2  * Original code for Roojs - LGPL
3  * <script type="text/javascript">
4  */
5  
6 /**
7  * @class Roo.XComponent
8  * A delayed Element creator...
9  * 
10  * Mypart.xyx = new Roo.XComponent({
11
12     parent : 'Mypart.xyz', // empty == document.element.!!
13     order : '001',
14     name : 'xxxx'
15     region : 'xxxx'
16     disabled : function() {} 
17      
18     tree : function() { // return an tree of xtype declared components
19         var MODULE = this;
20         return 
21         {
22             xtype : 'NestedLayoutPanel',
23             // technicall
24         }
25      ]
26  *})
27  * 
28  * 
29  * @extends Roo.util.Observable
30  */
31 Roo.XComponent = function(cfg) {
32     Roo.apply(this, cfg);
33     this.addEvents({ 
34         /**
35              * @event built
36              * Fires when this the componnt is built
37              * @param {Roo.XComponent} c the component
38              */
39         'built' : true,
40         /**
41              * @event buildcomplete
42              * Fires on the top level element when all elements have been built
43              * @param {Roo.XComponent} c the top level component.
44          */
45         'buildcomplete' : true,
46         
47     });
48     
49     Roo.XComponent.register(this);
50     this.modules = false;
51     this.el = false; // where the layout goes..
52     
53     
54 }
55 Roo.extend(Roo.XComponent, Roo.util.Observable, {
56     /**
57      * @property el
58      * The created element (with Roo.factory())
59      * @type {Roo.Layout}
60      */
61     el  : false,
62     
63     /**
64      * @property el
65      * for BC  - use el in new code
66      * @type {Roo.Layout}
67      */
68     panel : false,
69     
70     /**
71      * @property layout
72      * for BC  - use el in new code
73      * @type {Roo.Layout}
74      */
75     layout : false,
76     
77      /**
78      * @cfg {Function|boolean} disabled
79      * If this module is disabled by some rule, return true from the funtion
80      */
81     disabled : false,
82     
83     /**
84      * @cfg {String} parent 
85      * Name of parent element which it get xtype added to..
86      */
87     parent: false,
88     
89     /**
90      * @cfg {String} order
91      * Used to set the order in which elements are created (usefull for multiple tabs)
92      */
93     
94     order : false,
95     /**
96      * @cfg {String} name
97      * String to display while loading.
98      */
99     name : false,
100     /**
101      * @cfg {Array} items
102      * A single item array - the first element is the root of the tree..
103      * It's done this way to stay compatible with the Xtype system...
104      */
105     items : false,
106      
107      
108     
109 });
110
111 Roo.apply(Roo.XComponent, {
112     
113     /**
114      * @property  buildCompleted
115      * True when the builder has completed building the interface.
116      * @type Boolean
117      */
118     buildCompleted : false,
119      
120     /**
121      * @property  topModule
122      * the upper most module - uses document.element as it's constructor.
123      * @type Object
124      */
125      
126     topModule  : false,
127       
128     /**
129      * @property  modules
130      * array of modules to be created by registration system.
131      * @type Roo.XComponent
132      */
133     
134     modules : [],
135       
136     
137     /**
138      * Register components to be built later.
139      * @param {Object} details about module
140      *
141      * This solves the following issues
142      * - Building is not done on page load, but after an authentication process has occured.
143      * - Interface elements are registered on page load
144      * - Parent Interface elements may not be loaded before child, so this handles that..
145      * 
146      *
147      * example:
148      * 
149      * MyApp.register({
150           order : '000001',
151           module : 'Pman.Tab.projectMgr',
152           region : 'center',
153           parent : 'Pman.layout',
154           disabled : false,  // or use a function..
155         })
156      * 
157      */
158     register : function(obj) {
159         this.modules.push(obj);
160          
161     },
162     /**
163      * convert a string to an object..
164      * 
165      */
166     
167     toObject : function(str)
168     {
169         if (!str || typeof(str) == 'object') {
170             return str;
171         }
172         var ar = str.split('.');
173         var rt, o;
174         rt = ar.shift();
175             /** eval:var:o */
176         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
177         if (o === false) {
178             throw "Module not found : " + str;
179         }
180         Roo.each(ar, function(e) {
181             if (typeof(o[e]) == 'undefined') {
182                 throw "Module not found : " + str;
183             }
184             o = o[e];
185         });
186         return o;
187         
188     },
189     
190     
191     /**
192      * move modules into their correct place in the tree..
193      * 
194      */
195     preBuild : function ()
196     {
197         
198         Roo.each(this.modules , function (obj)
199         {
200             obj.parent = this.toObject(obj.parent);
201             
202             if (!obj.parent) {
203                 this.topModule = obj;
204                 return;
205             }
206             
207             if (!obj.parent.modules) {
208                 obj.parent.modules = new Roo.util.MixedCollection(false, 
209                     function(o) { return o.order + '' }
210                 );
211             }
212             
213             obj.parent.modules.add(obj);
214         }, this);
215     },
216     
217      /**
218      * make a list of modules to build.
219      * @return {Array} list of modules. 
220      */ 
221     
222     buildOrder : function()
223     {
224         var _this = this;
225         var cmp = function(a,b) {   
226             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
227         };
228         
229         if (!this.topModule || !this.topModule.modules) {
230             throw "No top level modules to build";
231         }
232        
233         // make a flat list in order of modules to build.
234         var mods = [ this.topModule ];
235         
236         
237         // add modules to their parents..
238         var addMod = function(m) {
239            // console.log(m.modKey);
240             
241             mods.push(m);
242             if (m.modules) {
243                 m.modules.keySort('ASC',  cmp );
244                 m.modules.each(addMod);
245             }
246             // not sure if this is used any more..
247             if (m.finalize) {
248                 m.finalize.name = m.name + " (clean up) ";
249                 mods.push(m.finalize);
250             }
251             
252         }
253         this.topModule.modules.keySort('ASC',  cmp );
254         this.topModule.modules.each(addMod);
255         return mods;
256     },
257     
258      /**
259      * Build the registered modules.
260      * @param {Object} parent element.
261      * @param {Function} optional method to call after module has been added.
262      * 
263      */ 
264    
265     build : function() 
266     {
267         
268         this.preBuild();
269         var mods = this.buildOrder();
270       
271         //this.allmods = mods;
272         //console.log(mods);
273         //return;
274         if (!mods.length) { // should not happen
275             throw "NO modules!!!";
276         }
277         
278         
279         
280         // flash it up as modal - so we store the mask!?
281         Roo.MessageBox.show({ title: 'loading' });
282         Roo.MessageBox.show({
283            title: "Please wait...",
284            msg: "Building Interface...",
285            width:450,
286            progress:true,
287            closable:false,
288            modal: false
289           
290         });
291         var total = mods.length;
292         
293         var _this = this;
294         var progressRun = function() {
295             if (!mods.length) {
296                 console.log('hide?');
297                 Roo.MessageBox.hide();
298                 _this.topModule.fireEvent('buildcomplete', _this.topModule);
299                 return;    
300             }
301             
302             var m = mods.shift();
303             console.log(m);
304             if (typeof(m) == 'function') { // not sure if this is supported any more..
305                 m.call(this);
306                 return progressRun.defer(10, _this);
307             } 
308             
309             Roo.MessageBox.updateProgress(
310                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
311                     " of " + total + 
312                     (m.name ? (' - ' + m.name) : '')
313                     );
314             
315          
316             
317             var disabled = (typeof(m.disabled) == 'function') ?
318                 m.disabled.call(m.module.disabled) : m.disabled;    
319             
320             
321             if (disabled) {
322                 return progressRun(); // we do not update the display!
323             }
324             
325             if (!m.parent) {
326                 // it's a top level one..
327                 var layoutbase = new Ext.BorderLayout(document.body, {
328                
329                     center: {
330                          titlebar: false,
331                          autoScroll:false,
332                          closeOnTab: true,
333                          tabPosition: 'top',
334                          //resizeTabs: true,
335                          alwaysShowTabs: true,
336                          minTabWidth: 140
337                     }
338                 });
339                 var tree = m.tree();
340                 tree.region = 'center';
341                 m.el = layoutbase.addxtype(tree);
342                 m.panel = m.el;
343                 m.layout = m.panel.layout;    
344                 return progressRun.defer(10, _this);
345             }
346             
347             var tree = m.tree();
348             tree.region = tree.region || m.region;
349             m.el = m.parent.el.addxtype(tree);
350             m.fireEvent('built', m);
351             m.panel = m.el;
352             m.layout = m.panel.layout;    
353             progressRun.defer(10, _this); 
354             
355         }
356         progressRun.defer(1, _this);
357      
358         
359         
360     }
361      
362    
363     
364     
365 });
366