Roo/XComponent.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              * @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         
267         return o;
268         
269     },
270     
271     
272     /**
273      * move modules into their correct place in the tree..
274      * 
275      */
276     preBuild : function ()
277     {
278         var _t = this;
279         Roo.each(this.modules , function (obj)
280         {
281             var opar = obj.parent;
282             obj.parent = this.toObject(opar);
283             
284             if (!obj.parent) {
285                 this.topModule = obj;
286                 return;
287             }
288             if (typeof(obj.parent) == 'string') {
289                 this.elmodules.push(obj);
290                 return;
291             }
292             if (obj.parent.constructor != Roo.XComponent) {
293                 Roo.log("Object Parent is not instance of XComponent:" + opar)
294             }
295             if (!obj.parent.modules) {
296                 obj.parent.modules = new Roo.util.MixedCollection(false, 
297                     function(o) { return o.order + '' }
298                 );
299             }
300             
301             obj.parent.modules.add(obj);
302         }, this);
303     },
304     
305      /**
306      * make a list of modules to build.
307      * @return {Array} list of modules. 
308      */ 
309     
310     buildOrder : function()
311     {
312         var _this = this;
313         var cmp = function(a,b) {   
314             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
315         };
316         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
317             throw "No top level modules to build";
318         }
319         
320         // make a flat list in order of modules to build.
321         var mods = this.topModule ? [ this.topModule ] : [];
322         Roo.each(this.elmodules,function(e) { mods.push(e) });
323
324         
325         // add modules to their parents..
326         var addMod = function(m) {
327            // Roo.debug && Roo.log(m.modKey);
328             
329             mods.push(m);
330             if (m.modules) {
331                 m.modules.keySort('ASC',  cmp );
332                 m.modules.each(addMod);
333             }
334             // not sure if this is used any more..
335             if (m.finalize) {
336                 m.finalize.name = m.name + " (clean up) ";
337                 mods.push(m.finalize);
338             }
339             
340         }
341         if (this.topModule) { 
342             this.topModule.modules.keySort('ASC',  cmp );
343             this.topModule.modules.each(addMod);
344         }
345         return mods;
346     },
347     
348      /**
349      * Build the registered modules.
350      * @param {Object} parent element.
351      * @param {Function} optional method to call after module has been added.
352      * 
353      */ 
354    
355     build : function() 
356     {
357         
358         this.preBuild();
359         var mods = this.buildOrder();
360       
361         //this.allmods = mods;
362         //Roo.debug && Roo.log(mods);
363         //return;
364         if (!mods.length) { // should not happen
365             throw "NO modules!!!";
366         }
367         
368         
369         
370         // flash it up as modal - so we store the mask!?
371         Roo.MessageBox.show({ title: 'loading' });
372         Roo.MessageBox.show({
373            title: "Please wait...",
374            msg: "Building Interface...",
375            width:450,
376            progress:true,
377            closable:false,
378            modal: false
379           
380         });
381         var total = mods.length;
382         
383         var _this = this;
384         var progressRun = function() {
385             if (!mods.length) {
386                 Roo.debug && Roo.log('hide?');
387                 Roo.MessageBox.hide();
388                 if (_this.topModule) { 
389                     _this.topModule.fireEvent('buildcomplete', _this.topModule);
390                 }
391                 // THE END...
392                 return false;   
393             }
394             
395             var m = mods.shift();
396             
397             
398             Roo.debug && Roo.log(m);
399             // not sure if this is supported any more.. - modules that are are just function
400             if (typeof(m) == 'function') { 
401                 m.call(this);
402                 return progressRun.defer(10, _this);
403             } 
404             
405             
406             
407             Roo.MessageBox.updateProgress(
408                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
409                     " of " + total + 
410                     (m.name ? (' - ' + m.name) : '')
411                     );
412             
413          
414             // is the module disabled?
415             var disabled = (typeof(m.disabled) == 'function') ?
416                 m.disabled.call(m.module.disabled) : m.disabled;    
417             
418             
419             if (disabled) {
420                 return progressRun(); // we do not update the display!
421             }
422             
423             // now build 
424             
425             m.render();
426             // it's 10 on top level, and 1 on others??? why...
427             return progressRun.defer(10, _this);
428              
429         }
430         progressRun.defer(1, _this);
431      
432         
433         
434     }
435     
436      
437    
438     
439     
440 });
441