Roo/XComponent.js
[roojs1] / Roo / XComponent.js
1 /*
2  * Original code for Roojs - LGPL
3  * <script type="text/javascript">
4  */
5  
6 /**
7  * 
8  * This needs some more thought..
9  * 
10  * 
11  * 
12  * @class Roo.XComponent
13  * @extends Roo.data.Observable
14  * 
15  * A delayed Element creator...
16  * 
17  * Mypart.xyx = new Roo.XComponent({
18
19     parent : 'Mypart.xyz', // empty == document.element.!!
20     order : '001',
21     name : 'xxxx'
22     region : 'xxxx'
23     disabled : function() {} 
24      
25     items : [  // technically only one component..
26         {
27             xtype : 'NestedLayoutPanel',
28             // technicall
29         }
30      ]
31  *})
32  * 
33  * 
34  * 
35  */
36 Roo.XComponent = function(cfg) {
37     Roo.apply(this, cfg);
38     this.addEvents({ 
39         /**
40              * @event built
41              * Fires when this the componnt is built
42              * @param {Roo.XComponent} c the component
43              */
44         'built' : true,
45         /**
46              * @event buildcomplete
47              * Fires on the top level element when all elements have been built
48              * @param {Roo.XComponent} c the top level component.
49          */
50         'buildcomplete' : true,
51         
52     });
53     
54     Roo.XComponent.register(this);
55     this.modules = false;
56     this.el = false; // where the layout goes..
57     this.setX(this.items[0]);
58     
59 }
60 Roo.extend(Roo.XComponent, Roo.util.Observable, {
61     /**
62      * @property el
63      * The created element (with Roo.factory())
64      * @type {Roo.Layout}
65      */
66     el  : false,
67     
68     /**
69      * @property el
70      * for BC  - use el in new code
71      * @type {Roo.Layout}
72      */
73     panel : false,
74     
75     /**
76      * @property layout
77      * for BC  - use el in new code
78      * @type {Roo.Layout}
79      */
80     layout : false,
81     
82      /**
83      * @cfg {Function|boolean} disabled
84      * If this module is disabled by some rule, return true from the funtion
85      */
86     disabled : false,
87     
88     /**
89      * @cfg {String} parent 
90      * Name of parent element which it get xtype added to..
91      */
92     parent: false,
93     
94     /**
95      * @cfg {String} order
96      * Used to set the order in which elements are created (usefull for multiple tabs)
97      */
98     
99     order : false,
100     /**
101      * @cfg {String} name
102      * String to display while loading.
103      */
104     name : false,
105     /**
106      * @cfg {Array} items
107      * A single item array - the first element is the root of the tree..
108      * It's done this way to stay compatible with the Xtype system...
109      */
110     items : false,
111     
112     /**
113      * setx 
114      * Set's the xowner value on all the children
115      * basically var _this= this.xmodule; can be used in any component..
116      * to refer to it's own xmodule
117      * @param xtype element.
118      */
119     setX : function(obj)
120     {
121         obj.xmodule = this;
122         if (!obj.items) return;
123         Roo.each(obj.items, function (o) {
124             this.setX(o);
125         }, this);
126     }
127      
128     
129 });
130
131 Roo.apply(Roo.XComponent, {
132     
133     /**
134      * @property  buildCompleted
135      * True when the builder has completed building the interface.
136      * @type Boolean
137      */
138     buildCompleted : false,
139      
140     /**
141      * @property  topModule
142      * the upper most module - uses document.element as it's constructor.
143      * @type Object
144      */
145      
146     topModule  : false,
147       
148     /**
149      * @property  modules
150      * array of modules to be created by registration system.
151      * @type Roo.XComponent
152      */
153     
154     modules : [],
155       
156     
157     /**
158      * Register components to be built later.
159      * @param {Object} details about module
160      *
161      * This solves the following issues
162      * - Building is not done on page load, but after an authentication process has occured.
163      * - Interface elements are registered on page load
164      * - Parent Interface elements may not be loaded before child, so this handles that..
165      * 
166      *
167      * example:
168      * 
169      * MyApp.register({
170           order : '000001',
171           module : 'Pman.Tab.projectMgr',
172           region : 'center',
173           parent : 'Pman.layout',
174           disabled : false,  // or use a function..
175         })
176      * 
177      */
178     register : function(obj) {
179         this.modules.push(obj);
180          
181     },
182     /**
183      * convert a string to an object..
184      * 
185      */
186     
187     toObject : function(str)
188     {
189         if (!str || typeof(str) == 'object') {
190             return str;
191         }
192         var ar = str.split('.');
193         var rt, o;
194         rt = ar.shift();
195             /** eval:var:o */
196         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
197         if (o === false) {
198             throw "Module not found : " + str;
199         }
200         Roo.each(ar, function(e) {
201             if (typeof(o[e]) == 'undefined') {
202                 throw "Module not found : " + str;
203             }
204             o = o[e];
205         });
206         return o;
207         
208     },
209     
210     
211     /**
212      * move modules into their correct place in the tree..
213      * 
214      */
215     preBuild : function ()
216     {
217         
218         Roo.each(this.modules , function (obj)
219         {
220             obj.parent = this.toObject(obj.parent);
221             
222             if (!obj.parent) {
223                 this.topModule = obj;
224                 return;
225             }
226             
227             if (!obj.parent.modules) {
228                 obj.parent.modules = new Roo.util.MixedCollection(false, 
229                     function(o) { return o.order + '' }
230                 );
231             }
232             
233             obj.parent.modules.add(obj);
234         }, this);
235     },
236     
237      /**
238      * make a list of modules to build.
239      * @return {Array} list of modules. 
240      */ 
241     
242     buildOrder : function()
243     {
244         var _this = this;
245         var cmp = function(a,b) {   
246             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
247         };
248         
249         if (!this.topModule || !this.topModule.modules) {
250             throw "No top level modules to build";
251         }
252        
253         // make a flat list in order of modules to build.
254         var mods = [ this.topModule ];
255         
256         
257         // add modules to their parents..
258         var addMod = function(m) {
259            // console.log(m.modKey);
260             
261             mods.push(m);
262             if (m.modules) {
263                 m.modules.keySort('ASC',  cmp );
264                 m.modules.each(addMod);
265             }
266             // not sure if this is used any more..
267             if (m.finalize) {
268                 m.finalize.name = m.name + " (clean up) ";
269                 mods.push(m.finalize);
270             }
271             
272         }
273         this.topModule.modules.keySort('ASC',  cmp );
274         this.topModule.modules.each(addMod);
275         return mods;
276     },
277     
278      /**
279      * Build the registered modules.
280      * @param {Object} parent element.
281      * @param {Function} optional method to call after module has been added.
282      * 
283      */ 
284    
285     build : function() 
286     {
287         
288         this.preBuild();
289         var mods = this.buildOrder();
290         throw "testing";
291         //this.allmods = mods;
292         //console.log(mods);
293         //return;
294         if (!mods.length) { // should not happen
295             throw "NO modules!!!";
296         }
297         
298         
299         
300         // flash it up as modal - so we store the mask!?
301         Roo.MessageBox.show({ title: 'loading' });
302         Roo.MessageBox.show({
303            title: "Please wait...",
304            msg: "Building Interface...",
305            width:450,
306            progress:true,
307            closable:false,
308            modal: false
309           
310         });
311         var total = mods.length;
312         
313         var _this = this;
314         var progressRun = function() {
315             if (!mods.length) {
316                 console.log('hide?');
317                 Roo.MessageBox.hide();
318                 _this.topModule.fireEvent('buildcomplete', _this.topModule);
319                 return;    
320             }
321             
322             var m = mods.shift();
323             console.log(m);
324             if (typeof(m) == 'function') { // not sure if this is supported any more..
325                 m.call(this);
326                 return progressRun.defer(10, _this);
327             } 
328             
329             Roo.MessageBox.updateProgress(
330                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
331                     " of " + total + 
332                     (m.name ? (' - ' + m.name) : '')
333                     );
334             
335          
336             
337             var disabled = (typeof(m.disabled) == 'function') ?
338                 m.disabled.call(m.module.disabled) : m.disabled;    
339             
340             
341             if (disabled) {
342                 return progressRun(); // we do not update the display!
343             }
344             
345             if (!m.parent) {
346                 // it's a top level one..
347                 var layoutbase = new Ext.BorderLayout(document.body, {
348                
349                     center: {
350                          titlebar: false,
351                          autoScroll:false,
352                          closeOnTab: true,
353                          tabPosition: 'top',
354                          //resizeTabs: true,
355                          alwaysShowTabs: true,
356                          minTabWidth: 140
357                     }
358                 });
359                 m.items[0].region = 'center';
360                 m.el = layoutbase.addxtype(  m.items[0] );
361                 m.panel = m.el;
362                 m.layout = m.panel.layout;    
363                 return progressRun.defer(10, _this);
364             }
365             
366             
367             m.items[0].region = m.items[0].region || m.region;
368             m.el = m.parent.el.addxtype(m.items[0]);
369             m.fireEvent('built', m);
370             m.panel = m.el;
371             m.layout = m.panel.layout;    
372             progressRun.defer(10, _this); 
373             
374         }
375         progressRun.defer(1, _this);
376      
377         
378         
379     }
380      
381    
382     
383     
384 });
385