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 ) default
14 * @cfg {String} badge_weight (default|primary|secondary|success|info|warning|danger|link ) 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);
42 this.weightClass = ["btn-default btn-outline-secondary",
54 * When a butotn is pressed
55 * @param {Roo.bootstrap.Button} btn
56 * @param {Roo.EventObject} e
61 * After the button has been toggles
62 * @param {Roo.bootstrap.Button} btn
63 * @param {Roo.EventObject} e
64 * @param {boolean} pressed (also available as button.pressed)
70 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
99 getAutoCreate : function(){
107 if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
108 throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
113 cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
115 if (this.toggle == true) {
118 cls: 'slider-frame roo-button',
123 'data-off-text':'OFF',
124 cls: 'slider-button',
130 if (['default', 'secondary' , 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
131 cfg.cls += ' '+this.weight;
140 cfg["aria-hidden"] = true;
142 cfg.html = "×";
148 if (this.theme==='default') {
149 cfg.cls = 'btn roo-button';
151 //if (this.parentType != 'Navbar') {
152 this.weight = this.weight.length ? this.weight : 'default';
154 if (['default', 'primary', 'secondary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
156 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
157 var weight = this.weight == 'default' ? 'secondary' : this.weight;
158 cfg.cls += ' btn-' + outline + weight;
159 if (this.weight == 'default') {
161 cfg.cls += ' btn-' + this.weight;
164 } else if (this.theme==='glow') {
167 cfg.cls = 'btn-glow roo-button';
169 if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
171 cfg.cls += ' ' + this.weight;
177 this.cls += ' inverse';
181 if (this.active || this.pressed === true) {
182 cfg.cls += ' active';
186 cfg.disabled = 'disabled';
190 Roo.log('changing to ul' );
192 this.glyphicon = 'caret';
193 if (Roo.bootstrap.version == 4) {
194 this.fa = 'caret-down';
199 cfg.cls += this.size.length ? (' btn-' + this.size) : '';
201 //gsRoo.log(this.parentType);
202 if (this.parentType === 'Navbar' && !this.parent().bar) {
203 Roo.log('changing to li?');
212 href : this.href || '#'
215 cfg.cn[0].html = this.html + ' <span class="caret"></span>';
216 cfg.cls += ' dropdown';
223 cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
225 if (this.glyphicon) {
226 cfg.html = ' ' + cfg.html;
231 cls: 'glyphicon glyphicon-' + this.glyphicon
236 cfg.html = ' ' + cfg.html;
241 cls: 'fa fas fa-' + this.fa
251 // cfg.cls='btn roo-button';
255 var value = cfg.html;
260 cls: 'glyphicon glyphicon-' + this.glyphicon,
267 cls: 'fa fas fa-' + this.fa,
272 var bw = this.badge_weight.length ? this.badge_weight :
273 (this.weight.length ? this.weight : 'secondary');
274 bw = bw == 'default' ? 'secondary' : bw;
280 cls: 'badge badge-' + bw,
289 cfg.cls += ' dropdown';
290 cfg.html = typeof(cfg.html) != 'undefined' ?
291 cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
294 if (cfg.tag !== 'a' && this.href !== '') {
295 throw "Tag must be a to set href.";
296 } else if (this.href.length > 0) {
297 cfg.href = this.href;
300 if(this.removeClass){
305 cfg.target = this.target;
310 initEvents: function() {
311 // Roo.log('init events?');
312 // Roo.log(this.el.dom);
315 if (typeof (this.menu) != 'undefined') {
316 this.menu.parentType = this.xtype;
317 this.menu.triggerEl = this.el;
318 this.addxtype(Roo.apply({}, this.menu));
322 if (this.el.hasClass('roo-button')) {
323 this.el.on('click', this.onClick, this);
325 this.el.select('.roo-button').on('click', this.onClick, this);
328 if(this.removeClass){
329 this.el.on('click', this.onClick, this);
332 this.el.enableDisplayMode();
335 onClick : function(e)
341 Roo.log('button on click ');
342 if(this.preventDefault){
346 if (this.pressed === true || this.pressed === false) {
347 this.toggleActive(e);
351 this.fireEvent('click', this, e);
355 * Enables this button
359 this.disabled = false;
360 this.el.removeClass('disabled');
364 * Disable this button
368 this.disabled = true;
369 this.el.addClass('disabled');
372 * sets the active state on/off,
373 * @param {Boolean} state (optional) Force a particular state
375 setActive : function(v) {
377 this.el[v ? 'addClass' : 'removeClass']('active');
381 * toggles the current active state
383 toggleActive : function(e)
385 this.setActive(!this.pressed);
386 this.fireEvent('toggle', this, e, !this.pressed);
389 * get the current active state
390 * @return {boolean} true if it's active
392 isActive : function()
394 return this.el.hasClass('active');
397 * set the text of the first selected button
399 setText : function(str)
401 this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
404 * get the text of the first selected button
408 return this.el.select('.roo-button-text',true).first().dom.innerHTML;
411 setWeight : function(str)
413 this.el.removeClass(this.weightClass);
415 var outline = this.outline ? 'outline-' : '';
416 if (str == 'default') {
417 this.el.addClass('btn-default btn-outline-secondary');
420 this.el.addClass('btn-' + outline + str);