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