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     });
50     this.region = this.region || 'center'; // default..
51     Roo.XComponent.register(this);
52     this.modules = false;
53     this.el = false; // where the layout goes..
54     
55     
56 }
57 Roo.extend(Roo.XComponent, Roo.util.Observable, {
58     /**
59      * @property el
60      * The created element (with Roo.factory())
61      * @type {Roo.Layout}
62      */
63     el  : false,
64     
65     /**
66      * @property el
67      * for BC  - use el in new code
68      * @type {Roo.Layout}
69      */
70     panel : false,
71     
72     /**
73      * @property layout
74      * for BC  - use el in new code
75      * @type {Roo.Layout}
76      */
77     layout : false,
78     
79      /**
80      * @cfg {Function|boolean} disabled
81      * If this module is disabled by some rule, return true from the funtion
82      */
83     disabled : false,
84     
85     /**
86      * @cfg {String} parent 
87      * Name of parent element which it get xtype added to..
88      */
89     parent: false,
90     
91     /**
92      * @cfg {String} order
93      * Used to set the order in which elements are created (usefull for multiple tabs)
94      */
95     
96     order : false,
97     /**
98      * @cfg {String} name
99      * String to display while loading.
100      */
101     name : false,
102     /**
103      * @cfg {String} region
104      * Region to render component to (defaults to center)
105      */
106     region : 'center',
107     
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      * @property _tree
117      * The method that retuns the tree of parts that make up this compoennt 
118      * @type {function}
119      */
120     _tree  : false,
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.substring(0,1) == '#') {
135             // if parent is a '#.....' string, then let's use that..
136             var ename = this.parent.substr(1)
137             this.parent = (this.parent == '#bootstrap') ? { el : true}  : false; // flags it as a top module...
138             el = Roo.get(ename);
139             if (!el && !this.parent) {
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                 if (!this.parent.el) {
169                         // probably an old style ctor, which has been disabled.
170                         return;
171                         
172                 }
173                 // The 'tree' method is  '_tree now' 
174             
175         var tree = this._tree ? this._tree() : this.tree();
176         
177         tree.region = tree.region || this.region;
178         Roo.log('tree');
179         Roo.log(tree);
180         if (this.parent.el === true) {
181             // bootstrap... - body..
182             this.parent.el = Roo.factory(tree);
183         }
184         Roo.log('this.parent.el');
185         Roo.log(this.parent.el);
186         this.el = this.parent.el.addxtype(tree);
187         
188         Roo.log('this.el');
189         Roo.log(this.el);
190         
191         Roo.log('this');
192         Roo.log(this);
193         
194         this.fireEvent('built', this);
195         
196         this.panel = this.el;
197         this.layout = this.panel.layout;
198                 this.parentLayout = this.parent.layout  || false;  
199          
200     }
201     
202 });
203
204 Roo.apply(Roo.XComponent, {
205     /**
206      * @property  hideProgress
207      * true to disable the building progress bar.. usefull on single page renders.
208      * @type Boolean
209      */
210     hideProgress : false,
211     /**
212      * @property  buildCompleted
213      * True when the builder has completed building the interface.
214      * @type Boolean
215      */
216     buildCompleted : false,
217      
218     /**
219      * @property  topModule
220      * the upper most module - uses document.element as it's constructor.
221      * @type Object
222      */
223      
224     topModule  : false,
225       
226     /**
227      * @property  modules
228      * array of modules to be created by registration system.
229      * @type {Array} of Roo.XComponent
230      */
231     
232     modules : [],
233     /**
234      * @property  elmodules
235      * array of modules to be created by which use #ID 
236      * @type {Array} of Roo.XComponent
237      */
238      
239     elmodules : [],
240
241     
242     /**
243      * Register components to be built later.
244      *
245      * This solves the following issues
246      * - Building is not done on page load, but after an authentication process has occured.
247      * - Interface elements are registered on page load
248      * - Parent Interface elements may not be loaded before child, so this handles that..
249      * 
250      *
251      * example:
252      * 
253      * MyApp.register({
254           order : '000001',
255           module : 'Pman.Tab.projectMgr',
256           region : 'center',
257           parent : 'Pman.layout',
258           disabled : false,  // or use a function..
259         })
260      
261      * * @param {Object} details about module
262      */
263     register : function(obj) {
264                 
265         Roo.XComponent.event.fireEvent('register', obj);
266         switch(typeof(obj.disabled) ) {
267                 
268             case 'undefined':
269                 break;
270             
271             case 'function':
272                 if ( obj.disabled() ) {
273                         return;
274                 }
275                 break;
276             
277             default:
278                 if (obj.disabled) {
279                         return;
280                 }
281                 break;
282         }
283                 
284         this.modules.push(obj);
285          
286     },
287     /**
288      * convert a string to an object..
289      * eg. 'AAA.BBB' -> finds AAA.BBB
290
291      */
292     
293     toObject : function(str)
294     {
295         if (!str || typeof(str) == 'object') {
296             return str;
297         }
298         if (str.substring(0,1) == '#') {
299             return str;
300         }
301
302         var ar = str.split('.');
303         var rt, o;
304         rt = ar.shift();
305             /** eval:var:o */
306         try {
307             eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
308         } catch (e) {
309             throw "Module not found : " + str;
310         }
311         
312         if (o === false) {
313             throw "Module not found : " + str;
314         }
315         Roo.each(ar, function(e) {
316             if (typeof(o[e]) == 'undefined') {
317                 throw "Module not found : " + str;
318             }
319             o = o[e];
320         });
321         
322         return o;
323         
324     },
325     
326     
327     /**
328      * move modules into their correct place in the tree..
329      * 
330      */
331     preBuild : function ()
332     {
333         var _t = this;
334         Roo.each(this.modules , function (obj)
335         {
336             Roo.XComponent.event.fireEvent('beforebuild', obj);
337             
338             var opar = obj.parent;
339             try { 
340                 obj.parent = this.toObject(opar);
341             } catch(e) {
342                 Roo.log("parent:toObject failed: " + e.toString());
343                 return;
344             }
345             
346             if (!obj.parent) {
347                 Roo.debug && Roo.log("GOT top level module");
348                 Roo.debug && Roo.log(obj);
349                 obj.modules = new Roo.util.MixedCollection(false, 
350                     function(o) { return o.order + '' }
351                 );
352                 this.topModule = obj;
353                 return;
354             }
355                         // parent is a string (usually a dom element name..)
356             if (typeof(obj.parent) == 'string') {
357                 this.elmodules.push(obj);
358                 return;
359             }
360             if (obj.parent.constructor != Roo.XComponent) {
361                 Roo.log("Warning : Object Parent is not instance of XComponent:" + obj.name)
362             }
363             if (!obj.parent.modules) {
364                 obj.parent.modules = new Roo.util.MixedCollection(false, 
365                     function(o) { return o.order + '' }
366                 );
367             }
368             if (obj.parent.disabled) {
369                 obj.disabled = true;
370             }
371             obj.parent.modules.add(obj);
372         }, this);
373     },
374     
375      /**
376      * make a list of modules to build.
377      * @return {Array} list of modules. 
378      */ 
379     
380     buildOrder : function()
381     {
382         var _this = this;
383         var cmp = function(a,b) {   
384             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
385         };
386         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
387             throw "No top level modules to build";
388         }
389         
390         // make a flat list in order of modules to build.
391         var mods = this.topModule ? [ this.topModule ] : [];
392                 
393         
394         // elmodules (is a list of DOM based modules )
395         Roo.each(this.elmodules, function(e) {
396             mods.push(e);
397             if (!this.topModule &&
398                 typeof(e.parent) == 'string' &&
399                 e.parent.substring(0,1) == '#' &&
400                 Roo.get(e.parent.substr(1))
401                ) {
402                 
403                 _this.topModule = e;
404             }
405             
406         });
407
408         
409         // add modules to their parents..
410         var addMod = function(m) {
411             Roo.debug && Roo.log("build Order: add: " + m.name);
412                 
413             mods.push(m);
414             if (m.modules && !m.disabled) {
415                 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
416                 m.modules.keySort('ASC',  cmp );
417                 Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
418     
419                 m.modules.each(addMod);
420             } else {
421                 Roo.debug && Roo.log("build Order: no child modules");
422             }
423             // not sure if this is used any more..
424             if (m.finalize) {
425                 m.finalize.name = m.name + " (clean up) ";
426                 mods.push(m.finalize);
427             }
428             
429         }
430         if (this.topModule && this.topModule.modules) { 
431             this.topModule.modules.keySort('ASC',  cmp );
432             this.topModule.modules.each(addMod);
433         } 
434         return mods;
435     },
436     
437      /**
438      * Build the registered modules.
439      * @param {Object} parent element.
440      * @param {Function} optional method to call after module has been added.
441      * 
442      */ 
443    
444     build : function() 
445     {
446         
447         this.preBuild();
448         var mods = this.buildOrder();
449       
450         //this.allmods = mods;
451         //Roo.debug && Roo.log(mods);
452         //return;
453         if (!mods.length) { // should not happen
454             throw "NO modules!!!";
455         }
456         
457         
458         var msg = "Building Interface...";
459         // flash it up as modal - so we store the mask!?
460         if (!this.hideProgress && Roo.MessageBox) {
461             Roo.MessageBox.show({ title: 'loading' });
462             Roo.MessageBox.show({
463                title: "Please wait...",
464                msg: msg,
465                width:450,
466                progress:true,
467                closable:false,
468                modal: false
469               
470             });
471         }
472         var total = mods.length;
473         
474         var _this = this;
475         var progressRun = function() {
476             if (!mods.length) {
477                 Roo.debug && Roo.log('hide?');
478                 if (!this.hideProgress && Roo.MessageBox) {
479                     Roo.MessageBox.hide();
480                 }
481                 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
482                 
483                 // THE END...
484                 return false;   
485             }
486             
487             var m = mods.shift();
488             
489             
490             Roo.debug && Roo.log(m);
491             // not sure if this is supported any more.. - modules that are are just function
492             if (typeof(m) == 'function') { 
493                 m.call(this);
494                 return progressRun.defer(10, _this);
495             } 
496             
497             
498             msg = "Building Interface " + (total  - mods.length) + 
499                     " of " + total + 
500                     (m.name ? (' - ' + m.name) : '');
501                         Roo.debug && Roo.log(msg);
502             if (!this.hideProgress &&  Roo.MessageBox) { 
503                 Roo.MessageBox.updateProgress(  (total  - mods.length)/total, msg  );
504             }
505             
506          
507             // is the module disabled?
508             var disabled = (typeof(m.disabled) == 'function') ?
509                 m.disabled.call(m.module.disabled) : m.disabled;    
510             
511             
512             if (disabled) {
513                 return progressRun(); // we do not update the display!
514             }
515             
516             // now build 
517             
518                         
519                         
520             m.render();
521             // it's 10 on top level, and 1 on others??? why...
522             return progressRun.defer(10, _this);
523              
524         }
525         progressRun.defer(1, _this);
526      
527         
528         
529     },
530         
531         
532         /**
533          * Event Object.
534          *
535          *
536          */
537         event: false, 
538     /**
539          * wrapper for event.on - aliased later..  
540          * Typically use to register a event handler for register:
541          *
542          * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
543          *
544          */
545     on : false
546    
547     
548     
549 });
550
551 Roo.XComponent.event = new Roo.util.Observable({
552                 events : { 
553                         /**
554                          * @event register
555                          * Fires when an Component is registered,
556                          * set the disable property on the Component to stop registration.
557                          * @param {Roo.XComponent} c the component being registerd.
558                          * 
559                          */
560                         'register' : true,
561             /**
562                          * @event beforebuild
563                          * Fires before each Component is built
564                          * can be used to apply permissions.
565                          * @param {Roo.XComponent} c the component being registerd.
566                          * 
567                          */
568                         'beforebuild' : true,
569                         /**
570                          * @event buildcomplete
571                          * Fires on the top level element when all elements have been built
572                          * @param {Roo.XComponent} the top level component.
573                          */
574                         'buildcomplete' : true
575                         
576                 }
577 });
578
579 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event); 
580