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         el = el || false;
132         
133         if (!el && typeof(m.parent) == 'string' && m.parent[0] == '#') {
134             // if parent is a '#.....' string, then let's use that..
135             this.parent = false;
136             el = Roo.get(m.substr(1));
137         }
138         if (!this.parent) {
139             
140             el = el ? Roo.get(el) : false;
141             
142             // it's a top level one..
143             this.parent =  {
144                 el : new Ext.BorderLayout(el || document.body, {
145                 
146                      center: {
147                          titlebar: false,
148                          autoScroll:false,
149                          closeOnTab: true,
150                          tabPosition: 'top',
151                           //resizeTabs: true,
152                          alwaysShowTabs: el ? false :  true,
153                          hideTabs: el ? true :  false,
154                          minTabWidth: 140
155                      }
156                  })
157             }
158         }
159             
160         var tree = this.tree();
161         tree.region = tree.region || this.region;
162         this.el = this.parent.el.addxtype(tree);
163         this.fireEvent('built', this);
164         
165         this.panel = this.el;
166         this.layout = this.panel.layout;    
167          
168     }
169     
170      
171     
172 });
173
174 Roo.apply(Roo.XComponent, {
175     
176     /**
177      * @property  buildCompleted
178      * True when the builder has completed building the interface.
179      * @type Boolean
180      */
181     buildCompleted : false,
182      
183     /**
184      * @property  topModule
185      * the upper most module - uses document.element as it's constructor.
186      * @type Object
187      */
188      
189     topModule  : false,
190       
191     /**
192      * @property  modules
193      * array of modules to be created by registration system.
194      * @type {Array} of Roo.XComponent
195      */
196     
197     modules : [],
198     /**
199      * @property  elmodules
200      * array of modules to be created by which use #ID 
201      * @type {Array} of Roo.XComponent
202      */
203      
204     elmodules : [],
205
206     
207     /**
208      * Register components to be built later.
209      *
210      * This solves the following issues
211      * - Building is not done on page load, but after an authentication process has occured.
212      * - Interface elements are registered on page load
213      * - Parent Interface elements may not be loaded before child, so this handles that..
214      * 
215      *
216      * example:
217      * 
218      * MyApp.register({
219           order : '000001',
220           module : 'Pman.Tab.projectMgr',
221           region : 'center',
222           parent : 'Pman.layout',
223           disabled : false,  // or use a function..
224         })
225      
226      * * @param {Object} details about module
227      */
228     register : function(obj) {
229         this.modules.push(obj);
230          
231     },
232     /**
233      * convert a string to an object..
234      * eg. 'AAA.BBB' -> finds AAA.BBB
235
236      */
237     
238     toObject : function(str)
239     {
240         if (!str || typeof(str) == 'object') {
241             return str;
242         }
243         if (str[0]=='#') {
244             return str;
245         }
246
247         var ar = str.split('.');
248         var rt, o;
249         rt = ar.shift();
250             /** eval:var:o */
251         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
252         if (o === false) {
253             throw "Module not found : " + str;
254         }
255         Roo.each(ar, function(e) {
256             if (typeof(o[e]) == 'undefined') {
257                 throw "Module not found : " + str;
258             }
259             o = o[e];
260         });
261         return o;
262         
263     },
264     
265     
266     /**
267      * move modules into their correct place in the tree..
268      * 
269      */
270     preBuild : function ()
271     {
272         
273         Roo.each(this.modules , function (obj)
274         {
275             obj.parent = this.toObject(obj.parent);
276             
277             if (!obj.parent) {
278                 this.topModule = obj;
279                 return;
280             }
281             if (typeof(obj.parent) == 'string') {
282                 this.elmodules.push(obj);
283                 return;
284             }
285             
286             if (!obj.parent.modules) {
287                 obj.parent.modules = new Roo.util.MixedCollection(false, 
288                     function(o) { return o.order + '' }
289                 );
290             }
291             
292             obj.parent.modules.add(obj);
293         }, this);
294     },
295     
296      /**
297      * make a list of modules to build.
298      * @return {Array} list of modules. 
299      */ 
300     
301     buildOrder : function()
302     {
303         var _this = this;
304         var cmp = function(a,b) {   
305             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
306         };
307         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
308             throw "No top level modules to build";
309         }
310         
311         // make a flat list in order of modules to build.
312         var mods = this.topModule ? [ this.topModule ] : [];
313         Roo.each(this.elmodules,function(e) { mods.push(e) });
314
315         
316         // add modules to their parents..
317         var addMod = function(m) {
318            // Roo.debug && Roo.log(m.modKey);
319             
320             mods.push(m);
321             if (m.modules) {
322                 m.modules.keySort('ASC',  cmp );
323                 m.modules.each(addMod);
324             }
325             // not sure if this is used any more..
326             if (m.finalize) {
327                 m.finalize.name = m.name + " (clean up) ";
328                 mods.push(m.finalize);
329             }
330             
331         }
332         if (this.topModule) { 
333             this.topModule.modules.keySort('ASC',  cmp );
334             this.topModule.modules.each(addMod);
335         }
336         return mods;
337     },
338     
339      /**
340      * Build the registered modules.
341      * @param {Object} parent element.
342      * @param {Function} optional method to call after module has been added.
343      * 
344      */ 
345    
346     build : function() 
347     {
348         
349         this.preBuild();
350         var mods = this.buildOrder();
351       
352         //this.allmods = mods;
353         //Roo.debug && Roo.log(mods);
354         //return;
355         if (!mods.length) { // should not happen
356             throw "NO modules!!!";
357         }
358         
359         
360         
361         // flash it up as modal - so we store the mask!?
362         Roo.MessageBox.show({ title: 'loading' });
363         Roo.MessageBox.show({
364            title: "Please wait...",
365            msg: "Building Interface...",
366            width:450,
367            progress:true,
368            closable:false,
369            modal: false
370           
371         });
372         var total = mods.length;
373         
374         var _this = this;
375         var progressRun = function() {
376             if (!mods.length) {
377                 Roo.debug && Roo.log('hide?');
378                 Roo.MessageBox.hide();
379                 if (_this.topModule) { 
380                     _this.topModule.fireEvent('buildcomplete', _this.topModule);
381                 }
382                 // THE END...
383                 return false;   
384             }
385             
386             var m = mods.shift();
387             
388             
389             Roo.debug && Roo.log(m);
390             // not sure if this is supported any more.. - modules that are are just function
391             if (typeof(m) == 'function') { 
392                 m.call(this);
393                 return progressRun.defer(10, _this);
394             } 
395             
396             
397             
398             Roo.MessageBox.updateProgress(
399                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
400                     " of " + total + 
401                     (m.name ? (' - ' + m.name) : '')
402                     );
403             
404          
405             // is the module disabled?
406             var disabled = (typeof(m.disabled) == 'function') ?
407                 m.disabled.call(m.module.disabled) : m.disabled;    
408             
409             
410             if (disabled) {
411                 return progressRun(); // we do not update the display!
412             }
413             
414             // now build 
415             
416             m.render();
417             // it's 10 on top level, and 1 on others??? why...
418             return progressRun.defer(10, _this);
419              
420         }
421         progressRun.defer(1, _this);
422      
423         
424         
425     }
426     
427      
428    
429     
430     
431 });
432