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