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