XComponent - fix code after refactoring.
[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         
133         if (!el && typeof(this.parent) == 'string' && this.parent[0] == '#') {
134             // if parent is a '#.....' string, then let's use that..
135             var ename = this.parent.substr(1)
136             this.parent = false;
137             el = Roo.get(ename);
138             if (!el) {
139                 Roo.log("Warning - element can not be found :#" + ename );
140                 return;
141             }
142         }
143         if (!this.parent) {
144             
145             el = el ? Roo.get(el) : false;
146             
147             // it's a top level one..
148             this.parent =  {
149                 el : new Roo.BorderLayout(el || document.body, {
150                 
151                      center: {
152                          titlebar: false,
153                          autoScroll:false,
154                          closeOnTab: true,
155                          tabPosition: 'top',
156                           //resizeTabs: true,
157                          alwaysShowTabs: el ? false :  true,
158                          hideTabs: el ? true :  false,
159                          minTabWidth: 140
160                      }
161                  })
162             }
163         }
164         
165         
166             
167         var tree = this.tree();
168         tree.region = tree.region || this.region;
169         this.el = this.parent.el.addxtype(tree);
170         this.fireEvent('built', this);
171         
172         this.panel = this.el;
173         this.layout = this.panel.layout;    
174          
175     }
176     
177 });
178
179 Roo.apply(Roo.XComponent, {
180     
181     /**
182      * @property  buildCompleted
183      * True when the builder has completed building the interface.
184      * @type Boolean
185      */
186     buildCompleted : false,
187      
188     /**
189      * @property  topModule
190      * the upper most module - uses document.element as it's constructor.
191      * @type Object
192      */
193      
194     topModule  : false,
195       
196     /**
197      * @property  modules
198      * array of modules to be created by registration system.
199      * @type {Array} of Roo.XComponent
200      */
201     
202     modules : [],
203     /**
204      * @property  elmodules
205      * array of modules to be created by which use #ID 
206      * @type {Array} of Roo.XComponent
207      */
208      
209     elmodules : [],
210
211     
212     /**
213      * Register components to be built later.
214      *
215      * This solves the following issues
216      * - Building is not done on page load, but after an authentication process has occured.
217      * - Interface elements are registered on page load
218      * - Parent Interface elements may not be loaded before child, so this handles that..
219      * 
220      *
221      * example:
222      * 
223      * MyApp.register({
224           order : '000001',
225           module : 'Pman.Tab.projectMgr',
226           region : 'center',
227           parent : 'Pman.layout',
228           disabled : false,  // or use a function..
229         })
230      
231      * * @param {Object} details about module
232      */
233     register : function(obj) {
234         this.modules.push(obj);
235          
236     },
237     /**
238      * convert a string to an object..
239      * eg. 'AAA.BBB' -> finds AAA.BBB
240
241      */
242     
243     toObject : function(str)
244     {
245         if (!str || typeof(str) == 'object') {
246             return str;
247         }
248         if (str[0]=='#') {
249             return str;
250         }
251
252         var ar = str.split('.');
253         var rt, o;
254         rt = ar.shift();
255             /** eval:var:o */
256         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
257         if (o === false) {
258             throw "Module not found : " + str;
259         }
260         Roo.each(ar, function(e) {
261             if (typeof(o[e]) == 'undefined') {
262                 throw "Module not found : " + str;
263             }
264             o = o[e];
265         });
266         return o;
267         
268     },
269     
270     
271     /**
272      * move modules into their correct place in the tree..
273      * 
274      */
275     preBuild : function ()
276     {
277         
278         Roo.each(this.modules , function (obj)
279         {
280             obj.parent = this.toObject(obj.parent);
281             
282             if (!obj.parent) {
283                 this.topModule = obj;
284                 return;
285             }
286             if (typeof(obj.parent) == 'string') {
287                 this.elmodules.push(obj);
288                 return;
289             }
290             
291             if (!obj.parent.modules) {
292                 obj.parent.modules = new Roo.util.MixedCollection(false, 
293                     function(o) { return o.order + '' }
294                 );
295             }
296             
297             obj.parent.modules.add(obj);
298         }, this);
299     },
300     
301      /**
302      * make a list of modules to build.
303      * @return {Array} list of modules. 
304      */ 
305     
306     buildOrder : function()
307     {
308         var _this = this;
309         var cmp = function(a,b) {   
310             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
311         };
312         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
313             throw "No top level modules to build";
314         }
315         
316         // make a flat list in order of modules to build.
317         var mods = this.topModule ? [ this.topModule ] : [];
318         Roo.each(this.elmodules,function(e) { mods.push(e) });
319
320         
321         // add modules to their parents..
322         var addMod = function(m) {
323            // Roo.debug && Roo.log(m.modKey);
324             
325             mods.push(m);
326             if (m.modules) {
327                 m.modules.keySort('ASC',  cmp );
328                 m.modules.each(addMod);
329             }
330             // not sure if this is used any more..
331             if (m.finalize) {
332                 m.finalize.name = m.name + " (clean up) ";
333                 mods.push(m.finalize);
334             }
335             
336         }
337         if (this.topModule) { 
338             this.topModule.modules.keySort('ASC',  cmp );
339             this.topModule.modules.each(addMod);
340         }
341         return mods;
342     },
343     
344      /**
345      * Build the registered modules.
346      * @param {Object} parent element.
347      * @param {Function} optional method to call after module has been added.
348      * 
349      */ 
350    
351     build : function() 
352     {
353         
354         this.preBuild();
355         var mods = this.buildOrder();
356       
357         //this.allmods = mods;
358         //Roo.debug && Roo.log(mods);
359         //return;
360         if (!mods.length) { // should not happen
361             throw "NO modules!!!";
362         }
363         
364         
365         
366         // flash it up as modal - so we store the mask!?
367         Roo.MessageBox.show({ title: 'loading' });
368         Roo.MessageBox.show({
369            title: "Please wait...",
370            msg: "Building Interface...",
371            width:450,
372            progress:true,
373            closable:false,
374            modal: false
375           
376         });
377         var total = mods.length;
378         
379         var _this = this;
380         var progressRun = function() {
381             if (!mods.length) {
382                 Roo.debug && Roo.log('hide?');
383                 Roo.MessageBox.hide();
384                 if (_this.topModule) { 
385                     _this.topModule.fireEvent('buildcomplete', _this.topModule);
386                 }
387                 // THE END...
388                 return false;   
389             }
390             
391             var m = mods.shift();
392             
393             
394             Roo.debug && Roo.log(m);
395             // not sure if this is supported any more.. - modules that are are just function
396             if (typeof(m) == 'function') { 
397                 m.call(this);
398                 return progressRun.defer(10, _this);
399             } 
400             
401             
402             
403             Roo.MessageBox.updateProgress(
404                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
405                     " of " + total + 
406                     (m.name ? (' - ' + m.name) : '')
407                     );
408             
409          
410             // is the module disabled?
411             var disabled = (typeof(m.disabled) == 'function') ?
412                 m.disabled.call(m.module.disabled) : m.disabled;    
413             
414             
415             if (disabled) {
416                 return progressRun(); // we do not update the display!
417             }
418             
419             // now build 
420             
421             m.render();
422             // it's 10 on top level, and 1 on others??? why...
423             return progressRun.defer(10, _this);
424              
425         }
426         progressRun.defer(1, _this);
427      
428         
429         
430     }
431     
432      
433    
434     
435     
436 });
437