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