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