Roo/bootstrap/Component.js
[roojs1] / Roo / bootstrap / Component.js
1 /*
2  * - LGPL
3  *
4  * base class for bootstrap elements.
5  * 
6  */
7
8 Roo.bootstrap = Roo.bootstrap || {};
9 /**
10  * @class Roo.bootstrap.Component
11  * @extends Roo.Component
12  * Bootstrap Component base class
13  * @cfg {String} cls css class
14  * @cfg {String} style any extra css
15  * @cfg {Object} xattr extra attributes to add to 'element' (used by builder to store stuff.)
16  * @cfg {Boolean} can_build_overlaid  True if element can be rebuild from a HTML page
17  * @cfg {string} dataId cutomer id
18  * @cfg {string} name Specifies name attribute
19  * @cfg {string} tooltip  Text for the tooltip
20  * @cfg {string} container_method method to fetch parents container element (used by NavHeaderbar -  getHeaderChildContainer)
21  * 
22  * @constructor
23  * Do not use directly - it does not do anything..
24  * @param {Object} config The config object
25  */
26
27
28
29 Roo.bootstrap.Component = function(config){
30     Roo.bootstrap.Component.superclass.constructor.call(this, config);
31        
32     this.addEvents({
33         /**
34          * @event childrenrendered
35          * Fires when the children have been rendered..
36          * @param {Roo.bootstrap.Component} this
37          */
38         "childrenrendered" : true
39         
40         
41         
42     });
43     
44     
45 };
46
47 Roo.extend(Roo.bootstrap.Component, Roo.BoxComponent,  {
48     
49     
50     allowDomMove : false, // to stop relocations in parent onRender...
51     
52     cls : false,
53     
54     style : false,
55     
56     autoCreate : false,
57     
58     tooltip : null,
59     /**
60      * Initialize Events for the element
61      */
62     initEvents : function() { },
63     
64     xattr : false,
65     
66     parentId : false,
67     
68     can_build_overlaid : true,
69     
70     container_method : false,
71     
72     dataId : false,
73     
74     name : false,
75     
76     parent: function() {
77         // returns the parent component..
78         return Roo.ComponentMgr.get(this.parentId)
79         
80         
81     },
82     
83     // private
84     onRender : function(ct, position)
85     {
86        // Roo.log("Call onRender: " + this.xtype);
87         
88         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
89         
90         if(this.el){
91             if (this.el.attr('xtype')) {
92                 this.el.attr('xtypex', this.el.attr('xtype'));
93                 this.el.dom.removeAttribute('xtype');
94                 
95                 this.initEvents();
96             }
97             
98             return;
99         }
100         
101          
102         
103         var cfg = Roo.apply({},  this.getAutoCreate());
104         cfg.id = this.id || Roo.id();
105         
106         // fill in the extra attributes 
107         if (this.xattr && typeof(this.xattr) =='object') {
108             for (var i in this.xattr) {
109                 cfg[i] = this.xattr[i];
110             }
111         }
112         
113         if(this.dataId){
114             cfg.dataId = this.dataId;
115         }
116         
117         if (this.cls) {
118             cfg.cls = (typeof(cfg.cls) == 'undefined') ? this.cls : cfg.cls + ' ' + this.cls;
119         }
120         
121         if (this.style) { // fixme needs to support more complex style data.
122             cfg.style = this.style;
123         }
124         
125         if(this.name){
126             cfg.name = this.name;
127         }
128         
129         this.el = ct.createChild(cfg, position);
130         
131         Roo.log(this);
132         
133         if (this.tooltip) {
134             Roo.log(this.tooltip);
135             this.tooltipEl().attr('tooltip', this.tooltip);
136         }
137         
138         if(this.tabIndex !== undefined){
139             this.el.dom.setAttribute('tabIndex', this.tabIndex);
140         }
141         this.initEvents();
142         
143         
144     },
145     /**
146      * Fetch the element to add children to
147      * @return {Roo.Element} defaults to this.el
148      */
149     getChildContainer : function()
150     {
151         return this.el;
152     },
153     /**
154      * Fetch the element to display the tooltip on.
155      * @return {Roo.Element} defaults to this.el
156      */
157     tooltipEl : function()
158     {
159         Roo.log('tooltipEl!!!!!!!!!!!!!!');
160         Roo.log(this.el);
161         return this.el;
162     },
163         
164     addxtype  : function(tree,cntr)
165     {
166         var cn = this;
167         
168         cn = Roo.factory(tree);
169            
170         cn.parentType = this.xtype; //??
171         cn.parentId = this.id;
172         
173         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
174         if (typeof(cn.container_method) == 'string') {
175             cntr = cn.container_method;
176         }
177         
178         
179         var has_flexy_each =  (typeof(tree['flexy:foreach']) != 'undefined');
180         
181         var has_flexy_if =  (typeof(tree['flexy:if']) != 'undefined');
182         
183         var build_from_html =  Roo.XComponent.build_from_html;
184           
185         var is_body  = (tree.xtype == 'Body') ;
186           
187         var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
188           
189         var self_cntr_el = Roo.get(this[cntr](false));
190         
191         // do not try and build conditional elements 
192         if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
193             return false;
194         }
195         
196         if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
197             if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
198                 return this.addxtypeChild(tree,cntr);
199             }
200             
201             var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
202                 
203             if(echild){
204                 return this.addxtypeChild(Roo.apply({}, tree),cntr);
205             }
206             
207             Roo.log('skipping render');
208             return cn;
209             
210         }
211         
212         var ret = false;
213         if (!build_from_html) {
214             return false;
215         }
216         
217         // this i think handles overlaying multiple children of the same type
218         // with the sam eelement.. - which might be buggy..
219         while (true) {
220             var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
221             
222             if (!echild) {
223                 break;
224             }
225             
226             if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
227                 break;
228             }
229             
230             ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
231         }
232         return ret;
233     },
234     
235     addxtypeChild : function (tree, cntr)
236     {
237         Roo.debug && Roo.log('addxtypeChild:' + cntr);
238         var cn = this;
239         cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
240         
241         
242         var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
243                     (typeof(tree['flexy:foreach']) != 'undefined');
244           
245         
246         
247          skip_children = false;
248         // render the element if it's not BODY.
249         if (tree.xtype != 'Body') {
250            
251             cn = Roo.factory(tree);
252            
253             cn.parentType = this.xtype; //??
254             cn.parentId = this.id;
255             
256             var build_from_html =  Roo.XComponent.build_from_html;
257             
258             
259             // does the container contain child eleemnts with 'xtype' attributes.
260             // that match this xtype..
261             // note - when we render we create these as well..
262             // so we should check to see if body has xtype set.
263             if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
264                
265                 var self_cntr_el = Roo.get(this[cntr](false));
266                 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
267                 if (echild) { 
268                     //Roo.log(Roo.XComponent.build_from_html);
269                     //Roo.log("got echild:");
270                     //Roo.log(echild);
271                 }
272                 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
273                 // and are not displayed -this causes this to use up the wrong element when matching.
274                 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
275                 
276                 
277                 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
278                   //  Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
279                   
280                   
281                   
282                     cn.el = echild;
283                   //  Roo.log("GOT");
284                     //echild.dom.removeAttribute('xtype');
285                 } else {
286                     Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
287                     Roo.debug && Roo.log(self_cntr_el);
288                     Roo.debug && Roo.log(echild);
289                     Roo.debug && Roo.log(cn);
290                 }
291             }
292            
293             
294            
295             // if object has flexy:if - then it may or may not be rendered.
296             if (build_from_html && has_flexy && !cn.el &&  cn.can_build_overlaid) {
297                 // skip a flexy if element.
298                 Roo.debug && Roo.log('skipping render');
299                 Roo.debug && Roo.log(tree);
300                 if (!cn.el) {
301                     Roo.debug && Roo.log('skipping all children');
302                     skip_children = true;
303                 }
304                 
305              } else {
306                  
307                 // actually if flexy:foreach is found, we really want to create 
308                 // multiple copies here...
309                 //Roo.log('render');
310                 //Roo.log(this[cntr]());
311                 cn.render(this[cntr](true));
312              }
313             // then add the element..
314         }
315         
316         
317         // handle the kids..
318         
319         var nitems = [];
320         /*
321         if (typeof (tree.menu) != 'undefined') {
322             tree.menu.parentType = cn.xtype;
323             tree.menu.triggerEl = cn.el;
324             nitems.push(cn.addxtype(Roo.apply({}, tree.menu)));
325             
326         }
327         */
328         if (!tree.items || !tree.items.length) {
329             cn.items = nitems;
330             return cn;
331         }
332         var items = tree.items;
333         delete tree.items;
334         
335         //Roo.log(items.length);
336             // add the items..
337         if (!skip_children) {    
338             for(var i =0;i < items.length;i++) {
339                 nitems.push(cn.addxtype(Roo.apply({}, items[i])));
340             }
341         }
342         
343         cn.items = nitems;
344         
345         this.fireEvent('childrenrendered', this);
346         
347         return cn;
348     },
349     /**
350      * Show a component - removes 'hidden' class
351      */
352     show : function()
353     {
354         if (this.el) {
355             this.el.removeClass('hidden');
356         }
357     },
358     /**
359      * Hide a component - adds 'hidden' class
360      */
361     hide: function()
362     {
363         if (this.el && !this.el.hasClass('hidden')) {
364             this.el.addClass('hidden');
365         }
366         
367     }
368 });
369
370