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