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