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