4 * base class for bootstrap elements.
8 Roo.bootstrap = Roo.bootstrap || {};
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 * @cfg {string|object} visibilityEl (el|parent) What element to use for visibility (@see getVisibilityEl())
24 * Do not use directly - it does not do anything..
25 * @param {Object} config The config object
30 Roo.bootstrap.Component = function(config){
31 Roo.bootstrap.Component.superclass.constructor.call(this, config);
35 * @event childrenrendered
36 * Fires when the children have been rendered..
37 * @param {Roo.bootstrap.Component} this
39 "childrenrendered" : true
48 Roo.extend(Roo.bootstrap.Component, Roo.BoxComponent, {
51 allowDomMove : false, // to stop relocations in parent onRender...
61 * Initialize Events for the element
63 initEvents : function() { },
69 can_build_overlaid : true,
71 container_method : false,
78 // returns the parent component..
79 return Roo.ComponentMgr.get(this.parentId)
85 onRender : function(ct, position)
87 // Roo.log("Call onRender: " + this.xtype);
89 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
92 if (this.el.attr('xtype')) {
93 this.el.attr('xtypex', this.el.attr('xtype'));
94 this.el.dom.removeAttribute('xtype');
104 var cfg = Roo.apply({}, this.getAutoCreate());
106 cfg.id = this.id || Roo.id();
108 // fill in the extra attributes
109 if (this.xattr && typeof(this.xattr) =='object') {
110 for (var i in this.xattr) {
111 cfg[i] = this.xattr[i];
116 cfg.dataId = this.dataId;
120 cfg.cls = (typeof(cfg.cls) == 'undefined') ? this.cls : cfg.cls + ' ' + this.cls;
123 if (this.style) { // fixme needs to support more complex style data.
124 cfg.style = this.style;
128 cfg.name = this.name;
131 this.el = ct.createChild(cfg, position);
134 this.tooltipEl().attr('tooltip', this.tooltip);
137 if(this.tabIndex !== undefined){
138 this.el.dom.setAttribute('tabIndex', this.tabIndex);
145 * Fetch the element to add children to
146 * @return {Roo.Element} defaults to this.el
148 getChildContainer : function()
153 * Fetch the element to display the tooltip on.
154 * @return {Roo.Element} defaults to this.el
156 tooltipEl : function()
161 * This is really a wrapper for addxtypeChild
162 * it handles stuff relating to flexy:foreach / flexy:if
163 * = some of our projects use a flat rendering of the output, and try and overlay it with dynamic data.
164 * -- this is a bit of a nightmare... and is even more confusing to debug..
169 addxtype : function(tree,cntr)
173 cn = Roo.factory(tree);
174 //Roo.log(['addxtype', cn]);
176 cn.parentType = this.xtype; //??
177 cn.parentId = this.id;
179 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
180 if (typeof(cn.container_method) == 'string') {
181 cntr = cn.container_method;
185 var has_flexy_each = (typeof(tree['flexy:foreach']) != 'undefined');
187 var has_flexy_if = (typeof(tree['flexy:if']) != 'undefined');
189 var build_from_html = Roo.XComponent.build_from_html;
191 var is_body = (tree.xtype == 'Body') ;
193 var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
195 var self_cntr_el = Roo.get(this[cntr](false));
197 // do not try and build conditional elements
198 if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
202 if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
203 if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
204 return this.addxtypeChild(tree,cntr, is_body);
207 var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
210 return this.addxtypeChild(Roo.apply({}, tree),cntr);
213 Roo.log('skipping render');
219 if (!build_from_html) {
223 // this i think handles overlaying multiple children of the same type
224 // with the sam eelement.. - which might be buggy..
226 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
232 if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
236 ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
242 * add a child to this element
243 * - turn the child.cfg into a child_instance
244 * - call child_instance.render( this { getContainerMethod()} )
245 * - loop through the children, and call addxtype.. (reall this) on newly created child.
249 addxtypeChild : function (tree, cntr, is_body)
251 Roo.debug && Roo.log('addxtypeChild:' + cntr);
253 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
256 var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
257 (typeof(tree['flexy:foreach']) != 'undefined');
261 skip_children = false;
262 // render the element if it's not BODY.
265 // if parent was disabled, then do not try and create the children..
266 if(!this[cntr](true)){
271 cn = Roo.factory(tree);
273 cn.parentType = this.xtype; //??
274 cn.parentId = this.id;
276 var build_from_html = Roo.XComponent.build_from_html;
279 // does the container contain child eleemnts with 'xtype' attributes.
280 // that match this xtype..
281 // note - when we render we create these as well..
282 // so we should check to see if body has xtype set.
283 if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
285 var self_cntr_el = Roo.get(this[cntr](false));
286 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
288 //Roo.log(Roo.XComponent.build_from_html);
289 //Roo.log("got echild:");
292 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
293 // and are not displayed -this causes this to use up the wrong element when matching.
294 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
297 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
298 // Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
304 //echild.dom.removeAttribute('xtype');
306 Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
307 Roo.debug && Roo.log(self_cntr_el);
308 Roo.debug && Roo.log(echild);
309 Roo.debug && Roo.log(cn);
315 // if object has flexy:if - then it may or may not be rendered.
316 if (build_from_html && has_flexy && !cn.el && cn.can_build_overlaid) {
317 // skip a flexy if element.
318 Roo.debug && Roo.log('skipping render');
319 Roo.debug && Roo.log(tree);
321 Roo.debug && Roo.log('skipping all children');
322 skip_children = true;
327 // actually if flexy:foreach is found, we really want to create
328 // multiple copies here...
330 //Roo.log(this[cntr]());
331 // some elements do not have render methods.. like the layouts...
333 if(this[cntr](true) === false){
338 cn.render && cn.render(this[cntr](true));
341 // then add the element..
345 var items = tree.items;
348 cn.addxtypeChildren(items, skip_children);
353 * add a number of children to this object,
354 * which in turn calls render...
358 addxtypeChildren: function(child_array, skip_children)
361 if (!child_array || !child_array.length ) {
366 for(var i =0;i < child_array.length;i++) {
370 // Roo.log(['add child', items[i]]);
371 nitems.push(this.addxtype(Roo.apply({}, child_array[i])));
375 this.fireEvent('childrenrendered', this);
383 * Set the element that will be used to show or hide
385 setVisibilityEl : function(el)
387 this.visibilityEl = el;
391 * Get the element that will be used to show or hide
393 getVisibilityEl : function()
395 if (typeof(this.visibilityEl) == 'object') {
396 return this.visibilityEl;
399 if (typeof(this.visibilityEl) == 'string') {
400 return this.visibilityEl == 'parent' ? this.parent().getEl() : this.getEl();
407 * Show a component - removes 'hidden' class
411 if(!this.getVisibilityEl()){
415 this.getVisibilityEl().removeClass(['hidden','d-none']);
417 this.fireEvent('show', this);
422 * Hide a component - adds 'hidden' class
426 if(!this.getVisibilityEl()){
430 this.getVisibilityEl().addClass(['hidden','d-none']);
432 this.fireEvent('hide', this);