Roo/form/ComboBoxArray.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         // elmodules (is a list of DOM based modules )
378         Roo.each(this.elmodules, function(e) {
379             mods.push(e)
380         });
381
382         
383         // add modules to their parents..
384         var addMod = function(m) {
385             Roo.debug && Roo.log("build Order: add: " + m.name);
386             
387         mods.push(m);
388         if (m.modules && !m.disabled) {
389             Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules");
390             m.modules.keySort('ASC',  cmp );
391             Roo.debug && Roo.log("build Order: " + m.modules.length + " child modules (after sort)");
392
393             m.modules.each(addMod);
394         } else {
395             Roo.debug && Roo.log("build Order: no child modules");
396             }
397             // not sure if this is used any more..
398             if (m.finalize) {
399                 m.finalize.name = m.name + " (clean up) ";
400                 mods.push(m.finalize);
401             }
402             
403         }
404         if (this.topModule) { 
405             this.topModule.modules.keySort('ASC',  cmp );
406             this.topModule.modules.each(addMod);
407         }
408         return mods;
409     },
410     
411      /**
412      * Build the registered modules.
413      * @param {Object} parent element.
414      * @param {Function} optional method to call after module has been added.
415      * 
416      */ 
417    
418     build : function() 
419     {
420         
421         this.preBuild();
422         var mods = this.buildOrder();
423       
424         //this.allmods = mods;
425         //Roo.debug && Roo.log(mods);
426         //return;
427         if (!mods.length) { // should not happen
428             throw "NO modules!!!";
429         }
430         
431         
432         var msg = "Building Interface...";
433         // flash it up as modal - so we store the mask!?
434         if (!this.hideProgress) {
435             Roo.MessageBox.show({ title: 'loading' });
436             Roo.MessageBox.show({
437                title: "Please wait...",
438                msg: msg,
439                width:450,
440                progress:true,
441                closable:false,
442                modal: false
443               
444             });
445         }
446         var total = mods.length;
447         
448         var _this = this;
449         var progressRun = function() {
450             if (!mods.length) {
451                 Roo.debug && Roo.log('hide?');
452                 if (!this.hideProgress) {
453                     Roo.MessageBox.hide();
454                 }
455                 Roo.XComponent.event.fireEvent('buildcomplete', _this.topModule);
456                 
457                 // THE END...
458                 return false;   
459             }
460             
461             var m = mods.shift();
462             
463             
464             Roo.debug && Roo.log(m);
465             // not sure if this is supported any more.. - modules that are are just function
466             if (typeof(m) == 'function') { 
467                 m.call(this);
468                 return progressRun.defer(10, _this);
469             } 
470             
471             
472             msg = "Building Interface " + (total  - mods.length) + 
473                     " of " + total + 
474                     (m.name ? (' - ' + m.name) : '');
475                         Roo.debug && Roo.log(msg);
476             if (!this.hideProgress) { 
477                 Roo.MessageBox.updateProgress(  (total  - mods.length)/total, msg  );
478             }
479             
480          
481             // is the module disabled?
482             var disabled = (typeof(m.disabled) == 'function') ?
483                 m.disabled.call(m.module.disabled) : m.disabled;    
484             
485             
486             if (disabled) {
487                 return progressRun(); // we do not update the display!
488             }
489             
490             // now build 
491             
492                         
493                         
494             m.render();
495             // it's 10 on top level, and 1 on others??? why...
496             return progressRun.defer(10, _this);
497              
498         }
499         progressRun.defer(1, _this);
500      
501         
502         
503     },
504         
505         
506         /**
507          * Event Object.
508          *
509          *
510          */
511         event: false, 
512     /**
513          * wrapper for event.on - aliased later..  
514          * Typically use to register a event handler for register:
515          *
516          * eg. Roo.XComponent.on('register', function(comp) { comp.disable = true } );
517          *
518          */
519     on : false
520    
521     
522     
523 });
524
525 Roo.XComponent.event = new Roo.util.Observable({
526                 events : { 
527                         /**
528                          * @event register
529                          * Fires when an Component is registered,
530                          * set the disable property on the Component to stop registration.
531                          * @param {Roo.XComponent} c the component being registerd.
532                          * 
533                          */
534                         'register' : true,
535             /**
536                          * @event beforebuild
537                          * Fires before each Component is built
538                          * can be used to apply permissions.
539                          * @param {Roo.XComponent} c the component being registerd.
540                          * 
541                          */
542                         'beforebuild' : true,
543                         /**
544                          * @event buildcomplete
545                          * Fires on the top level element when all elements have been built
546                          * @param {Roo.XComponent} the top level component.
547                          */
548                         'buildcomplete' : true
549                         
550                 }
551 });
552
553 Roo.XComponent.on = Roo.XComponent.event.on.createDelegate(Roo.XComponent.event); 
554