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 fs
22 * @cfg {String} badge text for badge
23 * @cfg {String} theme (default|glow)
24 * @cfg {Boolean} inverse dark themed version
25 * @cfg {Boolean} toggle is it a slidy toggle button
26 * @cfg {Boolean} pressed (true|false) default null - if the button ahs active state
27 * @cfg {String} ontext text for on slidy toggle state
28 * @cfg {String} offtext text for off slidy toggle state
29 * @cfg {Boolean} preventDefault default true (stop click event triggering the URL if it's a link.)
30 * @cfg {Boolean} removeClass remove the standard class..
31 * @cfg {String} target target for a href. (_self|_blank|_parent|_top| other)
35 * @param {Object} config The config object
39 Roo.bootstrap.Button = function(config){
40 Roo.bootstrap.Button.superclass.constructor.call(this, config);
41 this.weightClass = ["btn-default btn-outline-secondary",
53 * When a butotn is pressed
54 * @param {Roo.bootstrap.Button} btn
55 * @param {Roo.EventObject} e
60 * After the button has been toggles
61 * @param {Roo.bootstrap.Button} btn
62 * @param {Roo.EventObject} e
63 * @param {boolean} pressed (also available as button.pressed)
69 Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
98 getAutoCreate : function(){
106 if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
107 throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
112 cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
114 if (this.toggle == true) {
117 cls: 'slider-frame roo-button',
122 'data-off-text':'OFF',
123 cls: 'slider-button',
129 if (['default', 'secondary' , 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
130 cfg.cls += ' '+this.weight;
139 cfg["aria-hidden"] = true;
141 cfg.html = "×";
147 if (this.theme==='default') {
148 cfg.cls = 'btn roo-button';
150 //if (this.parentType != 'Navbar') {
151 this.weight = this.weight.length ? this.weight : 'default';
153 if (['default', 'primary', 'secondary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
155 var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
156 var weight = this.weight == 'default' ? 'secondary' : this.weight;
157 cfg.cls += ' btn-' + outline + weight;
158 if (this.weight == 'default') {
160 cfg.cls += ' btn-' + this.weight;
163 } else if (this.theme==='glow') {
166 cfg.cls = 'btn-glow roo-button';
168 if (['default', 'primary', 'success', 'info', 'warning', 'danger', 'link'].indexOf(this.weight) > -1) {
170 cfg.cls += ' ' + this.weight;
176 this.cls += ' inverse';
180 if (this.active || this.pressed === true) {
181 cfg.cls += ' active';
185 cfg.disabled = 'disabled';
189 Roo.log('changing to ul' );
191 this.glyphicon = 'caret';
192 if (Roo.bootstrap.version == 4) {
193 this.fa = 'caret-down';
198 cfg.cls += this.size.length ? (' btn-' + this.size) : '';
200 //gsRoo.log(this.parentType);
201 if (this.parentType === 'Navbar' && !this.parent().bar) {
202 Roo.log('changing to li?');
211 href : this.href || '#'
214 cfg.cn[0].html = this.html + ' <span class="caret"></span>';
215 cfg.cls += ' dropdown';
222 cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
224 if (this.glyphicon) {
225 cfg.html = ' ' + cfg.html;
230 cls: 'glyphicon glyphicon-' + this.glyphicon
235 cfg.html = ' ' + cfg.html;
240 cls: 'fa fas fa-' + this.fa
250 // cfg.cls='btn roo-button';
254 var value = cfg.html;
259 cls: 'glyphicon glyphicon-' + this.glyphicon,
264 var bw = this.badge_weight.length ? this.badge_weight :
265 (this.weight.length ? this.weight : 'secondary');
266 bw = bw == 'default' ? 'secondary' : bw;
272 cls: 'badge badge-' + bw,
281 cfg.cls += ' dropdown';
282 cfg.html = typeof(cfg.html) != 'undefined' ?
283 cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
286 if (cfg.tag !== 'a' && this.href !== '') {
287 throw "Tag must be a to set href.";
288 } else if (this.href.length > 0) {
289 cfg.href = this.href;
292 if(this.removeClass){
297 cfg.target = this.target;
302 initEvents: function() {
303 // Roo.log('init events?');
304 // Roo.log(this.el.dom);
307 if (typeof (this.menu) != 'undefined') {
308 this.menu.parentType = this.xtype;
309 this.menu.triggerEl = this.el;
310 this.addxtype(Roo.apply({}, this.menu));
314 if (this.el.hasClass('roo-button')) {
315 this.el.on('click', this.onClick, this);
317 this.el.select('.roo-button').on('click', this.onClick, this);
320 if(this.removeClass){
321 this.el.on('click', this.onClick, this);
324 this.el.enableDisplayMode();
327 onClick : function(e)
333 Roo.log('button on click ');
334 if(this.preventDefault){
338 if (this.pressed === true || this.pressed === false) {
339 this.toggleActive(e);
343 this.fireEvent('click', this, e);
347 * Enables this button
351 this.disabled = false;
352 this.el.removeClass('disabled');
356 * Disable this button
360 this.disabled = true;
361 this.el.addClass('disabled');
364 * sets the active state on/off,
365 * @param {Boolean} state (optional) Force a particular state
367 setActive : function(v) {
369 this.el[v ? 'addClass' : 'removeClass']('active');
373 * toggles the current active state
375 toggleActive : function(e)
377 this.setActive(!this.pressed);
378 this.fireEvent('toggle', this, e, !this.pressed);
381 * get the current active state
382 * @return {boolean} true if it's active
384 isActive : function()
386 return this.el.hasClass('active');
389 * set the text of the first selected button
391 setText : function(str)
393 this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
396 * get the text of the first selected button
400 return this.el.select('.roo-button-text',true).first().dom.innerHTML;
403 setWeight : function(str)
405 this.el.removeClass(this.weightClass);
407 var outline = this.outline ? 'outline-' : '';
408 if (str == 'default') {
409 this.el.addClass('btn-default btn-outline-secondary');
412 this.el.addClass('btn-' + outline + str);