Roo/Document.js
[roojs1] / Roo / Document.js
1 /*
2  * Original code for Roojs - LGPL
3  * <script type="text/javascript">
4  */
5  
6 /**
7  * @class Roo.Documents
8  * @extends Roo.data.Observable
9  * 
10  * Document and interface builder class..
11  * 
12  * MyApp = new Roo.Document({
13  *     loadingIndicator : 'loading',
14  *     listeners :  Roo.Login.onLoad();
15  * });
16  * 
17  * MyApp.on('beforeload', function() {
18  *      MyApp.register()
19  
20  * 
21  * 
22  * 
23  */
24 Roo.Document = function(cfg) {
25      
26     this.addEvents({ 
27         'ready' : true,
28         'beforeload' : true, // fired after page ready, before module building.
29         'load' : true, // fired after module building
30         'authrefreshed' : true // fire on auth updated?? - should be on Login?!?!?
31         
32     });
33     this.modules = [];
34     Roo.util.Observable.call(this,cfg);
35     var _this = this;
36     Roo.onReady(function() {
37         _this.fireEvent('ready');
38     },null,false);
39     
40     this.on('ready', onReady, this);
41     this.on('load', onLoad, this);  
42     
43     
44 }
45 Roo.extend(Roo.Document, Roo.util.Observable, {
46     /**
47      * @property  buildCompleted
48      * True when the builder has completed building the interface.
49      * @type Boolean
50      */
51     buildCompleted : false,
52      /**
53      * @property  loadingIndicator
54      * ID of loading indictor element.
55      * @type String
56      */
57     loadingIndicator : false,
58     /**
59      * @property  modules
60      * array of modules to be created by registration system.
61      * @type Array
62      */
63     
64     modules : false,
65       
66     // protected - on Ready handler.
67     onReady :   function()
68     {
69         // kludge to fix firebug debugger
70         if (typeof(console) == 'undefined') {
71             console = { log : function() {  } };
72         }
73          
74         // init cookies..
75         Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
76             
77         // link errors...
78          
79             
80     },
81      
82     
83     
84     /**
85      * Register components to be built later.
86      * @param {Object} details about module
87      *
88      * This solves the following issues
89      * - Building is not done on page load, but after an authentication process has occured.
90      * - Interface elements are registered on page load
91      * - Parent Interface elements may not be loaded before child, so this handles that..
92      * 
93      *
94      * example:
95      * 
96      * MyApp.register({
97           order : '000001',
98           module : 'Pman.Tab.projectMgr',
99           region : 'center',
100           parent : 'Pman.layout'
101         })
102      * 
103      */
104     register : function(obj) {
105         this.modules.push(obj);
106         
107         
108     },
109     
110     
111     
112     /**
113      * move modules into their correct place in the tree..
114      * 
115      */
116     preBuild : function ()
117     {
118         var modules = this.modules;
119         this.modules = false;
120         
121         function toObject(str)
122         {
123             if (typeof(str) == 'object') {
124                 return str;
125             }
126             var ar = str.split('.');
127             var rt, o;
128             rt = ar.unshift();
129                 /** eval:var:o */
130             eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
131             if (o === false) {
132                 throw "Module not found : " + str;
133             }
134             Roo.each(ar, function(e) {
135                 if (typeof(o[e]) == 'undefined') {
136                     throw "Module not found : " + str;
137                 }
138                 o = o[e];
139             });
140             return o;
141             
142         }
143         
144         Roo.each(modules , function (obj)
145         {
146             
147             obj.parent = toObject(obj.parent);
148              
149             if (!obj.parent.modules) {
150                 obj.parent.modules = new Roo.util.MixedCollection(false, function(o) { return o.modKey });
151             }
152             
153             obj.parent.modules.add(obj);
154         }
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(parent, onComplete) 
166     {
167         
168         var _this = this;
169         var cmp = function(a,b) {   
170             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
171         };
172         
173         if (!parent.modules) {
174             return;
175         }
176         parent.modules.keySort('ASC',  cmp );
177         var mods = [];
178         
179         
180         // add modules to their parents..
181         var addMod = function(m) {
182            // console.log(m.modKey);
183             
184             mods.push(m);
185             if (m.module.modules) {
186                 m.module.modules.keySort('ASC',  cmp );
187                 m.module.modules.each(addMod);
188             }
189             if (m.finalize) {
190                 m.finalize.name = m.name + " (clean up) ";
191                 mods.push(m.finalize);
192             }
193             
194         }
195  
196         parent.modules.each(addMod);
197         //this.allmods = mods;
198         //console.log(mods);
199         //return;
200         if (!mods.length) {
201             if (onComplete) onComplete();
202             return;
203         }
204         // flash it up as modal - so we store the mask!?
205         Ext.MessageBox.show({ title: 'loading' });
206         Ext.MessageBox.show({
207            title: "Please wait...",
208            msg: "Building Interface...",
209            width:450,
210            progress:true,
211            closable:false,
212            modal: false
213           
214         });
215         var n = 0;
216         var progressRun = function() {
217             
218             var m = mods[n];
219             
220             
221             Ext.MessageBox.updateProgress(
222                 (n+1)/mods.length,  "Building Interface " + (n+1) + 
223                     " of " + mods.length + 
224                     (m.name ? (' - ' + m.name) : '')
225                     );
226             
227             
228             
229             if (typeof(m) == 'function') {
230                 m();
231                 
232             } else {
233                 if (m.parent.layout && !m.module.disabled) {
234                     m.module.add(m.parent.layout, m.region);    
235                 }
236                 
237             }
238             
239             
240             n++;
241             if (n >= mods.length) {
242                 onComplete();  
243                 return;
244             }
245                 
246             
247             progressRun.defer(10, Pman);    
248         }
249         progressRun.defer(1, Pman);
250      
251         
252         
253     },
254     
255 },
256    
257     
258     
259 });
260