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