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.
36 * @param {Object} config The config object
40 Roo.bootstrap.Button = function(config){
41 Roo.bootstrap.Button.superclass.constructor.call(this, config);
47 * When a butotn is pressed
48 * @param {Roo.bootstrap.Button} btn
49 * @param {Roo.EventObject} e
54 * After the button has been toggles
55 * @param {Roo.bootstrap.Button} btn
56 * @param {Roo.EventObject} e
57 * @param {boolean} pressed (also available as button.pressed)
63 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
92 getAutoCreate : function(){
100 if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
101 throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
106 cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
108 if (this.toggle == true) {
111 cls: 'slider-frame roo-button',
116 'data-off-text':'OFF',
117 cls: 'slider-button',
123 if (['default', 'secondary' , 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
124 cfg.cls += ' '+this.weight;
133 cfg["aria-hidden"] = true;
135 cfg.html = "×";
141 if (this.theme==='default') {
142 cfg.cls = 'btn roo-button';
144 //if (this.parentType != 'Navbar') {
145 this.weight = this.weight.length ? this.weight : 'default';
147 if (['default', 'primary', 'secondary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
149 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
150 var weight = this.weight == 'default' ? 'secondary' : this.weight;
151 cfg.cls += ' btn-' + outline + weight;
152 if (this.weight == 'default') {
154 cfg.cls += ' btn-' + this.weight;
157 } else if (this.theme==='glow') {
160 cfg.cls = 'btn-glow roo-button';
162 if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
164 cfg.cls += ' ' + this.weight;
170 this.cls += ' inverse';
174 if (this.active || this.pressed === true) {
175 cfg.cls += ' active';
179 cfg.disabled = 'disabled';
183 Roo.log('changing to ul' );
185 this.glyphicon = 'caret';
186 if (Roo.bootstrap.version == 4) {
187 this.fa = 'caret-down';
192 cfg.cls += this.size.length ? (' btn-' + this.size) : '';
194 //gsRoo.log(this.parentType);
195 if (this.parentType === 'Navbar' && !this.parent().bar) {
196 Roo.log('changing to li?');
205 href : this.href || '#'
208 cfg.cn[0].html = this.html + ' <span class="caret"></span>';
209 cfg.cls += ' dropdown';
216 cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
218 if (this.glyphicon) {
219 cfg.html = ' ' + cfg.html;
224 cls: 'glyphicon glyphicon-' + this.glyphicon
229 cfg.html = ' ' + cfg.html;
234 cls: 'fa fas fa-' + this.fa
244 // cfg.cls='btn roo-button';
248 var value = cfg.html;
253 cls: 'glyphicon glyphicon-' + this.glyphicon,
260 cls: 'fa fas fa-' + this.fa,
265 var bw = this.badge_weight.length ? this.badge_weight :
266 (this.weight.length ? this.weight : 'secondary');
267 bw = bw == 'default' ? 'secondary' : bw;
273 cls: 'badge badge-' + bw,
282 cfg.cls += ' dropdown';
283 cfg.html = typeof(cfg.html) != 'undefined' ?
284 cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
287 if (cfg.tag !== 'a' && this.href !== '') {
288 throw "Tag must be a to set href.";
289 } else if (this.href.length > 0) {
290 cfg.href = this.href;
293 if(this.removeClass){
298 cfg.target = this.target;
303 initEvents: function() {
304 // Roo.log('init events?');
305 // Roo.log(this.el.dom);
308 if (typeof (this.menu) != 'undefined') {
309 this.menu.parentType = this.xtype;
310 this.menu.triggerEl = this.el;
311 this.addxtype(Roo.apply({}, this.menu));
315 if (this.el.hasClass('roo-button')) {
316 this.el.on('click', this.onClick, this);
318 this.el.select('.roo-button').on('click', this.onClick, this);
321 if(this.removeClass){
322 this.el.on('click', this.onClick, this);
325 this.el.enableDisplayMode();
328 onClick : function(e)
334 Roo.log('button on click ');
335 if(this.preventDefault){
339 if (this.pressed === true || this.pressed === false) {
340 this.toggleActive(e);
344 this.fireEvent('click', this, e);
348 * Enables this button
352 this.disabled = false;
353 this.el.removeClass('disabled');
357 * Disable this button
361 this.disabled = true;
362 this.el.addClass('disabled');
365 * sets the active state on/off,
366 * @param {Boolean} state (optional) Force a particular state
368 setActive : function(v) {
370 this.el[v ? 'addClass' : 'removeClass']('active');
374 * toggles the current active state
376 toggleActive : function(e)
378 this.setActive(!this.pressed);
379 this.fireEvent('toggle', this, e, !this.pressed);
382 * get the current active state
383 * @return {boolean} true if it's active
385 isActive : function()
387 return this.el.hasClass('active');
390 * set the text of the first selected button
392 setText : function(str)
394 this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
397 * get the text of the first selected button
401 return this.el.select('.roo-button-text',true).first().dom.innerHTML;
404 setWeight : function(str)
406 this.el.removeClass(Roo.bootstrap.Button.weightClass );
408 var outline = this.outline ? 'outline-' : '';
409 if (str == 'default') {
410 this.el.addClass('btn-default btn-outline-secondary');
413 this.el.addClass('btn-' + outline + str);
418 Roo.bootstrap.Button.weightClass = [
421 "btn-outline-secondary",