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