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, function(o) { return o.order + '' });
161             }
162             
163             obj.parent.modules.add(obj);
164         }, this);
165     }
166     
167     
168      /**
169      * Build the registered modules.
170      * @param {Object} parent element.
171      * @param {Function} optional method to call after module has been added.
172      * 
173      */ 
174    
175     build : function(onCompleteFn) 
176     {
177         
178         this.preBuild();
179         var onComplete = function () {
180             if (onCompleteFn) {
181                 onCompleteFn.call(this);
182             }
183             
184             
185         
186             
187         }
188         var _this = this;
189         var cmp = function(a,b) {   
190             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
191         };
192         
193         if (!parent.modules) {
194             return;
195         }
196        
197         // make a flat list in order of modules to build.
198         var mods = [];
199         
200         
201         // add modules to their parents..
202         var addMod = function(m) {
203            // console.log(m.modKey);
204             
205             mods.push(m);
206             if (m.module.modules) {
207                 m.module.modules.keySort('ASC',  cmp );
208                 m.module.modules.each(addMod);
209             }
210             if (m.finalize) {
211                 m.finalize.name = m.name + " (clean up) ";
212                 mods.push(m.finalize);
213             }
214             
215         }
216         parent.modules.keySort('ASC',  cmp );
217         parent.modules.each(addMod);
218         //this.allmods = mods;
219         //console.log(mods);
220         //return;
221         if (!mods.length) { // should not happen
222             throw "NO modules!!!";
223         }
224         // flash it up as modal - so we store the mask!?
225         Roo.MessageBox.show({ title: 'loading' });
226         Roo.MessageBox.show({
227            title: "Please wait...",
228            msg: "Building Interface...",
229            width:450,
230            progress:true,
231            closable:false,
232            modal: false
233           
234         });
235         var n = -1;
236         var _this = this;
237         var progressRun = function() {
238             n++;
239             if (n >= mods.length) {
240                 Roo.MessageBox.hide();
241                 _this.topModule.fireEvent('built', _this.topModule);
242                 return;
243             }
244             
245             var m = mods[n];
246             
247             
248             Roo.MessageBox.updateProgress(
249                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
250                     " of " + mods.length + 
251                     (m.name ? (' - ' + m.name) : '')
252                     );
253             
254             if (typeof(m) == 'function') {
255                 m.call(this);
256                 progressRun.defer(10, _this);    
257                 return;
258             } 
259             var disabled = (typeof(m.module.disabled) == 'function') ?
260                 m.module.disabled.call(m.module.disabled) : m.module.disabled;
261                 
262             }
263             
264             if (m.parent.layout && !disabled) {
265                 // modules have to support a  'add method'
266                 // should we just move that code into here..
267                 m.module.add(m.parent.layout, m.region);    
268             }
269                  
270              
271             
272         }
273         progressRun.defer(1, _this);
274      
275         
276         
277     }
278      
279    
280     
281     
282 });
283