META-INF.resources.primefaces.forms.forms.selectbooleanbutton.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of primefaces Show documentation
Show all versions of primefaces Show documentation
PrimeFaces is one of the most popular UI libraries in Java EE Ecosystem and widely used by software companies, world renowned brands, banks, financial institutions, insurance companies, universities and more.
/**
* PrimeFaces SelectBooleanButton Widget
*/
PrimeFaces.widget.SelectBooleanButton = PrimeFaces.widget.BaseWidget.extend({
init: function(cfg) {
this._super(cfg);
this.input = $(this.jqId + '_input');
this.disabled = this.input.is(':disabled');
this.icon = this.jq.children('.ui-button-icon-left');
var $this = this;
//bind events if not disabled
if(!this.disabled) {
this.jq.on('mouseover', function() {
if(!$this.jq.hasClass('ui-state-active')) {
$this.jq.addClass('ui-state-hover');
}
}).on('mouseout', function() {
$this.jq.removeClass('ui-state-hover');
})
.on('click', function() {
$this.toggle();
$this.input.trigger('focus');
});
}
this.input.on('focus', function() {
$this.jq.addClass('ui-state-focus');
})
.on('blur', function() {
$this.jq.removeClass('ui-state-focus');
})
.on('keydown', function(e) {
var keyCode = $.ui.keyCode;
if(e.which === keyCode.SPACE) {
e.preventDefault();
}
})
.on('keyup', function(e) {
var keyCode = $.ui.keyCode;
if(e.which === keyCode.SPACE) {
$this.toggle();
e.preventDefault();
}
});
//pfs metadata
this.input.data(PrimeFaces.CLIENT_ID_DATA, this.id);
},
toggle: function() {
if(!this.disabled) {
if(this.input.prop('checked'))
this.uncheck();
else
this.check();
}
},
check: function() {
if(!this.disabled) {
this.input.prop('checked', true);
this.jq.addClass('ui-state-active').children('.ui-button-text').html(this.cfg.onLabel);
if(this.icon.length > 0) {
this.icon.removeClass(this.cfg.offIcon).addClass(this.cfg.onIcon);
}
this.input.trigger('change');
}
},
uncheck: function() {
if(!this.disabled) {
this.input.prop('checked', false);
this.jq.removeClass('ui-state-active').children('.ui-button-text').html(this.cfg.offLabel);
if(this.icon.length > 0) {
this.icon.removeClass(this.cfg.onIcon).addClass(this.cfg.offIcon);
}
this.input.trigger('change');
}
}
});