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