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