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