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