META-INF.resources.primefaces.forms.forms.selectlistbox.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 SelectListbox Widget
*/
PrimeFaces.widget.SelectListbox = PrimeFaces.widget.BaseWidget.extend({
init: function(cfg) {
this._super(cfg);
this.input = $(this.jqId + '_input'),
this.listContainer = this.jq.children('.ui-selectlistbox-listcontainer');
this.listElement = this.listContainer.children('.ui-selectlistbox-list');
this.options = $(this.input).children('option');
this.allItems = this.listElement.find('.ui-selectlistbox-item');
this.items = this.allItems.filter(':not(.ui-state-disabled)');
//scroll to selected
var selected = this.options.filter(':selected:not(:disabled)');
if(selected.length) {
PrimeFaces.scrollInView(this.listContainer, this.items.eq(selected.eq(0).index()));
}
this.bindEvents();
//pfs metadata
this.input.data(PrimeFaces.CLIENT_ID_DATA, this.id);
},
bindEvents: function() {
var $this = this;
//items
this.items.on('mouseover.selectListbox', function() {
var item = $(this);
if(!item.hasClass('ui-state-highlight')) {
item.addClass('ui-state-hover');
}
})
.on('mouseout.selectListbox', function() {
$(this).removeClass('ui-state-hover');
})
.on('dblclick.selectListbox', function(e) {
$this.input.trigger('dblclick');
PrimeFaces.clearSelection();
e.preventDefault();
});
//input
this.input.on('focus.selectListbox', function() {
$this.jq.addClass('ui-state-focus');
}).on('blur.selectListbox', function() {
$this.jq.removeClass('ui-state-focus');
});
if(this.cfg.filter) {
this.filterInput = this.jq.find('> div.ui-selectlistbox-filter-container > input.ui-selectlistbox-filter');
PrimeFaces.skinInput(this.filterInput);
this.filterInput.on('keyup.selectListbox', function(e) {
$this.filter(this.value);
});
this.setupFilterMatcher();
}
},
unselectAll: function() {
this.items.removeClass('ui-state-highlight ui-state-hover');
this.options.filter(':selected').prop('selected', false);
},
selectItem: function(item) {
item.addClass('ui-state-highlight').removeClass('ui-state-hover');
this.options.eq(item.index()).prop('selected', true);
},
unselectItem: function(item) {
item.removeClass('ui-state-highlight');
this.options.eq(item.index()).prop('selected', false);
},
setupFilterMatcher: function() {
this.cfg.filterMatchMode = this.cfg.filterMatchMode||'startsWith';
this.filterMatchers = {
'startsWith': this.startsWithFilter
,'contains': this.containsFilter
,'endsWith': this.endsWithFilter
,'custom': this.cfg.filterFunction
};
this.filterMatcher = this.filterMatchers[this.cfg.filterMatchMode];
},
startsWithFilter: function(value, filter) {
return value.indexOf(filter) === 0;
},
containsFilter: function(value, filter) {
return value.indexOf(filter) !== -1;
},
endsWithFilter: function(value, filter) {
return value.indexOf(filter, value.length - filter.length) !== -1;
},
filter: function(value) {
var filterValue = this.cfg.caseSensitive ? $.trim(value) : $.trim(value).toLowerCase();
if(filterValue === '') {
this.items.filter(':hidden').show();
}
else {
for(var i = 0; i < this.options.length; i++) {
var option = this.options.eq(i),
itemLabel = this.cfg.caseSensitive ? option.text() : option.text().toLowerCase(),
item = this.items.eq(i);
if(this.filterMatcher(itemLabel, filterValue))
item.show();
else
item.hide();
}
}
}
});