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  * Or a way to group chunks of interface together.
10  * 
11  * Mypart.xyx = new Roo.XComponent({
12
13     parent : 'Mypart.xyz', // empty == document.element.!!
14     order : '001',
15     name : 'xxxx'
16     region : 'xxxx'
17     disabled : function() {} 
18      
19     tree : function() { // return an tree of xtype declared components
20         var MODULE = this;
21         return 
22         {
23             xtype : 'NestedLayoutPanel',
24             // technicall
25         }
26      ]
27  *})
28  *
29  *
30  * It can be used to build a big heiracy, with parent etc.
31  * or you can just use this to render a single compoent to a dom element
32  * MYPART.render(Roo.Element | String(id) | dom_element )
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 {String} region
110      * Region to render component to (defaults to center)
111      */
112     region : 'center',
113     
114     /**
115      * @cfg {Array} items
116      * A single item array - the first element is the root of the tree..
117      * It's done this way to stay compatible with the Xtype system...
118      */
119     items : false,
120     
121     
122      /**
123      * render
124      * render element to dom or tree
125      * @param {Roo.Element|String|DomElement} optional render to if parent is not set.
126      */
127     
128     render : function(el)
129     {
130         
131         if (!this.parent) {
132             
133             el = el ? Roo.get(el) : false;
134             
135             
136             // it's a top level one..
137             this.parent =  {
138                 el : new Ext.BorderLayout(el || document.body, {
139                 
140                      center: {
141                          titlebar: false,
142                          autoScroll:false,
143                          closeOnTab: true,
144                          tabPosition: 'top',
145                           //resizeTabs: true,
146                          alwaysShowTabs: el ? false :  true,
147                          minTabWidth: 140
148                      }
149                  })
150             }
151         }
152             
153         var tree = this.tree();
154         tree.region = tree.region || this.region;
155         this.el = this.parent.el.addxtype(tree);
156         this.fireEvent('built', this);
157         
158         this.panel = this.el;
159         this.layout = this.panel.layout;    
160          
161     }
162     
163      
164      
165     
166 });
167
168 Roo.apply(Roo.XComponent, {
169     
170     /**
171      * @property  buildCompleted
172      * True when the builder has completed building the interface.
173      * @type Boolean
174      */
175     buildCompleted : false,
176      
177     /**
178      * @property  topModule
179      * the upper most module - uses document.element as it's constructor.
180      * @type Object
181      */
182      
183     topModule  : false,
184       
185     /**
186      * @property  modules
187      * array of modules to be created by registration system.
188      * @type Roo.XComponent
189      */
190     
191     modules : [],
192       
193     
194     /**
195      * Register components to be built later.
196      *
197      * This solves the following issues
198      * - Building is not done on page load, but after an authentication process has occured.
199      * - Interface elements are registered on page load
200      * - Parent Interface elements may not be loaded before child, so this handles that..
201      * 
202      *
203      * example:
204      * 
205      * MyApp.register({
206           order : '000001',
207           module : 'Pman.Tab.projectMgr',
208           region : 'center',
209           parent : 'Pman.layout',
210           disabled : false,  // or use a function..
211         })
212      
213      * * @param {Object} details about module
214      */
215     register : function(obj) {
216         this.modules.push(obj);
217          
218     },
219     /**
220      * convert a string to an object..
221      * 
222      */
223     
224     toObject : function(str)
225     {
226         if (!str || typeof(str) == 'object') {
227             return str;
228         }
229         var ar = str.split('.');
230         var rt, o;
231         rt = ar.shift();
232             /** eval:var:o */
233         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
234         if (o === false) {
235             throw "Module not found : " + str;
236         }
237         Roo.each(ar, function(e) {
238             if (typeof(o[e]) == 'undefined') {
239                 throw "Module not found : " + str;
240             }
241             o = o[e];
242         });
243         return o;
244         
245     },
246     
247     
248     /**
249      * move modules into their correct place in the tree..
250      * 
251      */
252     preBuild : function ()
253     {
254         
255         Roo.each(this.modules , function (obj)
256         {
257             obj.parent = this.toObject(obj.parent);
258             
259             if (!obj.parent) {
260                 this.topModule = obj;
261                 return;
262             }
263             
264             if (!obj.parent.modules) {
265                 obj.parent.modules = new Roo.util.MixedCollection(false, 
266                     function(o) { return o.order + '' }
267                 );
268             }
269             
270             obj.parent.modules.add(obj);
271         }, this);
272     },
273     
274      /**
275      * make a list of modules to build.
276      * @return {Array} list of modules. 
277      */ 
278     
279     buildOrder : function()
280     {
281         var _this = this;
282         var cmp = function(a,b) {   
283             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
284         };
285         
286         if (!this.topModule || !this.topModule.modules) {
287             throw "No top level modules to build";
288         }
289        
290         // make a flat list in order of modules to build.
291         var mods = [ this.topModule ];
292         
293         
294         // add modules to their parents..
295         var addMod = function(m) {
296            // Roo.debug && Roo.log(m.modKey);
297             
298             mods.push(m);
299             if (m.modules) {
300                 m.modules.keySort('ASC',  cmp );
301                 m.modules.each(addMod);
302             }
303             // not sure if this is used any more..
304             if (m.finalize) {
305                 m.finalize.name = m.name + " (clean up) ";
306                 mods.push(m.finalize);
307             }
308             
309         }
310         this.topModule.modules.keySort('ASC',  cmp );
311         this.topModule.modules.each(addMod);
312         return mods;
313     },
314     
315      /**
316      * Build the registered modules.
317      * @param {Object} parent element.
318      * @param {Function} optional method to call after module has been added.
319      * 
320      */ 
321    
322     build : function() 
323     {
324         
325         this.preBuild();
326         var mods = this.buildOrder();
327       
328         //this.allmods = mods;
329         //Roo.debug && Roo.log(mods);
330         //return;
331         if (!mods.length) { // should not happen
332             throw "NO modules!!!";
333         }
334         
335         
336         
337         // flash it up as modal - so we store the mask!?
338         Roo.MessageBox.show({ title: 'loading' });
339         Roo.MessageBox.show({
340            title: "Please wait...",
341            msg: "Building Interface...",
342            width:450,
343            progress:true,
344            closable:false,
345            modal: false
346           
347         });
348         var total = mods.length;
349         
350         var _this = this;
351         var progressRun = function() {
352             if (!mods.length) {
353                 Roo.debug && Roo.log('hide?');
354                 Roo.MessageBox.hide();
355                 _this.topModule.fireEvent('buildcomplete', _this.topModule);
356                 return flase;    
357             }
358             
359             var m = mods.shift();
360             
361             
362             Roo.debug && Roo.log(m);
363             // not sure if this is supported any more.. - modules that are are just function
364             if (typeof(m) == 'function') { 
365                 m.call(this);
366                 return progressRun.defer(10, _this);
367             } 
368             
369             
370             
371             Roo.MessageBox.updateProgress(
372                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
373                     " of " + total + 
374                     (m.name ? (' - ' + m.name) : '')
375                     );
376             
377          
378             // is the module disabled?
379             var disabled = (typeof(m.disabled) == 'function') ?
380                 m.disabled.call(m.module.disabled) : m.disabled;    
381             
382             
383             if (disabled) {
384                 return progressRun(); // we do not update the display!
385             }
386             
387             // now build 
388             
389             m.render();
390             // it's 10 on top level, and 1 on others??? why...
391             return progressRun.defer(10, _this);
392              
393         }
394         progressRun.defer(1, _this);
395      
396         
397         
398     }
399     
400      
401    
402     
403     
404 });
405