Commit 79a15968 authored by David Stutz's avatar David Stutz

Merge pull request #279 from Tyf0x/perf

** Performance improvement ** - based on thorst #268 contribution.
parents b30473b0 3757ccd6
...@@ -225,10 +225,10 @@ ...@@ -225,10 +225,10 @@
templates: { templates: {
button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"></button>', button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"></button>',
ul: '<ul class="multiselect-container dropdown-menu"></ul>', ul: '<ul class="multiselect-container dropdown-menu"></ul>',
filter: '<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input class="form-control multiselect-search" type="text"></div>', filter: '<li class="multiselect-item filter"><div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input class="form-control multiselect-search" type="text"></div></li>',
li: '<li><a href="javascript:void(0);"><label></label></a></li>', li: '<li><a href="javascript:void(0);"><label></label></a></li>',
divider: '<li class="divider"></li>', divider: '<li class="multiselect-item divider"></li>',
liGroup: '<li><label class="multiselect-group"></label></li>' liGroup: '<li class="multiselect-item group"><label class="multiselect-group"></label></li>'
} }
}, },
...@@ -354,19 +354,11 @@ ...@@ -354,19 +354,11 @@
var $checkboxesNotThis = $('input', this.$container).not($target); var $checkboxesNotThis = $('input', this.$container).not($target);
if (isSelectAllOption) { if (isSelectAllOption) {
var values = [];
// Select the visible checkboxes except the "select-all" and possible divider.
var availableInputs = $('li input[value!="' + this.options.selectAllValue + '"][data-role!="divider"]', this.$ul).filter(':visible');
for (var i = 0, j = availableInputs.length; i < j; i++) {
values.push(availableInputs[i].value);
}
if (checked) { if (checked) {
this.select(values); this.selectall();
} }
else { else {
this.deselect(values); this.deselectall();
} }
} }
...@@ -520,6 +512,7 @@ ...@@ -520,6 +512,7 @@
$checkbox.val(value); $checkbox.val(value);
if (value === this.options.selectAllValue) { if (value === this.options.selectAllValue) {
$li.addClass("multiselect-item multiselect-all");
$checkbox.parent().parent() $checkbox.parent().parent()
.addClass('multiselect-all'); .addClass('multiselect-all');
} }
...@@ -659,7 +652,7 @@ ...@@ -659,7 +652,7 @@
}, this)); }, this));
} }
// TODO: check whether select all option needs to be updated. this.updateSelectAll();
}, this), 300, this); }, this), 300, this);
}, this)); }, this));
} }
...@@ -751,20 +744,9 @@ ...@@ -751,20 +744,9 @@
* *
*/ */
clearSelection: function () { clearSelection: function () {
this.deselectall(false);
var selected = this.getSelected(); this.updateButtonText();
this.updateSelectAll();
if (selected.length) {
var arry = [];
for (var i = 0; i < selected.length; i = i + 1) {
arry.push(selected[i].value);
}
this.deselect(arry);
this.$select.change();
}
}, },
/** /**
...@@ -796,6 +778,57 @@ ...@@ -796,6 +778,57 @@
this.updateButtonText(); this.updateButtonText();
}, },
/**
* Selects all enabled & visible options.
*
*/
selectall: function () {
var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul),
visibleCheckboxes = allCheckboxes.filter(":visible"),
allCheckboxesCount = allCheckboxes.length,
visibleCheckboxesCount = visibleCheckboxes.length;
visibleCheckboxes.prop('checked', true);
$("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").addClass(this.options.selectedClass);
if (allCheckboxesCount === visibleCheckboxesCount) {
$("option:enabled:not([data-role='divider'])", this.$select).prop('selected', true);
}
else {
var values = visibleCheckboxes.map(function() { return $(this).val() }).get();
$("option:enabled:not([data-role='divider'])", this.$select).filter(function(index){ return $.inArray($(this).val(), values) !== -1; }).prop('selected', true);
}
},
/**
* Deselects all options.
* If justVisible is true or not specified, only visible options are deselected.
*
* @param {Boolean} justVisible
*/
deselectall: function (justVisible) {
var allCheckboxes = $("li input[type='checkbox']:enabled", this.$ul),
justVisible = typeof justVisible === 'undefined' ? true : justVisible,
visibleCheckboxes = void(0);
if(justVisible) {
var values = void(0);
visibleCheckboxes = allCheckboxes.filter(":visible");
visibleCheckboxes.prop('checked', false);
values = visibleCheckboxes.map(function() { return $(this).val() }).get();
$("option:enabled:not([data-role='divider'])", this.$select).filter(function(index){ return $.inArray($(this).val(), values) !== -1; }).prop('selected', false);
$("li:not(.divider):not(.disabled)", this.$ul).filter(":visible").removeClass(this.options.selectedClass);
}else {
allCheckboxes.prop('checked', false);
$("option:enabled:not([data-role='divider'])", this.$select).prop('selected', false);
$("li:not(.divider):not(.disabled)", this.$ul).removeClass(this.options.selectedClass);
}
},
/** /**
* Rebuild the plugin. * Rebuild the plugin.
* Rebuilds the dropdown, the filter and the select all option. * Rebuilds the dropdown, the filter and the select all option.
...@@ -880,13 +913,15 @@ ...@@ -880,13 +913,15 @@
}, },
/** /**
* Updates the select all option based on the currently selected options. * Updates the select all option based on the currently displayed and selected checkboxes.
*/ */
updateSelectAll: function() { updateSelectAll: function() {
if (this.hasSelectAll()) { if (this.hasSelectAll()) {
var selected = this.getSelected(); var allBoxes = $("li:not(.multiselect-item) input:enabled", this.$ul).filter(":visible"),
allBoxesLength = allBoxes.length,
checkedBoxesLength = allBoxes.filter(":checked").length;
if (selected.length === $('option:not([data-role=divider])', this.$select).length - 1) { if (checkedBoxesLength > 0 && checkedBoxesLength === allBoxesLength) {
this.select(this.options.selectAllValue); this.select(this.options.selectAllValue);
} }
else { else {
...@@ -915,9 +950,7 @@ ...@@ -915,9 +950,7 @@
* @returns {jQUery} * @returns {jQUery}
*/ */
getSelected: function() { getSelected: function() {
return $('option[value!="' + this.options.selectAllValue + '"]:selected', this.$select).filter(function() { return $('option:not([value="' + this.options.selectAllValue + '"])', this.$select).filter(":selected");
return $(this).prop('selected');
});
}, },
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment