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