colorbrewer/colorbrewer-bg.css
[roojs1] / Roo / XComponent.js
1 /*
2  * Original code for Roojs - LGPL
3  * <script type="text/javascript">
4  */
5  
6 /**
7  * @class Roo.XComponent
8  * A delayed Element creator...
9  * Or a way to group chunks of interface together.
10  * 
11  * Mypart.xyx = new Roo.XComponent({
12
13     parent : 'Mypart.xyz', // empty == document.element.!!
14     order : '001',
15     name : 'xxxx'
16     region : 'xxxx'
17     disabled : function() {} 
18      
19     tree : function() { // return an tree of xtype declared components
20         var MODULE = this;
21         return 
22         {
23             xtype : 'NestedLayoutPanel',
24             // technicall
25         }
26      ]
27  *})
28  *
29  *
30  * It can be used to build a big heiracy, with parent etc.
31  * or you can just use this to render a single compoent to a dom element
32  * MYPART.render(Roo.Element | String(id) | dom_element )
33  * 
34  * @extends Roo.util.Observable
35  * @constructor
36  * @param cfg {Object} configuration of component
37  * 
38  */
39 Roo.XComponent = function(cfg) {
40     Roo.apply(this, cfg);
41     this.addEvents({ 
42         /**
43              * @event built
44              * Fires when this the componnt is built
45              * @param {Roo.XComponent} c the component
46              */
47         'built' : true,
48         /**
49              * @event buildcomplete
50              * Fires on the top level element when all elements have been built
51              * @param {Roo.XComponent} c the top level component.
52          */
53         'buildcomplete' : true
54         
55     });
56     this.region = this.region || 'center'; // default..
57     Roo.XComponent.register(this);
58     this.modules = false;
59     this.el = false; // where the layout goes..
60     
61     
62 }
63 Roo.extend(Roo.XComponent, Roo.util.Observable, {
64     /**
65      * @property el
66      * The created element (with Roo.factory())
67      * @type {Roo.Layout}
68      */
69     el  : false,
70     
71     /**
72      * @property el
73      * for BC  - use el in new code
74      * @type {Roo.Layout}
75      */
76     panel : false,
77     
78     /**
79      * @property layout
80      * for BC  - use el in new code
81      * @type {Roo.Layout}
82      */
83     layout : false,
84     
85      /**
86      * @cfg {Function|boolean} disabled
87      * If this module is disabled by some rule, return true from the funtion
88      */
89     disabled : false,
90     
91     /**
92      * @cfg {String} parent 
93      * Name of parent element which it get xtype added to..
94      */
95     parent: false,
96     
97     /**
98      * @cfg {String} order
99      * Used to set the order in which elements are created (usefull for multiple tabs)
100      */
101     
102     order : false,
103     /**
104      * @cfg {String} name
105      * String to display while loading.
106      */
107     name : false,
108     /**
109      * @cfg {String} region
110      * Region to render component to (defaults to center)
111      */
112     region : 'center',
113     
114     /**
115      * @cfg {Array} items
116      * A single item array - the first element is the root of the tree..
117      * It's done this way to stay compatible with the Xtype system...
118      */
119     items : false,
120     
121     
122      /**
123      * render
124      * render element to dom or tree
125      * @param {Roo.Element|String|DomElement} optional render to if parent is not set.
126      */
127     
128     render : function(el)
129     {
130         
131         el = el || false;
132         var hp = this.parent ? 1 : 0;
133         
134         if (!el && typeof(this.parent) == 'string' && this.parent.substring(0,1) == '#') {
135             // if parent is a '#.....' string, then let's use that..
136             var ename = this.parent.substr(1)
137             this.parent = false;
138             el = Roo.get(ename);
139             if (!el) {
140                 Roo.log("Warning - element can not be found :#" + ename );
141                 return;
142             }
143         }
144         
145         
146         if (!this.parent) {
147             
148             el = el ? Roo.get(el) : false;
149             
150             // it's a top level one..
151             this.parent =  {
152                 el : new Roo.BorderLayout(el || document.body, {
153                 
154                      center: {
155                          titlebar: false,
156                          autoScroll:false,
157                          closeOnTab: true,
158                          tabPosition: 'top',
159                           //resizeTabs: true,
160                          alwaysShowTabs: el && hp? false :  true,
161                          hideTabs: el || !hp ? true :  false,
162                          minTabWidth: 140
163                      }
164                  })
165             }
166         }
167         
168         
169             
170         var tree = this.tree();
171         tree.region = tree.region || this.region;
172         this.el = this.parent.el.addxtype(tree);
173         this.fireEvent('built', this);
174         
175         this.panel = this.el;
176         this.layout = this.panel.layout;    
177          
178     }
179     
180 });
181
182 Roo.apply(Roo.XComponent, {
183     
184     /**
185      * @property  buildCompleted
186      * True when the builder has completed building the interface.
187      * @type Boolean
188      */
189     buildCompleted : false,
190      
191     /**
192      * @property  topModule
193      * the upper most module - uses document.element as it's constructor.
194      * @type Object
195      */
196      
197     topModule  : false,
198       
199     /**
200      * @property  modules
201      * array of modules to be created by registration system.
202      * @type {Array} of Roo.XComponent
203      */
204     
205     modules : [],
206     /**
207      * @property  elmodules
208      * array of modules to be created by which use #ID 
209      * @type {Array} of Roo.XComponent
210      */
211      
212     elmodules : [],
213
214     
215     /**
216      * Register components to be built later.
217      *
218      * This solves the following issues
219      * - Building is not done on page load, but after an authentication process has occured.
220      * - Interface elements are registered on page load
221      * - Parent Interface elements may not be loaded before child, so this handles that..
222      * 
223      *
224      * example:
225      * 
226      * MyApp.register({
227           order : '000001',
228           module : 'Pman.Tab.projectMgr',
229           region : 'center',
230           parent : 'Pman.layout',
231           disabled : false,  // or use a function..
232         })
233      
234      * * @param {Object} details about module
235      */
236     register : function(obj) {
237         this.modules.push(obj);
238          
239     },
240     /**
241      * convert a string to an object..
242      * eg. 'AAA.BBB' -> finds AAA.BBB
243
244      */
245     
246     toObject : function(str)
247     {
248         if (!str || typeof(str) == 'object') {
249             return str;
250         }
251         if (str.substring(0,1) == '#') {
252             return str;
253         }
254
255         var ar = str.split('.');
256         var rt, o;
257         rt = ar.shift();
258             /** eval:var:o */
259         try {
260             eval('if (typeof ' + rt + ' == "undefined"){ o = false;} o = ' + rt + ';');
261         } catch (e) {
262             throw "Module not found : " + str;
263         }
264         
265         if (o === false) {
266             throw "Module not found : " + str;
267         }
268         Roo.each(ar, function(e) {
269             if (typeof(o[e]) == 'undefined') {
270                 throw "Module not found : " + str;
271             }
272             o = o[e];
273         });
274         
275         return o;
276         
277     },
278     
279     
280     /**
281      * move modules into their correct place in the tree..
282      * 
283      */
284     preBuild : function ()
285     {
286         var _t = this;
287         Roo.each(this.modules , function (obj)
288         {
289             var opar = obj.parent;
290             try { 
291                 obj.parent = this.toObject(opar);
292             } catch(e) {
293                 Roo.log(e.toString());
294                 return;
295             }
296             
297             if (!obj.parent) {
298                 this.topModule = obj;
299                 return;
300             }
301             if (typeof(obj.parent) == 'string') {
302                 this.elmodules.push(obj);
303                 return;
304             }
305             if (obj.parent.constructor != Roo.XComponent) {
306                 Roo.log("Object Parent is not instance of XComponent:" + obj.name)
307             }
308             if (!obj.parent.modules) {
309                 obj.parent.modules = new Roo.util.MixedCollection(false, 
310                     function(o) { return o.order + '' }
311                 );
312             }
313             
314             obj.parent.modules.add(obj);
315         }, this);
316     },
317     
318      /**
319      * make a list of modules to build.
320      * @return {Array} list of modules. 
321      */ 
322     
323     buildOrder : function()
324     {
325         var _this = this;
326         var cmp = function(a,b) {   
327             return String(a).toUpperCase() > String(b).toUpperCase() ? 1 : -1;
328         };
329         if ((!this.topModule || !this.topModule.modules) && !this.elmodules.length) {
330             throw "No top level modules to build";
331         }
332         
333         // make a flat list in order of modules to build.
334         var mods = this.topModule ? [ this.topModule ] : [];
335         Roo.each(this.elmodules,function(e) { mods.push(e) });
336
337         
338         // add modules to their parents..
339         var addMod = function(m) {
340            // Roo.debug && Roo.log(m.modKey);
341             
342             mods.push(m);
343             if (m.modules) {
344                 m.modules.keySort('ASC',  cmp );
345                 m.modules.each(addMod);
346             }
347             // not sure if this is used any more..
348             if (m.finalize) {
349                 m.finalize.name = m.name + " (clean up) ";
350                 mods.push(m.finalize);
351             }
352             
353         }
354         if (this.topModule) { 
355             this.topModule.modules.keySort('ASC',  cmp );
356             this.topModule.modules.each(addMod);
357         }
358         return mods;
359     },
360     
361      /**
362      * Build the registered modules.
363      * @param {Object} parent element.
364      * @param {Function} optional method to call after module has been added.
365      * 
366      */ 
367    
368     build : function() 
369     {
370         
371         this.preBuild();
372         var mods = this.buildOrder();
373       
374         //this.allmods = mods;
375         //Roo.debug && Roo.log(mods);
376         //return;
377         if (!mods.length) { // should not happen
378             throw "NO modules!!!";
379         }
380         
381         
382         
383         // flash it up as modal - so we store the mask!?
384         Roo.MessageBox.show({ title: 'loading' });
385         Roo.MessageBox.show({
386            title: "Please wait...",
387            msg: "Building Interface...",
388            width:450,
389            progress:true,
390            closable:false,
391            modal: false
392           
393         });
394         var total = mods.length;
395         
396         var _this = this;
397         var progressRun = function() {
398             if (!mods.length) {
399                 Roo.debug && Roo.log('hide?');
400                 Roo.MessageBox.hide();
401                 if (_this.topModule) { 
402                     _this.topModule.fireEvent('buildcomplete', _this.topModule);
403                 }
404                 // THE END...
405                 return false;   
406             }
407             
408             var m = mods.shift();
409             
410             
411             Roo.debug && Roo.log(m);
412             // not sure if this is supported any more.. - modules that are are just function
413             if (typeof(m) == 'function') { 
414                 m.call(this);
415                 return progressRun.defer(10, _this);
416             } 
417             
418             
419             
420             Roo.MessageBox.updateProgress(
421                 (total  - mods.length)/total,  "Building Interface " + (total  - mods.length) + 
422                     " of " + total + 
423                     (m.name ? (' - ' + m.name) : '')
424                     );
425             
426          
427             // is the module disabled?
428             var disabled = (typeof(m.disabled) == 'function') ?
429                 m.disabled.call(m.module.disabled) : m.disabled;    
430             
431             
432             if (disabled) {
433                 return progressRun(); // we do not update the display!
434             }
435             
436             // now build 
437             
438             m.render();
439             // it's 10 on top level, and 1 on others??? why...
440             return progressRun.defer(10, _this);
441              
442         }
443         progressRun.defer(1, _this);
444      
445         
446         
447     }
448     
449      
450    
451     
452     
453 });
454