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.
34 * @cfg {Roo.bootstrap.menu.Menu} menu a Menu
38 * @param {Object} config The config object
42 Roo.bootstrap.Button = function(config){
43 Roo.bootstrap.Button.superclass.constructor.call(this, config);
49 * When a button is pressed
50 * @param {Roo.bootstrap.Button} btn
51 * @param {Roo.EventObject} e
56 * When a button is double clicked
57 * @param {Roo.bootstrap.Button} btn
58 * @param {Roo.EventObject} e
63 * After the button has been toggles
64 * @param {Roo.bootstrap.Button} btn
65 * @param {Roo.EventObject} e
66 * @param {boolean} pressed (also available as button.pressed)
72 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
102 getAutoCreate : function(){
110 if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
111 throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
116 cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
118 if (this.toggle == true) {
121 cls: 'slider-frame roo-button',
126 'data-off-text':'OFF',
127 cls: 'slider-button',
132 // why are we validating the weights?
133 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
134 cfg.cls += ' ' + this.weight;
143 cfg["aria-hidden"] = true;
145 cfg.html = "×";
151 if (this.theme==='default') {
152 cfg.cls = 'btn roo-button';
154 //if (this.parentType != 'Navbar') {
155 this.weight = this.weight.length ? this.weight : 'default';
157 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
159 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
160 var weight = this.weight == 'default' ? 'secondary' : this.weight;
161 cfg.cls += ' btn-' + outline + weight;
162 if (this.weight == 'default') {
164 cfg.cls += ' btn-' + this.weight;
167 } else if (this.theme==='glow') {
170 cfg.cls = 'btn-glow roo-button';
172 if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
174 cfg.cls += ' ' + this.weight;
180 this.cls += ' inverse';
184 if (this.active || this.pressed === true) {
185 cfg.cls += ' active';
189 cfg.disabled = 'disabled';
193 Roo.log('changing to ul' );
195 this.glyphicon = 'caret';
196 if (Roo.bootstrap.version == 4) {
197 this.fa = 'caret-down';
202 cfg.cls += this.size.length ? (' btn-' + this.size) : '';
204 //gsRoo.log(this.parentType);
205 if (this.parentType === 'Navbar' && !this.parent().bar) {
206 Roo.log('changing to li?');
215 href : this.href || '#'
218 cfg.cn[0].html = this.html + ' <span class="caret"></span>';
219 cfg.cls += ' dropdown';
226 cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
228 if (this.glyphicon) {
229 cfg.html = ' ' + cfg.html;
234 cls: 'glyphicon glyphicon-' + this.glyphicon
239 cfg.html = ' ' + cfg.html;
244 cls: 'fa fas fa-' + this.fa
254 // cfg.cls='btn roo-button';
258 var value = cfg.html;
263 cls: 'glyphicon glyphicon-' + this.glyphicon,
270 cls: 'fa fas fa-' + this.fa,
275 var bw = this.badge_weight.length ? this.badge_weight :
276 (this.weight.length ? this.weight : 'secondary');
277 bw = bw == 'default' ? 'secondary' : bw;
283 cls: 'badge badge-' + bw,
292 cfg.cls += ' dropdown';
293 cfg.html = typeof(cfg.html) != 'undefined' ?
294 cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
297 if (cfg.tag !== 'a' && this.href !== '') {
298 throw "Tag must be a to set href.";
299 } else if (this.href.length > 0) {
300 cfg.href = this.href;
303 if(this.removeClass){
308 cfg.target = this.target;
313 initEvents: function() {
314 // Roo.log('init events?');
315 // Roo.log(this.el.dom);
318 if (typeof (this.menu) != 'undefined') {
319 this.menu.parentType = this.xtype;
320 this.menu.triggerEl = this.el;
321 this.addxtype(Roo.apply({}, this.menu));
325 if (this.el.hasClass('roo-button')) {
326 this.el.on('click', this.onClick, this);
327 this.el.on('dblclick', this.onDblClick, this);
329 this.el.select('.roo-button').on('click', this.onClick, this);
330 this.el.select('.roo-button').on('dblclick', this.onDblClick, this);
334 if(this.removeClass){
335 this.el.on('click', this.onClick, this);
338 if (this.group === true) {
339 if (this.pressed === false || this.pressed === true) {
342 this.pressed = false;
343 this.setActive(this.pressed);
348 this.el.enableDisplayMode();
351 onClick : function(e)
357 Roo.log('button on click ');
358 if(this.href === '' || this.preventDefault){
367 this.setActive(true);
368 var pi = this.parent().items;
369 for (var i = 0;i < pi.length;i++) {
373 if (pi[i].el.hasClass('roo-button')) {
374 pi[i].setActive(false);
377 this.fireEvent('click', this, e);
381 if (this.pressed === true || this.pressed === false) {
382 this.toggleActive(e);
386 this.fireEvent('click', this, e);
388 onDblClick: function(e)
393 if(this.preventDefault){
396 this.fireEvent('dblclick', this, e);
399 * Enables this button
403 this.disabled = false;
404 this.el.removeClass('disabled');
405 this.el.dom.removeAttribute("disabled");
409 * Disable this button
413 this.disabled = true;
414 this.el.addClass('disabled');
415 this.el.attr("disabled", "disabled")
418 * sets the active state on/off,
419 * @param {Boolean} state (optional) Force a particular state
421 setActive : function(v) {
423 this.el[v ? 'addClass' : 'removeClass']('active');
427 * toggles the current active state
429 toggleActive : function(e)
431 this.setActive(!this.pressed); // this modifies pressed...
432 this.fireEvent('toggle', this, e, this.pressed);
435 * get the current active state
436 * @return {boolean} true if it's active
438 isActive : function()
440 return this.el.hasClass('active');
443 * set the text of the first selected button
445 setText : function(str)
447 this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
450 * get the text of the first selected button
454 return this.el.select('.roo-button-text',true).first().dom.innerHTML;
457 setWeight : function(str)
459 this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-' + w; } ) );
460 this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-outline-' + w; } ) );
462 var outline = this.outline ? 'outline-' : '';
463 if (str == 'default') {
464 this.el.addClass('btn-default btn-outline-secondary');
467 this.el.addClass('btn-' + outline + str);
472 // fixme - this is probably generic bootstrap - should go in some kind of enum file.. - like sizes.
474 Roo.bootstrap.Button.weights = [