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);
135 this.tooltipEl().attr("data-placement='top'", this.tooltip);
138 if(this.tabIndex !== undefined){
139 this.el.dom.setAttribute('tabIndex', this.tabIndex);
146 * Fetch the element to add children to
147 * @return {Roo.Element} defaults to this.el
149 getChildContainer : function()
154 * Fetch the element to display the tooltip on.
155 * @return {Roo.Element} defaults to this.el
157 tooltipEl : function()
162 addxtype : function(tree,cntr)
166 cn = Roo.factory(tree);
167 //Roo.log(['addxtype', cn]);
169 cn.parentType = this.xtype; //??
170 cn.parentId = this.id;
172 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
173 if (typeof(cn.container_method) == 'string') {
174 cntr = cn.container_method;
178 var has_flexy_each = (typeof(tree['flexy:foreach']) != 'undefined');
180 var has_flexy_if = (typeof(tree['flexy:if']) != 'undefined');
182 var build_from_html = Roo.XComponent.build_from_html;
184 var is_body = (tree.xtype == 'Body') ;
186 var page_has_body = (Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body');
188 var self_cntr_el = Roo.get(this[cntr](false));
190 // do not try and build conditional elements
191 if ((has_flexy_each || has_flexy_if || this.can_build_overlaid == false ) && build_from_html) {
195 if (!has_flexy_each || !build_from_html || is_body || !page_has_body) {
196 if(!has_flexy_if || typeof(tree.name) == 'undefined' || !build_from_html || is_body || !page_has_body){
197 return this.addxtypeChild(tree,cntr, is_body);
200 var echild =self_cntr_el ? self_cntr_el.child('>*[name=' + tree.name + ']') : false;
203 return this.addxtypeChild(Roo.apply({}, tree),cntr);
206 Roo.log('skipping render');
212 if (!build_from_html) {
216 // this i think handles overlaying multiple children of the same type
217 // with the sam eelement.. - which might be buggy..
219 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
225 if (echild && echild.attr('xtype').split('.').pop() != cn.xtype) {
229 ret = this.addxtypeChild(Roo.apply({}, tree),cntr);
236 addxtypeChild : function (tree, cntr, is_body)
238 Roo.debug && Roo.log('addxtypeChild:' + cntr);
240 cntr = (typeof(cntr) == 'undefined' ) ? 'getChildContainer' : cntr;
243 var has_flexy = (typeof(tree['flexy:if']) != 'undefined') ||
244 (typeof(tree['flexy:foreach']) != 'undefined');
248 skip_children = false;
249 // render the element if it's not BODY.
252 // if parent was disabled, then do not try and create the children..
253 if(!this[cntr](true)){
258 cn = Roo.factory(tree);
260 cn.parentType = this.xtype; //??
261 cn.parentId = this.id;
263 var build_from_html = Roo.XComponent.build_from_html;
266 // does the container contain child eleemnts with 'xtype' attributes.
267 // that match this xtype..
268 // note - when we render we create these as well..
269 // so we should check to see if body has xtype set.
270 if (build_from_html && Roo.get(document.body).attr('xtype') == 'Roo.bootstrap.Body') {
272 var self_cntr_el = Roo.get(this[cntr](false));
273 var echild =self_cntr_el ? self_cntr_el.child('>*[xtype]') : false;
275 //Roo.log(Roo.XComponent.build_from_html);
276 //Roo.log("got echild:");
279 // there is a scenario where some of the child elements are flexy:if (and all of the same type)
280 // and are not displayed -this causes this to use up the wrong element when matching.
281 // at present the only work around for this is to nest flexy:if elements in another element that is always rendered.
284 if (echild && echild.attr('xtype').split('.').pop() == cn.xtype) {
285 // Roo.log("found child for " + this.xtype +": " + echild.attr('xtype') );
291 //echild.dom.removeAttribute('xtype');
293 Roo.debug && Roo.log("MISSING " + cn.xtype + " on child of " + (this.el ? this.el.attr('xbuilderid') : 'no parent'));
294 Roo.debug && Roo.log(self_cntr_el);
295 Roo.debug && Roo.log(echild);
296 Roo.debug && Roo.log(cn);
302 // if object has flexy:if - then it may or may not be rendered.
303 if (build_from_html && has_flexy && !cn.el && cn.can_build_overlaid) {
304 // skip a flexy if element.
305 Roo.debug && Roo.log('skipping render');
306 Roo.debug && Roo.log(tree);
308 Roo.debug && Roo.log('skipping all children');
309 skip_children = true;
314 // actually if flexy:foreach is found, we really want to create
315 // multiple copies here...
317 //Roo.log(this[cntr]());
318 // some elements do not have render methods.. like the layouts...
320 if(this[cntr](true) === false){
325 cn.render && cn.render(this[cntr](true));
328 // then add the element..
335 if (typeof (tree.menu) != 'undefined') {
336 tree.menu.parentType = cn.xtype;
337 tree.menu.triggerEl = cn.el;
338 nitems.push(cn.addxtype(Roo.apply({}, tree.menu)));
342 if (!tree.items || !tree.items.length) {
344 //Roo.log(["no children", this]);
349 var items = tree.items;
352 //Roo.log(items.length);
354 if (!skip_children) {
355 for(var i =0;i < items.length;i++) {
356 // Roo.log(['add child', items[i]]);
357 nitems.push(cn.addxtype(Roo.apply({}, items[i])));
363 //Roo.log("fire childrenrendered");
365 cn.fireEvent('childrenrendered', this);
371 * Set the element that will be used to show or hide
373 setVisibilityEl : function(el)
375 this.visibilityEl = el;
379 * Get the element that will be used to show or hide
381 getVisibilityEl : function()
383 if (typeof(this.visibilityEl) == 'object') {
384 return this.visibilityEl;
387 if (typeof(this.visibilityEl) == 'string') {
388 return this.visibilityEl == 'parent' ? this.parent().getEl() : this.getEl();
395 * Show a component - removes 'hidden' class
399 if(!this.getVisibilityEl()){
403 this.getVisibilityEl().removeClass('hidden');
405 this.fireEvent('show', this);
410 * Hide a component - adds 'hidden' class
414 if(!this.getVisibilityEl()){
418 this.getVisibilityEl().addClass('hidden');
420 this.fireEvent('hide', this);