re-add roo.document
[roojs1] / Roo / Document.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 = [];
56     this.el = false; // where the layout goes..
57     
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 Roo.apply(Roo.XComponent, 
114     /**
115      * @property  buildCompleted
116      * True when the builder has completed building the interface.
117      * @type Boolean
118      */
119     buildCompleted : false,
120      
121     /**
122      * @property  topModule
123      * the upper most module - uses document.element as it's constructor.
124      * @type Object
125      */
126      
127     topModule  : false,
128       
129     /**
130      * @property  modules
131      * array of modules to be created by registration system.
132      * @type Roo.XComponent
133      */
134     
135     modules : [],
136       
137     
138     /**
139      * Register components to be built later.
140      * @param {Object} details about module
141      *
142      * This solves the following issues
143      * - Building is not done on page load, but after an authentication process has occured.
144      * - Interface elements are registered on page load
145      * - Parent Interface elements may not be loaded before child, so this handles that..
146      * 
147      *
148      * example:
149      * 
150      * MyApp.register({
151           order : '000001',
152           module : 'Pman.Tab.projectMgr',
153           region : 'center',
154           parent : 'Pman.layout',
155           disabled : false,  // or use a function..
156         })
157      * 
158      */
159     register : function(obj) {
160         this.modules.push(obj);
161          
162     },
163     /**
164      * convert a string to an object..
165      * 
166      */
167     
168     toObject : function(str)
169     {
170         if (typeof(str) == 'object') {
171             return str;
172         }
173         var ar = str.split('.');
174         var rt, o;
175         rt = ar.unshift();
176             /** eval:var:o */
177         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
178         if (o === false) {
179             throw "Module not found : " + str;
180         }
181         Roo.each(ar, function(e) {
182             if (typeof(o[e]) == 'undefined') {
183                 throw "Module not found : " + str;
184             }
185             o = o[e];
186         });
187         return o;
188         
189     }
190     
191     
192     /**
193      * move modules into their correct place in the tree..
194      * 
195      */
196     preBuild : function ()
197     {
198         var modules = this.modules;
199         this.modules = false;
200         
201         Roo.each(modules , function (obj)
202         {
203             obj.parent = this.toObject(obj.parent);
204             
205             if (!obj.parent) {
206                 this.topModule = obj;
207                 return;
208             }
209             obj.parent = toObject(obj.parent);
210             if (!obj.parent.modules) {
211                 obj.parent.modules = new Roo.util.MixedCollection(false, 
212                     function(o) { return o.order + '' }
213                 );
214             }
215             
216             obj.parent.modules.add(obj);
217         }, this);
218     }
219     
220      /**
221      * make a list of modules to build.
222      * @return {Array} list of modules. 
223      */ 
224     
225     buildOrder : function()
226     {
227         var _this = this;
228         var cmp = function(a,b) {   
229             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
230         };
231         
232         if (!this.topModule || !this.topModule.modules) {
233             throw "No top level modules to build";
234         }
235        
236         // make a flat list in order of modules to build.
237         var mods = [ this.topModule ];
238         
239         
240         // add modules to their parents..
241         var addMod = function(m) {
242            // console.log(m.modKey);
243             
244             mods.push(m);
245             if (m.module.modules) {
246                 m.module.modules.keySort('ASC',  cmp );
247                 m.module.modules.each(addMod);
248             }
249             if (m.finalize) {
250                 m.finalize.name = m.name + " (clean up) ";
251                 mods.push(m.finalize);
252             }
253             
254         }
255         this.topModule.modules.keySort('ASC',  cmp );
256         this.topModule.modules.each(addMod);
257     }
258     
259      /**
260      * Build the registered modules.
261      * @param {Object} parent element.
262      * @param {Function} optional method to call after module has been added.
263      * 
264      */ 
265    
266     build : function() 
267     {
268         
269         this.preBuild();
270         var mods = this.buildOrder();
271         
272         //this.allmods = mods;
273         //console.log(mods);
274         //return;
275         if (!mods.length) { // should not happen
276             throw "NO modules!!!";
277         }
278         
279         
280         
281         // flash it up as modal - so we store the mask!?
282         Roo.MessageBox.show({ title: 'loading' });
283         Roo.MessageBox.show({
284            title: "Please wait...",
285            msg: "Building Interface...",
286            width:450,
287            progress:true,
288            closable:false,
289            modal: false
290           
291         });
292         var total = mods.length();
293         
294         var _this = this;
295         var progressRun = function() {
296             if (mods.length) {
297                 Roo.MessageBox.hide();
298                 _this.topModule.fireEvent('buildcomplete', _this.topModule);
299                 return;    
300             }
301             
302             var m = mods.unshift();
303             
304             if (typeof(m) == 'function') { // not sure if this is supported any more..
305                 m.call(this);
306                 return progressRun.defer(10, _this);
307             } 
308             
309             Roo.MessageBox.updateProgress(
310                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
311                     " of " + total + 
312                     (m.name ? (' - ' + m.name) : '')
313                     );
314             
315          
316             
317             var disabled = (typeof(m.module.disabled) == 'function') ?
318                 m.module.disabled.call(m.module.disabled) : m.module.disabled;    
319             }
320             
321             if (disabled) {
322                 return progressRun(); // we do not update the display!
323             }
324             
325             m.el = m.parent.el.addxtype(m.items[0]);
326             m.fireEvent('built', m);
327             m.panel = this.el;
328             m.layout = m.panel.layout;    
329              
330             
331         }
332         progressRun.defer(1, _this);
333      
334         
335         
336     }
337      
338    
339     
340     
341 });
342