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                 this.topModule = obj;
316                 return;
317             }
318             if (typeof(obj.parent) == 'string') {
319                 this.elmodules.push(obj);
320                 return;
321             }
322             if (obj.parent.constructor != Roo.XComponent) {
323                 Roo.log("Object Parent is not instance of XComponent:" + obj.name)
324             }
325             if (!obj.parent.modules) {
326                 obj.parent.modules = new Roo.util.MixedCollection(false, 
327                     function(o) { return o.order + '' }
328                 );
329             }
330             
331             obj.parent.modules.add(obj);
332         }, this);
333     },
334     
335      /**
336      * make a list of modules to build.
337      * @return {Array} list of modules. 
338      */ 
339     
340     buildOrder : function()
341     {
342         var _this = this;
343         var cmp = function(a,b) {   
344             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
345         };
346         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
347             throw "No top level modules to build";
348         }
349         
350         // make a flat list in order of modules to build.
351         var mods = this.topModule ? [ this.topModule ] : [];
352         Roo.each(this.elmodules,function(e) { mods.push(e) });
353
354         
355         // add modules to their parents..
356         var addMod = function(m) {
357            // Roo.debug && Roo.log(m.modKey);
358             
359             mods.push(m);
360             if (m.modules) {
361                 m.modules.keySort('ASC',  cmp );
362                 m.modules.each(addMod);
363             }
364             // not sure if this is used any more..
365             if (m.finalize) {
366                 m.finalize.name = m.name + " (clean up) ";
367                 mods.push(m.finalize);
368             }
369             
370         }
371         if (this.topModule) { 
372             this.topModule.modules.keySort('ASC',  cmp );
373             this.topModule.modules.each(addMod);
374         }
375         return mods;
376     },
377     
378      /**
379      * Build the registered modules.
380      * @param {Object} parent element.
381      * @param {Function} optional method to call after module has been added.
382      * 
383      */ 
384    
385     build : function() 
386     {
387         
388         this.preBuild();
389         var mods = this.buildOrder();
390       
391         //this.allmods = mods;
392         //Roo.debug && Roo.log(mods);
393         //return;
394         if (!mods.length) { // should not happen
395             throw "NO modules!!!";
396         }
397         
398         
399         
400         // flash it up as modal - so we store the mask!?
401         Roo.MessageBox.show({ title: 'loading' });
402         Roo.MessageBox.show({
403            title: "Please wait...",
404            msg: "Building Interface...",
405            width:450,
406            progress:true,
407            closable:false,
408            modal: false
409           
410         });
411         var total = mods.length;
412         
413         var _this = this;
414         var progressRun = function() {
415             if (!mods.length) {
416                 Roo.debug && Roo.log('hide?');
417                 Roo.MessageBox.hide();
418                 if (_this.topModule) { 
419                     _this.topModule.fireEvent('buildcomplete', _this.topModule);
420                 }
421                 // THE END...
422                 return false;   
423             }
424             
425             var m = mods.shift();
426             
427             
428             Roo.debug && Roo.log(m);
429             // not sure if this is supported any more.. - modules that are are just function
430             if (typeof(m) == 'function') { 
431                 m.call(this);
432                 return progressRun.defer(10, _this);
433             } 
434             
435             
436             
437             Roo.MessageBox.updateProgress(
438                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
439                     " of " + total + 
440                     (m.name ? (' - ' + m.name) : '')
441                     );
442             
443          
444             // is the module disabled?
445             var disabled = (typeof(m.disabled) == 'function') ?
446                 m.disabled.call(m.module.disabled) : m.disabled;    
447             
448             
449             if (disabled) {
450                 return progressRun(); // we do not update the display!
451             }
452             
453             // now build 
454             
455             m.render();
456             // it's 10 on top level, and 1 on others??? why...
457             return progressRun.defer(10, _this);
458              
459         }
460         progressRun.defer(1, _this);
461      
462         
463         
464     },
465         
466         
467         /**
468          * Event Object.
469          *
470          *
471          */
472         event: false, 
473     /**
474          * wrapper for event.on - aliased later..  
475          * Typically use to register a event handler for register:
476          *
477          * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
478          *
479          */
480     on : false
481    
482     
483     
484 });
485 Roo.XComponent.event = new Roo.util.Observable({
486                 events : { 
487                         /**
488                          * @event register
489                          * Fires when an Component is registered,
490                          * set the disable property on the Component to stop registration.
491                          * @param {Roo.XComponent} c the component being registerd.
492                          * 
493                          */
494                         'register' : true
495                         
496                 }
497 });
498
499 Roo.XComponent.on = Roo.XComponent.event.on
500