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     
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 Array
71      */
72      
73     topModule  : false,
74       
75     /**
76      * @property  modules
77      * array of modules to be created by registration system.
78      * @type Array
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     /**
111      * convert a string to an object..
112      * 
113      */
114     
115     toObject : function(str)
116     {
117         if (typeof(str) == 'object') {
118             return str;
119         }
120         var ar = str.split('.');
121         var rt, o;
122         rt = ar.unshift();
123             /** eval:var:o */
124         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
125         if (o === false) {
126             throw "Module not found : " + str;
127         }
128         Roo.each(ar, function(e) {
129             if (typeof(o[e]) == 'undefined') {
130                 throw "Module not found : " + str;
131             }
132             o = o[e];
133         });
134         return o;
135         
136     }
137     
138     
139     /**
140      * move modules into their correct place in the tree..
141      * 
142      */
143     preBuild : function ()
144     {
145         var modules = this.modules;
146         this.modules = false;
147         
148         Roo.each(modules , function (obj)
149         {
150             obj.parent = this.toObject(obj.parent);
151             
152             if (!obj.parent) {
153                 this.topModule = obj;
154                 return;
155             }
156             obj.parent = toObject(obj.parent);
157             if (!obj.parent.modules) {
158                 obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.order + '' });
159             }
160             
161             obj.parent.modules.add(obj);
162         }, this);
163     }
164     
165     
166      /**
167      * Build the registered modules.
168      * @param {Object} parent element.
169      * @param {Function} optional method to call after module has been added.
170      * 
171      */ 
172    
173     build : function(onCompleteFn) 
174     {
175         var onComplete = function () {
176             if (onCompleteFn) {
177                 onCompleteFn.call(this);
178             }
179             Roo.MessageBox.hide();
180             
181         
182             
183         }
184         var _this = this;
185         var cmp = function(a,b) {   
186             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
187         };
188         
189         if (!parent.modules) {
190             return;
191         }
192        
193         // make a flat list in order of modules to build.
194         var mods = [];
195         
196         
197         // add modules to their parents..
198         var addMod = function(m) {
199            // console.log(m.modKey);
200             
201             mods.push(m);
202             if (m.module.modules) {
203                 m.module.modules.keySort('ASC',  cmp );
204                 m.module.modules.each(addMod);
205             }
206             if (m.finalize) {
207                 m.finalize.name = m.name + " (clean up) ";
208                 mods.push(m.finalize);
209             }
210             
211         }
212         parent.modules.keySort('ASC',  cmp );
213         parent.modules.each(addMod);
214         //this.allmods = mods;
215         //console.log(mods);
216         //return;
217         if (!mods.length) {
218             return onComplete.call(this);
219         }
220         // flash it up as modal - so we store the mask!?
221         Roo.MessageBox.show({ title: 'loading' });
222         Roo.MessageBox.show({
223            title: "Please wait...",
224            msg: "Building Interface...",
225            width:450,
226            progress:true,
227            closable:false,
228            modal: false
229           
230         });
231         var n = -1;
232         var _this = this;
233         var progressRun = function() {
234             n++;
235             if (n >= mods.length) {
236                 return onComplete.call(this);
237             }
238             
239             var m = mods[n];
240             
241             
242             Roo.MessageBox.updateProgress(
243                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
244                     " of " + mods.length + 
245                     (m.name ? (' - ' + m.name) : '')
246                     );
247             
248             if (typeof(m) == 'function') {
249                 m.call(this);
250                 progressRun.defer(10, _this);    
251                 return;
252             } 
253             var disabled = (typeof(m.module.disabled) == 'function') ?
254                 m.module.disabled.call(m.module.disabled) : m.module.disabled;
255                 
256             }
257             
258             if (m.parent.layout && !disabled) {
259                 // modules have to support a  'add method'
260                 // should we just move that code into here..
261                 m.module.add(m.parent.layout, m.region);    
262             }
263                  
264              
265             
266         }
267         progressRun.defer(1, _this);
268      
269         
270         
271     }
272      
273    
274     
275     
276 });
277