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