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