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     this.el = false; // where the layout goes..
53     
54     
55 }
56 Roo.extend(Roo.XComponent, Roo.util.Observable {
57     /**
58      * @property el
59      * The created element (with Roo.factory())
60      * @type {Roo.Layout}
61      */
62
63     el  : false,
64     
65     
66     panel : false,
67     
68     /**
69      * @property layout
70      * for BC  - use el in new code
71      * @type {Roo.Layout}
72      */
73     layout : false,
74     
75      /**
76      * @cfg {Function|boolean} disabled
77      * If this module is disabled by some rule, return true from the funtion
78      */
79     disabled : false,
80     /**
81      * @cfg {String} parent 
82      * Name of parent element which it get xtype added to..
83      */
84     parent: false,
85     /**
86      * @cfg {String} order
87      * Used to set the order in which elements are created (usefull for multiple tabs)
88      */
89     
90     order : false,
91     /**
92      * @cfg {String} name
93      * String to display while loading.
94      */
95     name : false,
96     /**
97      * @cfg {Array} items
98      * A single item array - the first element is the root of the tree..
99      * It's done this way to stay compatible with the Xtype system...
100      */
101     items : false,
102 });
103
104 Roo.apply(Roo.XComponent, 
105     /**
106      * @property  buildCompleted
107      * True when the builder has completed building the interface.
108      * @type Boolean
109      */
110     buildCompleted : false,
111      
112     /**
113      * @property  topModule
114      * the upper most module - uses document.element as it's constructor.
115      * @type Object
116      */
117      
118     topModule  : false,
119       
120     /**
121      * @property  modules
122      * array of modules to be created by registration system.
123      * @type Roo.XComponent
124      */
125     
126     modules : [],
127       
128     
129     /**
130      * Register components to be built later.
131      * @param {Object} details about module
132      *
133      * This solves the following issues
134      * - Building is not done on page load, but after an authentication process has occured.
135      * - Interface elements are registered on page load
136      * - Parent Interface elements may not be loaded before child, so this handles that..
137      * 
138      *
139      * example:
140      * 
141      * MyApp.register({
142           order : '000001',
143           module : 'Pman.Tab.projectMgr',
144           region : 'center',
145           parent : 'Pman.layout',
146           disabled : false,  // or use a function..
147         })
148      * 
149      */
150     register : function(obj) {
151         this.modules.push(obj);
152          
153     },
154     /**
155      * convert a string to an object..
156      * 
157      */
158     
159     toObject : function(str)
160     {
161         if (typeof(str) == 'object') {
162             return str;
163         }
164         var ar = str.split('.');
165         var rt, o;
166         rt = ar.unshift();
167             /** eval:var:o */
168         eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
169         if (o === false) {
170             throw "Module not found : " + str;
171         }
172         Roo.each(ar, function(e) {
173             if (typeof(o[e]) == 'undefined') {
174                 throw "Module not found : " + str;
175             }
176             o = o[e];
177         });
178         return o;
179         
180     }
181     
182     
183     /**
184      * move modules into their correct place in the tree..
185      * 
186      */
187     preBuild : function ()
188     {
189         var modules = this.modules;
190         this.modules = false;
191         
192         Roo.each(modules , function (obj)
193         {
194             obj.parent = this.toObject(obj.parent);
195             
196             if (!obj.parent) {
197                 this.topModule = obj;
198                 return;
199             }
200             obj.parent = toObject(obj.parent);
201             if (!obj.parent.modules) {
202                 obj.parent.modules = new Roo.util.MixedCollection(false, 
203                     function(o) { return o.order + '' }
204                 );
205             }
206             
207             obj.parent.modules.add(obj);
208         }, this);
209     }
210     
211      /**
212      * make a list of modules to build.
213      * @return {Array} list of modules. 
214      */ 
215     
216     buildOrder : function()
217     {
218         var _this = this;
219         var cmp = function(a,b) {   
220             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
221         };
222         
223         if (!this.topModule || !this.topModule.modules) {
224             throw "No top level modules to build";
225         }
226        
227         // make a flat list in order of modules to build.
228         var mods = [ this.topModule ];
229         
230         
231         // add modules to their parents..
232         var addMod = function(m) {
233            // console.log(m.modKey);
234             
235             mods.push(m);
236             if (m.module.modules) {
237                 m.module.modules.keySort('ASC',  cmp );
238                 m.module.modules.each(addMod);
239             }
240             if (m.finalize) {
241                 m.finalize.name = m.name + " (clean up) ";
242                 mods.push(m.finalize);
243             }
244             
245         }
246         this.topModule.modules.keySort('ASC',  cmp );
247         this.topModule.modules.each(addMod);
248     }
249     
250      /**
251      * Build the registered modules.
252      * @param {Object} parent element.
253      * @param {Function} optional method to call after module has been added.
254      * 
255      */ 
256    
257     build : function() 
258     {
259         
260         this.preBuild();
261         var mods = this.buildOrder();
262         
263         //this.allmods = mods;
264         //console.log(mods);
265         //return;
266         if (!mods.length) { // should not happen
267             throw "NO modules!!!";
268         }
269         
270         
271         
272         // flash it up as modal - so we store the mask!?
273         Roo.MessageBox.show({ title: 'loading' });
274         Roo.MessageBox.show({
275            title: "Please wait...",
276            msg: "Building Interface...",
277            width:450,
278            progress:true,
279            closable:false,
280            modal: false
281           
282         });
283         var n = -1;
284         var _this = this;
285         var progressRun = function() {
286             n++;
287             if (n >= mods.length) {
288                 Roo.MessageBox.hide();
289                 _this.topModule.fireEvent('built', _this.topModule);
290                 return;
291             }
292             
293             var m = mods[n];
294             
295             
296             Roo.MessageBox.updateProgress(
297                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
298                     " of " + mods.length + 
299                     (m.name ? (' - ' + m.name) : '')
300                     );
301             
302             if (typeof(m) == 'function') {
303                 m.call(this);
304                 progressRun.defer(10, _this);    
305                 return;
306             } 
307             var disabled = (typeof(m.module.disabled) == 'function') ?
308                 m.module.disabled.call(m.module.disabled) : m.module.disabled;
309                 
310             }
311             
312             if (m.parent.layout && !disabled) {
313                 // modules have to support a  'add method'
314                 // should we just move that code into here..
315                 m.module.add(m.parent.layout, m.region);    
316             }
317                  
318              
319             
320         }
321         progressRun.defer(1, _this);
322      
323         
324         
325     }
326      
327    
328     
329     
330 });
331