Roo.bootstrap.Button = function(config){
Roo.bootstrap.Button.superclass.constructor.call(this, config);
this.addEvents({
"click" : true,
"dblclick" : true,
"toggle" : true
});
};
Roo.extend(Roo.bootstrap.Button, Roo.bootstrap.Component, {
html: false,
active: false,
weight: '',
badge_weight: '',
outline : false,
size: '',
tag: 'button',
href: '',
disabled: false,
isClose: false,
glyphicon: '',
fa: '',
badge: '',
theme: 'default',
inverse: false,
toggle: false,
ontext: 'ON',
offtext: 'OFF',
defaulton: true,
preventDefault: true,
removeClass: false,
name: false,
target: false,
group : false,
pressed : null,
getAutoCreate : function(){
var cfg = {
tag : 'button',
cls : 'roo-button',
html: ''
};
if (['a', 'button', 'input', 'submit'].indexOf(this.tag) < 0) {
throw "Invalid value for tag: " + this.tag + ". must be a, button, input or submit.";
this.tag = 'button';
} else {
cfg.tag = this.tag;
}
cfg.html = '<span class="roo-button-text">' + (this.html || cfg.html) + '</span>';
if (this.toggle == true) {
cfg={
tag: 'div',
cls: 'slider-frame roo-button',
cn: [
{
tag: 'span',
'data-on-text':'ON',
'data-off-text':'OFF',
cls: 'slider-button',
html: this.offtext
}
]
};
if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
cfg.cls += ' ' + this.weight;
}
return cfg;
}
if (this.isClose) {
cfg.cls += ' close';
cfg["aria-hidden"] = true;
cfg.html = "×";
return cfg;
}
if (this.theme==='default') {
cfg.cls = 'btn roo-button';
this.weight = this.weight.length ? this.weight : 'default';
if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
var outline = this.outline || this.weight == 'default' ? 'outline-' : '';
var weight = this.weight == 'default' ? 'secondary' : this.weight;
cfg.cls += ' btn-' + outline + weight;
if (this.weight == 'default') {
cfg.cls += ' btn-' + this.weight;
}
}
} else if (this.theme==='glow') {
cfg.tag = 'a';
cfg.cls = 'btn-glow roo-button';
if (Roo.bootstrap.Button.weights.indexOf(this.weight) > -1) {
cfg.cls += ' ' + this.weight;
}
}
if (this.inverse) {
this.cls += ' inverse';
}
if (this.active || this.pressed === true) {
cfg.cls += ' active';
}
if (this.disabled) {
cfg.disabled = 'disabled';
}
if (this.items) {
Roo.log('changing to ul' );
cfg.tag = 'ul';
this.glyphicon = 'caret';
if (Roo.bootstrap.version == 4) {
this.fa = 'caret-down';
}
}
cfg.cls += this.size.length ? (' btn-' + this.size) : '';
if (this.parentType === 'Navbar' && !this.parent().bar) {
Roo.log('changing to li?');
cfg.tag = 'li';
cfg.cls = '';
cfg.cn = [{
tag : 'a',
cls : 'roo-button',
html : this.html,
href : this.href || '#'
}];
if (this.menu) {
cfg.cn[0].html = this.html + ' <span class="caret"></span>';
cfg.cls += ' dropdown';
}
delete cfg.html;
}
cfg.cls += this.parentType === 'Navbar' ? ' navbar-btn' : '';
if (this.glyphicon) {
cfg.html = ' ' + cfg.html;
cfg.cn = [
{
tag: 'span',
cls: 'glyphicon glyphicon-' + this.glyphicon
}
];
}
if (this.fa) {
cfg.html = ' ' + cfg.html;
cfg.cn = [
{
tag: 'i',
cls: 'fa fas fa-' + this.fa
}
];
}
if (this.badge) {
cfg.html += ' ';
cfg.tag = 'a';
cfg.href=this.href;
var value = cfg.html;
if(this.glyphicon){
value = {
tag: 'span',
cls: 'glyphicon glyphicon-' + this.glyphicon,
html: this.html
};
}
if(this.fa){
value = {
tag: 'i',
cls: 'fa fas fa-' + this.fa,
html: this.html
};
}
var bw = this.badge_weight.length ? this.badge_weight :
(this.weight.length ? this.weight : 'secondary');
bw = bw == 'default' ? 'secondary' : bw;
cfg.cn = [
value,
{
tag: 'span',
cls: 'badge badge-' + bw,
html: this.badge
}
];
cfg.html='';
}
if (this.menu) {
cfg.cls += ' dropdown';
cfg.html = typeof(cfg.html) != 'undefined' ?
cfg.html + ' <span class="caret"></span>' : '<span class="caret"></span>';
}
if (cfg.tag !== 'a' && this.href !== '') {
throw "Tag must be a to set href.";
} else if (this.href.length > 0) {
cfg.href = this.href;
}
if(this.removeClass){
cfg.cls = '';
}
if(this.target){
cfg.target = this.target;
}
return cfg;
},
initEvents: function() {
if (typeof (this.menu) != 'undefined') {
this.menu.parentType = this.xtype;
this.menu.triggerEl = this.el;
this.addxtype(Roo.apply({}, this.menu));
}
if (this.el.hasClass('roo-button')) {
this.el.on('click', this.onClick, this);
this.el.on('dblclick', this.onDblClick, this);
} else {
this.el.select('.roo-button').on('click', this.onClick, this);
this.el.select('.roo-button').on('dblclick', this.onDblClick, this);
}
if(this.removeClass){
this.el.on('click', this.onClick, this);
}
if (this.group === true) {
if (this.pressed === false || this.pressed === true) {
} else {
this.pressed = false;
this.setActive(this.pressed);
}
}
this.el.enableDisplayMode();
},
onClick : function(e)
{
if (this.disabled) {
return;
}
Roo.log('button on click ');
if(this.href === '' || this.preventDefault){
e.preventDefault();
}
if (this.group) {
if (this.pressed) {
return;
}
this.setActive(true);
var pi = this.parent().items;
for (var i = 0;i < pi.length;i++) {
if (this == pi[i]) {
continue;
}
if (pi[i].el.hasClass('roo-button')) {
pi[i].setActive(false);
}
}
this.fireEvent('click', this, e);
return;
}
if (this.pressed === true || this.pressed === false) {
this.toggleActive(e);
}
this.fireEvent('click', this, e);
},
onDblClick: function(e)
{
if (this.disabled) {
return;
}
if(this.preventDefault){
e.preventDefault();
}
this.fireEvent('dblclick', this, e);
},
enable : function()
{
this.disabled = false;
this.el.removeClass('disabled');
this.el.dom.removeAttribute("disabled");
},
disable : function()
{
this.disabled = true;
this.el.addClass('disabled');
this.el.attr("disabled", "disabled")
},
setActive : function(v) {
this.el[v ? 'addClass' : 'removeClass']('active');
this.pressed = v;
},
toggleActive : function(e)
{
this.setActive(!this.pressed); this.fireEvent('toggle', this, e, this.pressed);
},
isActive : function()
{
return this.el.hasClass('active');
},
setText : function(str)
{
this.el.select('.roo-button-text',true).first().dom.innerHTML = str;
},
getText : function()
{
return this.el.select('.roo-button-text',true).first().dom.innerHTML;
},
setWeight : function(str)
{
this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-' + w; } ) );
this.el.removeClass(Roo.bootstrap.Button.weights.map(function(w) { return 'btn-outline-' + w; } ) );
this.weight = str;
var outline = this.outline ? 'outline-' : '';
if (str == 'default') {
this.el.addClass('btn-default btn-outline-secondary');
return;
}
this.el.addClass('btn-' + outline + str);
}
});
Roo.bootstrap.Button.weights = [
'default',
'secondary' ,
'primary',
'success',
'info',
'warning',
'danger',
'link',
'light',
'dark'
];