4 * base class for bootstrap elements.
8 Roo.bootstrap = Roo.bootstrap || {};
10 * @class Roo.bootstrap.Component
11 * @extends Roo.Component
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())
26 * Do not use directly - it does not do anything..
27 * @param {Object} config The config object
32 Roo.bootstrap.Component = function(config){
33 console.log("BOOSTRAP COMPONENT CONSTRUCTOR");
35 Roo.bootstrap.Component.superclass.constructor.call(this, config);
39 * @event childrenrendered
40 * Fires when the children have been rendered..
41 * @param {Roo.bootstrap.Component} this
43 "childrenrendered" : true
52 Roo.extend(Roo.bootstrap.Component, Roo.BoxComponent, {
55 allowDomMove : false, // to stop relocations in parent onRender...
65 * Initialize Events for the element
67 initEvents : function() { },
73 can_build_overlaid : true,
75 container_method : false,
82 // returns the parent component..
83 return Roo.ComponentMgr.get(this.parentId)
89 onRender : function(ct, position)
91 // Roo.log("Call onRender: " + this.xtype);
93 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
96 if (this.el.attr('xtype')) {
97 this.el.attr('xtypex', this.el.attr('xtype'));
98 this.el.dom.removeAttribute('xtype');
108 var cfg = Roo.apply({}, this.getAutoCreate());
110 cfg.id = this.id || Roo.id();
112 // fill in the extra attributes
113 if (this.xattr && typeof(this.xattr) =='object') {
114 for (var i in this.xattr) {
115 cfg[i] = this.xattr[i];
120 cfg.dataId = this.dataId;
124 cfg.cls = (typeof(cfg.cls) == 'undefined' ? this.cls : cfg.cls) + ' ' + this.cls;
127 if (this.style) { // fixme needs to support more complex style data.
128 cfg.style = (typeof(cfg.style) == 'undefined' ? this.style : cfg.style) + '; ' + this.style;
132 cfg.name = this.name;
135 this.el = ct.createChild(cfg, position);
138 this.tooltipEl().attr('tooltip', this.tooltip);
141 if(this.tabIndex !== undefined){
142 this.el.dom.setAttribute('tabIndex', this.tabIndex);
149 * Fetch the element to add children to
150 * @return {Roo.Element} defaults to this.el
152 getChildContainer : function()
156 getDocumentBody : function() // used by menus - as they are attached to the body so zIndexes work
158 return Roo.get(document.body);
162 * Fetch the element to display the tooltip on.
163 * @return {Roo.Element} defaults to this.el
165 tooltipEl : function()
170 addxtype : function(tree,cntr)
174 cn = Roo.factory(tree);
175 //Roo.log(['addxtype', cn]);
177 cn.parentType = this.xtype; //??
178 cn.parentId = this.id;
180 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
181 if (typeof(cn.container_method) == 'string') {
182 cntr = cn.container_method;
186 var has_flexy_each = (typeof(tree['flexy:foreach']) != 'undefined');
188 var has_flexy_if = (typeof(tree['flexy:if']) != 'undefined');
190 var build_from_html = Roo.XComponent.build_from_html;
192 var is_body = (tree.xtype == 'Body') ;
194 var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
196 var self_cntr_el = Roo.get(this[cntr](false));
198 // do not try and build conditional elements
199 if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
203 if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
204 if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
205 return this.addxtypeChild(tree,cntr, is_body);
208 var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
211 return this.addxtypeChild(Roo.apply({}, tree),cntr);
214 Roo.log('skipping render');
220 if (!build_from_html) {
224 // this i think handles overlaying multiple children of the same type
225 // with the sam eelement.. - which might be buggy..
227 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
233 if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
237 ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
244 addxtypeChild : function (tree, cntr, is_body)
246 Roo.debug && Roo.log('addxtypeChild:' + cntr);
248 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
251 var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
252 (typeof(tree['flexy:foreach']) != 'undefined');
256 skip_children = false;
257 // render the element if it's not BODY.
260 // if parent was disabled, then do not try and create the children..
261 if(!this[cntr](true)){
266 cn = Roo.factory(tree);
268 cn.parentType = this.xtype; //??
269 cn.parentId = this.id;
271 var build_from_html = Roo.XComponent.build_from_html;
274 // does the container contain child eleemnts with 'xtype' attributes.
275 // that match this xtype..
276 // note - when we render we create these as well..
277 // so we should check to see if body has xtype set.
278 if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
280 var self_cntr_el = Roo.get(this[cntr](false));
281 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
283 //Roo.log(Roo.XComponent.build_from_html);
284 //Roo.log("got echild:");
287 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
288 // and are not displayed -this causes this to use up the wrong element when matching.
289 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
292 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
293 // Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
299 //echild.dom.removeAttribute('xtype');
301 Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
302 Roo.debug && Roo.log(self_cntr_el);
303 Roo.debug && Roo.log(echild);
304 Roo.debug && Roo.log(cn);
310 // if object has flexy:if - then it may or may not be rendered.
311 if (build_from_html && has_flexy && !cn.el && cn.can_build_overlaid) {
312 // skip a flexy if element.
313 Roo.debug && Roo.log('skipping render');
314 Roo.debug && Roo.log(tree);
316 Roo.debug && Roo.log('skipping all children');
317 skip_children = true;
322 // actually if flexy:foreach is found, we really want to create
323 // multiple copies here...
325 //Roo.log(this[cntr]());
326 // some elements do not have render methods.. like the layouts...
328 if(this[cntr](true) === false){
333 cn.render && cn.render(this[cntr](true));
336 // then add the element..
343 if (typeof (tree.menu) != 'undefined') {
344 tree.menu.parentType = cn.xtype;
345 tree.menu.triggerEl = cn.el;
346 nitems.push(cn.addxtype(Roo.apply({}, tree.menu)));
350 if (!tree.items || !tree.items.length) {
352 //Roo.log(["no children", this]);
357 var items = tree.items;
360 //Roo.log(items.length);
362 if (!skip_children) {
363 for(var i =0;i < items.length;i++) {
364 // Roo.log(['add child', items[i]]);
365 nitems.push(cn.addxtype(Roo.apply({}, items[i])));
371 //Roo.log("fire childrenrendered");
373 cn.fireEvent('childrenrendered', this);
379 * Set the element that will be used to show or hide
381 setVisibilityEl : function(el)
383 this.visibilityEl = el;
387 * Get the element that will be used to show or hide
389 getVisibilityEl : function()
391 if (typeof(this.visibilityEl) == 'object') {
392 return this.visibilityEl;
395 if (typeof(this.visibilityEl) == 'string') {
396 return this.visibilityEl == 'parent' ? this.parent().getEl() : this.getEl();
403 * Show a component - removes 'hidden' class
407 if(!this.getVisibilityEl()){
411 this.getVisibilityEl().removeClass(['hidden','d-none']);
413 this.fireEvent('show', this);
418 * Hide a component - adds 'hidden' class
422 if(!this.getVisibilityEl()){
426 this.getVisibilityEl().addClass(['hidden','d-none']);
428 this.fireEvent('hide', this);