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