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