9 * @class Roo.bootstrap.Button
10 * @extends Roo.bootstrap.Component
11 * Bootstrap Button class
12 * @cfg {String} html The button content
13 * @cfg {String} weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default
14 * @cfg {String} badge_weight (default|primary|secondary|success|info|warning|danger|link|light|dark) default (same as button)
15 * @cfg {Boolean} outline default false (except for weight=default which emulates old behaveiour with an outline)
16 * @cfg {String} size (lg|sm|xs)
17 * @cfg {String} tag (a|input|submit)
18 * @cfg {String} href empty or href
19 * @cfg {Boolean} disabled default false;
20 * @cfg {Boolean} isClose default false;
21 * @cfg {String} glyphicon depricated - use fa
22 * @cfg {String} fa fontawesome icon - eg. 'comment' - without the fa/fas etc..
23 * @cfg {String} badge text for badge
24 * @cfg {String} theme (default|glow)
25 * @cfg {Boolean} inverse dark themed version
26 * @cfg {Boolean} toggle is it a slidy toggle button
27 * @cfg {Boolean} pressed  default null - if the button ahs active state
28 * @cfg {String} ontext text for on slidy toggle state
29 * @cfg {String} offtext text for off slidy toggle state
30 * @cfg {Boolean} preventDefault default true (stop click event triggering the URL if it's a link.)
31 * @cfg {Boolean} removeClass remove the standard class..
32 * @cfg {String} target (_self|_blank|_parent|_top|other) target for a href.
33 * @cfg {Boolean} grpup if parent is a btn group - then it turns it into a toogleGroup.
37 * @param {Object} config The config object
41 Roo.bootstrap.Button = function(config){
42 Roo.bootstrap.Button.superclass.constructor.call(this, config);
48 * When a button is pressed
49 * @param {Roo.bootstrap.Button} btn
50 * @param {Roo.EventObject} e
55 * When a button is double clicked
56 * @param {Roo.bootstrap.Button} btn
57 * @param {Roo.EventObject} e
62 * After the button has been toggles
63 * @param {Roo.bootstrap.Button} btn
64 * @param {Roo.EventObject} e
65 * @param {boolean} pressed (also available as button.pressed)
71 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
101 getAutoCreate : function(){
109 if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
110 throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
115 cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
117 if (this.toggle == true) {
120 cls: 'slider-frame roo-button',
125 'data-off-text':'OFF',
126 cls: 'slider-button',
131 // why are we validating the weights?
132 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
133 cfg.cls += ' ' + this.weight;
142 cfg["aria-hidden"] = true;
144 cfg.html = "×";
150 if (this.theme==='default') {
151 cfg.cls = 'btn roo-button';
153 //if (this.parentType != 'Navbar') {
154 this.weight = this.weight.length ? this.weight : 'default';
156 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
158 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
159 var weight = this.weight == 'default' ? 'secondary' : this.weight;
160 cfg.cls += ' btn-' + outline + weight;
161 if (this.weight == 'default') {
163 cfg.cls += ' btn-' + this.weight;
166 } else if (this.theme==='glow') {
169 cfg.cls = 'btn-glow roo-button';
171 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
173 cfg.cls += ' ' + this.weight;
179 this.cls += ' inverse';
183 if (this.active || this.pressed === true) {
184 cfg.cls += ' active';
188 cfg.disabled = 'disabled';
192 Roo.log('changing to ul' );
194 this.glyphicon = 'caret';
195 if (Roo.bootstrap.version == 4) {
196 this.fa = 'caret-down';
201 cfg.cls += this.size.length ? (' btn-' + this.size) : '';
203 //gsRoo.log(this.parentType);
204 if (this.parentType === 'Navbar' && !this.parent().bar) {
205 Roo.log('changing to li?');
214 href : this.href || '#'
217 cfg.cn[0].html = this.html + ' <span class="caret"></span>';
218 cfg.cls += ' dropdown';
225 cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
227 if (this.glyphicon) {
228 cfg.html = ' ' + cfg.html;
233 cls: 'glyphicon glyphicon-' + this.glyphicon
238 cfg.html = ' ' + cfg.html;
243 cls: 'fa fas fa-' + this.fa
253 // cfg.cls='btn roo-button';
257 var value = cfg.html;
262 cls: 'glyphicon glyphicon-' + this.glyphicon,
269 cls: 'fa fas fa-' + this.fa,
274 var bw = this.badge_weight.length ? this.badge_weight :
275 (this.weight.length ? this.weight : 'secondary');
276 bw = bw == 'default' ? 'secondary' : bw;
282 cls: 'badge badge-' + bw,
291 cfg.cls += ' dropdown';
292 cfg.html = typeof(cfg.html) != 'undefined' ?
293 cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
296 if (cfg.tag !== 'a' && this.href !== '') {
297 throw "Tag must be a to set href.";
298 } else if (this.href.length > 0) {
299 cfg.href = this.href;
302 if(this.removeClass){
307 cfg.target = this.target;
312 initEvents: function() {
313 // Roo.log('init events?');
314 // Roo.log(this.el.dom);
317 if (typeof (this.menu) != 'undefined') {
318 this.menu.parentType = this.xtype;
319 this.menu.triggerEl = this.el;
320 this.addxtype(Roo.apply({}, this.menu));
324 if (this.el.hasClass('roo-button')) {
325 this.el.on('click', this.onClick, this);
326 this.el.on('dblclick', this.onDblClick, this);
328 this.el.select('.roo-button').on('click', this.onClick, this);
329 this.el.select('.roo-button').on('dblclick', this.onDblClick, this);
333 if(this.removeClass){
334 this.el.on('click', this.onClick, this);
337 if (this.group === true) {
338 if (this.pressed === false || this.pressed === true) {
341 this.pressed = false;
342 this.setActive(this.pressed);
347 this.el.enableDisplayMode();
350 onClick : function(e)
356 Roo.log('button on click ');
357 if(this.preventDefault){
366 this.setActive(true);
367 var pi = this.parent().items;
368 for (var i = 0;i < pi.length;i++) {
372 if (pi[i].el.hasClass('roo-button')) {
373 pi[i].setActive(false);
376 this.fireEvent('click', this, e);
380 if (this.pressed === true || this.pressed === false) {
381 this.toggleActive(e);
385 this.fireEvent('click', this, e);
387 onDblClick: function(e)
392 if(this.preventDefault){
395 this.fireEvent('dblclick', this, e);
398 * Enables this button
402 this.disabled = false;
403 this.el.removeClass('disabled');
404 this.el.dom.removeAttribet("disabled");
408 * Disable this button
412 this.disabled = true;
413 this.el.addClass('disabled');
414 this.el.attr("disabled", "disabled")
417 * sets the active state on/off,
418 * @param {Boolean} state (optional) Force a particular state
420 setActive : function(v) {
422 this.el[v ? 'addClass' : 'removeClass']('active');
426 * toggles the current active state
428 toggleActive : function(e)
430 this.setActive(!this.pressed); // this modifies pressed...
431 this.fireEvent('toggle', this, e, this.pressed);
434 * get the current active state
435 * @return {boolean} true if it's active
437 isActive : function()
439 return this.el.hasClass('active');
442 * set the text of the first selected button
444 setText : function(str)
446 this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
449 * get the text of the first selected button
453 return this.el.select('.roo-button-text',true).first().dom.innerHTML;
456 setWeight : function(str)
458 this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-' + w; } ) );
459 this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-outline-' + w; } ) );
461 var outline = this.outline ? 'outline-' : '';
462 if (str == 'default') {
463 this.el.addClass('btn-default btn-outline-secondary');
466 this.el.addClass('btn-' + outline + str);
471 // fixme - this is probably generic bootstrap - should go in some kind of enum file.. - like sizes.
473 Roo.bootstrap.Button.weights = [