Commit e5587e3b authored by David Stutz's avatar David Stutz

#143. #183, #89, #78. But not perfect. Select all option needs to be updated...

#143. #183, #89, #78. But not perfect. Select all option needs to be updated depending on the filter.
parent 2a1be40e
......@@ -178,11 +178,16 @@
$('#example28').multiselect({
includeSelectAllOption: true,
enableFiltering: 1,
enableFiltering: true,
maxHeight: 150
});
$('#example32').multiselect();
$('#example39').multiselect({
includeSelectAllOption: true,
enableCaseInsensitiveFiltering: true
});
});
</script>
<p>
......@@ -493,6 +498,30 @@
The button will keep the <code>tabindex</code> of the select.
</td>
</tr>
<tr>
<td>
<select id="example39" multiple="multiple">
<option value="lab">Lab Course</option>
<option value="proseminar">Proseminar</option>
<optgroup label="Mathematics">
<option value="analysis">Analysis</option>
<option value="algebra">Linear Algebra</option>
<option value="discrete">Discrete Mathematics</option>
<option value="numerical">Numerical Analysis</option>
<option value="probability">Probability Theory</option>
</optgroup>
<optgroup label="Computer Science">
<option value="programming">Introduction to Programming</option>
<option value="automata">Automata Theory</option>
<option value="complexity">Complexity Theory</option>
<option value="software">Software Engineering</option>
</optgroup>
</select>
</td>
<td>
Using <code>optgroups</code>s with filtering and the select all option.
</td>
</tr>
</table>
<div class="page-header">
......@@ -939,7 +968,7 @@
<table class="table table-striped">
<tbody>
<tr>
<td>
<td width="50%">
<p><code>.multiselect('destroy')</code></p>
<p>
This method is used to destroy the plugin on the given element - meaning unbinding the plugin.
......
......@@ -10,7 +10,7 @@
"use strict";// jshint ;_;
if (typeof ko != 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect) {
if (typeof ko !== 'undefined' && ko.bindingHandlers && !ko.bindingHandlers.multiselect) {
ko.bindingHandlers.multiselect = {
init : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {},
update : function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
......@@ -37,7 +37,7 @@
this.query = '';
this.searchTimeout = null;
this.options.multiple = this.$select.attr('multiple') == "multiple";
this.options.multiple = this.$select.attr('multiple') === "multiple";
this.options.onChange = $.proxy(this.options.onChange, this);
this.options.onDropdownShow = $.proxy(this.options.onDropdownShow, this);
this.options.onDropdownHide = $.proxy(this.options.onDropdownHide, this);
......@@ -62,7 +62,7 @@
// option is selected, or a list of the selected options up to a length of 3 selected options by default.
// If more than 3 options are selected, the number of selected options is printed.
buttonText: function(options, select) {
if (options.length == 0) {
if (options.length === 0) {
return this.nonSelectedText + ' <b class="caret"></b>';
}
else {
......@@ -82,7 +82,7 @@
},
// Like the buttonText option to update the title of the button.
buttonTitle: function(options, select) {
if (options.length == 0) {
if (options.length === 0) {
return this.nonSelectedText;
}
else {
......@@ -173,7 +173,7 @@
this.$button.attr('tabindex', tabindex);
}
this.$container.prepend(this.$button)
this.$container.prepend(this.$button);
},
// Build dropdown container ul.
......@@ -196,7 +196,7 @@
});
}
this.$container.append(this.$ul)
this.$container.append(this.$ul);
},
// Build the dropdown and bind event handling.
......@@ -204,11 +204,13 @@
this.$select.children().each($.proxy(function(index, element) {
// Support optgroups and options without a group simultaneously.
var tag = $(element).prop('tagName').toLowerCase();
if (tag == 'optgroup') {
var tag = $(element).prop('tagName')
.toLowerCase();
if (tag === 'optgroup') {
this.createOptgroup(element);
}
else if (tag == 'option') {
else if (tag === 'option') {
this.createOptionValue(element);
}
// Other illegal tags will be ignored.
......@@ -217,15 +219,17 @@
// Bind the change event on the dropdown elements.
$('li input', this.$ul).on('change', $.proxy(function(event) {
var checked = $(event.target).prop('checked') || false;
var isSelectAllOption = $(event.target).val() == this.options.selectAllValue;
var isSelectAllOption = $(event.target).val() === this.options.selectAllValue;
// Apply or unapply the configured selected class.
if (this.options.selectedClass) {
if (checked) {
$(event.target).parents('li').addClass(this.options.selectedClass);
$(event.target).parents('li')
.addClass(this.options.selectedClass);
}
else {
$(event.target).parents('li').removeClass(this.options.selectedClass);
$(event.target).parents('li')
.removeClass(this.options.selectedClass);
}
}
......@@ -236,17 +240,23 @@
var $optionsNotThis = $('option', this.$select).not($option);
var $checkboxesNotThis = $('input', this.$container).not($(event.target));
// Toggle all options if the select all option was changed.
if (isSelectAllOption) {
if (this.$select[0][0].value == this.options.selectAllValue) {
if (this.$select[0][0].value === this.options.selectAllValue) {
var values = [];
var options = this.$select.children();
for(var i = 0; i < $checkboxesNotThis.length; i++) {
if (options[i].value !== this.options.selectAllValue) {
var options = $('option[value!="' + this.options.selectAllValue + '"]', this.$select);
for (var i = 0; i < options.length; i++) {
// Additionally check whether the option is visible within the dropcown.
if (options[i].value !== this.options.selectAllValue && this.getInputByValue(options[i].value).is(':visible')) {
values.push(options[i].value);
}
}
checked ? this.select(values) : this.deselect(values)
if (checked) {
this.select(values);
}
else {
this.deselect(values);
}
}
}
......@@ -270,7 +280,7 @@
this.$button.click();
}
if (this.options.selectedClass == "active") {
if (this.options.selectedClass === "active") {
$optionsNotThis.parents("a").css("outline", "");
}
}
......@@ -291,33 +301,36 @@
$('li a', this.$ul).on('touchstart click', function(event) {
event.stopPropagation();
if ( event.shiftKey) {
if (event.shiftKey) {
var checked = $(event.target).prop('checked') || false;
if ( checked ) {
if (checked) {
var prev = $(event.target).parents('li:last')
.siblings('li[class="active"]:first');
var currentIdx = $(event.target).parents('li').index(),
prevIdx = prev.index();
var currentIdx = $(event.target).parents('li')
.index();
var prevIdx = prev.index();
if ( currentIdx > prevIdx ) {
if (currentIdx > prevIdx) {
$(event.target).parents("li:last").prevUntil(prev).each(
function() {
$(this).find("input:first").prop("checked", true).trigger("change");
$(this).find("input:first").prop("checked", true)
.trigger("change");
}
);
} else {
}
else {
$(event.target).parents("li:last").nextUntil(prev).each(
function() {
$(this).find("input:first").prop("checked", true).trigger("change");
$(this).find("input:first").prop("checked", true)
.trigger("change");
}
);
}
}
}
$(event.target).blur();
});
......@@ -326,7 +339,7 @@
if ($('input[type="text"]', this.$container).is(':focus')) {
return;
}
if ((event.keyCode == 9 || event.keyCode == 27) && this.$container.hasClass('open')) {
if ((event.keyCode === 9 || event.keyCode === 27) && this.$container.hasClass('open')) {
// Close on tab or escape.
this.$button.click();
}
......@@ -340,11 +353,11 @@
var index = $items.index($items.filter(':focus'));
// Navigation up.
if (event.keyCode == 38 && index > 0) {
if (event.keyCode === 38 && index > 0) {
index--;
}
// Navigate down.
else if (event.keyCode == 40 && index < $items.length - 1) {
else if (event.keyCode === 40 && index < $items.length - 1) {
index++;
}
else if (!~index) {
......@@ -354,7 +367,7 @@
var $current = $items.eq(index);
$current.focus();
if (event.keyCode == 32 || event.keyCode == 13) {
if (event.keyCode === 32 || event.keyCode === 13) {
var $checkbox = $current.find('input');
$checkbox.prop("checked", !$checkbox.prop("checked"));
......@@ -386,8 +399,9 @@
var $checkbox = $('input', $li);
$checkbox.val(value);
if (value == this.options.selectAllValue) {
$checkbox.parent().parent().addClass('multiselect-all');
if (value === this.options.selectAllValue) {
$checkbox.parent().parent()
.addClass('multiselect-all');
}
$('label', $li).append(" " + label);
......@@ -395,13 +409,17 @@
this.$ul.append($li);
if ($(element).is(':disabled')) {
$checkbox.attr('disabled', 'disabled').prop('disabled', true).parents('li').addClass('disabled');
$checkbox.attr('disabled', 'disabled')
.prop('disabled', true)
.parents('li')
.addClass('disabled');
}
$checkbox.prop('checked', selected);
if (selected && this.options.selectedClass) {
$checkbox.parents('li').addClass(this.options.selectedClass);
$checkbox.parents('li')
.addClass(this.options.selectedClass);
}
},
......@@ -423,7 +441,8 @@
// Add the select all option to the select.
buildSelectAll: function() {
var alreadyHasSelectAll = this.$select[0][0] ? this.$select[0][0].value == this.options.selectAllValue : false;
var alreadyHasSelectAll = this.$select[0][0] ? this.$select[0][0].value === this.options.selectAllValue : false;
// If options.includeSelectAllOption === true, add the include all checkbox.
if (this.options.includeSelectAllOption && this.options.multiple && !alreadyHasSelectAll) {
this.$select.prepend('<option value="' + this.options.selectAllValue + '">' + this.options.selectAllText + '</option>');
......@@ -436,6 +455,7 @@
// Build filter if filtering OR case insensitive filtering is enabled and the number of options exceeds (or equals) enableFilterLength.
if (this.options.enableFiltering || this.options.enableCaseInsensitiveFiltering) {
var enableFilterLength = Math.max(this.options.enableFiltering, this.options.enableCaseInsensitiveFiltering);
if (this.$select.find('option').length >= enableFilterLength) {
this.$filter = $(this.templates.filter);
......@@ -450,24 +470,23 @@
this.searchTimeout = this.asyncFunction($.proxy(function() {
if (this.query != event.target.value) {
if (this.query !== event.target.value) {
this.query = event.target.value;
$.each($('li', this.$ul), $.proxy(function(index, element) {
var value = $('input', element).val();
if (value != this.options.selectAllValue) {
var text = $('label', element).text();
var value = $('input', element).val();
if (value && text && value != this.options.selectAllValue) {
if (value !== this.options.selectAllValue && text) {
// by default lets assume that element is not
// interesting for this search
var showElement = false;
var filterCandidate = '';
if ((this.options.filterBehavior == 'text' || this.options.filterBehavior == 'both')) {
if ((this.options.filterBehavior === 'text' || this.options.filterBehavior === 'both')) {
filterCandidate = text;
}
if ((this.options.filterBehavior == 'value' || this.options.filterBehavior == 'both')) {
if ((this.options.filterBehavior === 'value' || this.options.filterBehavior === 'both')) {
filterCandidate = value;
}
......@@ -485,9 +504,10 @@
$(element).hide();
}
}
}
}, this));
}
// TODO: check whether select all option needs to be updated.
}, this), 300, this);
}, this));
}
......@@ -504,29 +524,36 @@
refresh: function() {
$('option', this.$select).each($.proxy(function(index, element) {
var $input = $('li input', this.$ul).filter(function() {
return $(this).val() == $(element).val();
return $(this).val() === $(element).val();
});
if ($(element).is(':selected')) {
$input.prop('checked', true);
if (this.options.selectedClass) {
$input.parents('li').addClass(this.options.selectedClass);
$input.parents('li')
.addClass(this.options.selectedClass);
}
}
else {
$input.prop('checked', false);
if (this.options.selectedClass) {
$input.parents('li').removeClass(this.options.selectedClass);
$input.parents('li')
.removeClass(this.options.selectedClass);
}
}
if ($(element).is(":disabled")) {
$input.attr('disabled', 'disabled').prop('disabled', true).parents('li').addClass('disabled');
$input.attr('disabled', 'disabled')
.prop('disabled', true)
.parents('li')
.addClass('disabled');
}
else {
$input.prop('disabled', false).parents('li').removeClass('disabled');
$input.prop('disabled', false)
.parents('li')
.removeClass('disabled');
}
}, this));
......@@ -540,14 +567,14 @@
}
for (var i = 0; i < selectValues.length; i++) {
var value = selectValues[i];
var $option = this.getOptionByValue(value);
var $checkbox = this.getInputByValue(value);
if (this.options.selectedClass) {
$checkbox.parents('li').addClass(this.options.selectedClass);
$checkbox.parents('li')
.addClass(this.options.selectedClass);
}
$checkbox.prop('checked', true);
......@@ -572,7 +599,8 @@
var $checkbox = this.getInputByValue(value);
if (this.options.selectedClass) {
$checkbox.parents('li').removeClass(this.options.selectedClass);
$checkbox.parents('li')
.removeClass(this.options.selectedClass);
}
$checkbox.prop('checked', false);
......@@ -591,7 +619,7 @@
$('option[value="' + this.options.selectAllValue + '"]', this.$select).remove();
// Important to distinguish between radios and checkboxes.
this.options.multiple = this.$select.attr('multiple') == "multiple";
this.options.multiple = this.$select.attr('multiple') === "multiple";
this.buildSelectAll();
this.buildDropdownOptions();
......@@ -656,14 +684,14 @@
// Get the corresponding option by ts value.
getOptionByValue: function(value) {
return $('option', this.$select).filter(function() {
return $(this).val() == value;
return $(this).val() === value;
});
},
// Get an input in the dropdown by its value.
getInputByValue: function(value) {
return $('li input', this.$ul).filter(function() {
return $(this).val() == value;
return $(this).val() === value;
});
},
......@@ -681,7 +709,8 @@
$.fn.multiselect = function(option, parameter) {
return this.each(function() {
var data = $(this).data('multiselect'), options = typeof option == 'object' && option;
var data = $(this).data('multiselect');
var options = typeof option === 'object' && option;
// Initialize the multiselect.
if (!data) {
......@@ -689,7 +718,7 @@
}
// Call multiselect method.
if (typeof option == 'string') {
if (typeof option === 'string') {
data[option](parameter);
}
});
......
......@@ -138,19 +138,6 @@
</td>
<td>Everything fine.</td>
</tr>
<tr id="test-select-all-tr" class="success">
<th>Test select all</th>
<td>
<select id="test-select-all-select" multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<td>Everything fine.</td>
</tr>
</tbody>
</table>
</div>
......@@ -161,31 +148,31 @@
var build = function(select, tr) {
select.multiselect();
if (select.length == 0) {
if (select.length === 0) {
return 'Select not present anymore.';
}
if (select.css('display') != 'none') {
if (select.css('display') !== 'none') {
return 'Select still visible (expected <code>display: none;</code>).';
}
if ($('button.multiselect', tr).length == 0) {
if ($('button.multiselect', tr).length === 0) {
return 'Multiselect button not present.';
}
if ($('option', select).length != 5) {
if ($('option', select).length !== 5) {
return 'Not all options present anymore.';
}
if ($('ul.multiselect-container', tr).length == 0) {
if ($('ul.multiselect-container', tr).length === 0) {
return 'Unordered list <code>.multiselect-container</code> not present.';
}
if ($('ul.multiselect-container li', tr).length != 5) {
if ($('ul.multiselect-container li', tr).length !== 5) {
return 'No list item for each option present.';
}
if ($('ul.multiselect-container li a', tr).length != 5) {
if ($('ul.multiselect-container li a', tr).length !== 5) {
return 'Not all list items come with an anchor inside.';
}
......@@ -201,23 +188,23 @@
var buildOptgroups = function(select, tr) {
select.multiselect();
if ($('optgroup', select).length != 2) {
if ($('optgroup', select).length !== 2) {
return 'Optgroups not present anymore (2 expected).';
}
var first = $('optgroup', select).get(0);
var second = $('optgroup', select).get(1);
if ($('option', $(first)).length != 2) {
if ($('option', $(first)).length !== 2) {
return 'First optgroup does not have 2 options.';
}
if ($('option', $(second)).length != 3) {
if ($('option', $(second)).length !== 3) {
return 'Second optgroup does not have 3 options.';
}
// Check the corresponding labels.
if ($('label.multiselect-group', tr).length != 2) {
if ($('label.multiselect-group', tr).length !== 2) {
return 'Expected 2 labels within the unordered list.';
}
......@@ -225,11 +212,11 @@
var firstLabel = $('label.multiselect-group', tr).get(0);
var secondLabel = $('label.multiselect-group', tr).get(1);
if ($(firstLabel).text() != $(first).prop('label')) {
if ($(firstLabel).text() !== $(first).prop('label')) {
return 'First group labeled incorrectly.';
}
if ($(secondLabel).text() != $(second).prop('label')) {
if ($(secondLabel).text() !== $(second).prop('label')) {
return 'Second group labeled incorrectly.';
}
......@@ -244,11 +231,11 @@
var buildSelected = function(select, tr ) {
select.multiselect();
if ($('option:selected', select).length != 1) {
if ($('option:selected', select).length !== 1) {
return 'Multiselect did not adopt selected options (1 selected option).';
}
if ($('ul.multiselect-container li.active', tr).length != 1) {
if ($('ul.multiselect-container li.active', tr).length !== 1) {
return 'Corresponding list item not set to <code>.active</code>.';
}
......@@ -267,7 +254,7 @@
selectAllValue: value
});
if ($('.multiselect-container input[value="' + value + '"]', tr).length != 1) {
if ($('.multiselect-container input[value="' + value + '"]', tr).length !== 1) {
return 'Expected exactly one input with value ' + value + ' as select all option.';
}
......@@ -284,7 +271,7 @@
enableFiltering: true
});
if ($('.multiselect-search', tr).length != 1) {
if ($('.multiselect-search', tr).length !== 1) {
return 'No search input present.';
}
......@@ -296,11 +283,11 @@
}
// Test select.
var select = function(select, tr) {
select.multiselect();
var select = function(selectElement, tr) {
selectElement.multiselect();
// Check for no selected options and no active li's.
if ($('option:selected', select).length > 0) {
if ($('option:selected', selectElement).length > 0) {
return 'There are already selected options (0 expected).';
}
......@@ -308,42 +295,42 @@
return 'There are already active list items (0 expected).';
}
select.multiselect('select', 1);
selectElement.multiselect('select', '1');
if ($('option:selected', select).length != 1) {
if ($('option:selected', selectElement).length !== 1) {
return 'Just selected an option - option not marked selected.';
}
if ($('ul.multiselect-container li.active', tr).length != 1) {
if ($('ul.multiselect-container li.active', tr).length !== 1) {
return 'Just selected an option - list item not set active.';
}
if ($('option:selected', select).first().val() != 1) {
if ($('option:selected', selectElement).first().val() !== '1') {
return 'Wrong option selected.';
}
select.multiselect('select', [2, 3]);
selectElement.multiselect('select', ['2', '3']);
if ($('option:selected', select).length != 3) {
if ($('option:selected', selectElement).length !== 3) {
return 'Just selected two additional options - options not marked selected.';
}
if ($('ul.multiselect-container li.active', tr).length != 3) {
if ($('ul.multiselect-container li.active', tr).length !== 3) {
return 'Just selected two additional options - list items not set active.';
}
var second = $('option:selected', select).get(1),
third = $('option:selected', select).get(2);
var second = $('option:selected', selectElement).get(1),
third = $('option:selected', selectElement).get(2);
if (second == undefined || second.length == 0) {
if (second === undefined || second.length === 0) {
return 'Could not get second option.';
}
if (third == undefined || third.length == 0) {
if (third === undefined || third.length === 0) {
return 'Could not get third option.';
}
if ($(second).val() != 2 || $(third).val() != 3) {
if ($(second).val() !== '2' || $(third).val() !== '3') {
return 'Wrong options selected.';
}
......@@ -360,29 +347,29 @@
select.multiselect();
// Check for no selected options and no active li's.
if ($('option:selected', select).length != 3) {
if ($('option:selected', select).length !== 3) {
return 'There should be 3 options selected.';
}
if ($('ul.multiselect-container li.active', tr).length != 3) {
if ($('ul.multiselect-container li.active', tr).length !== 3) {
return 'There should be 3 list items set to active.';
}
select.multiselect('deselect', 1);
select.multiselect('deselect', '1');
if ($('option:selected', select).length != 2) {
if ($('option:selected', select).length !== 2) {
return 'Just deselected an option - option not marked deselected.';
}
if ($('ul.multiselect-container li.active', tr).length != 2) {
if ($('ul.multiselect-container li.active', tr).length !== 2) {
return 'Just deselected an option - list item not set inactive.';
}
if ($('option:selected', select).first().val() != 2) {
if ($('option:selected', select).first().val() !== '2') {
return 'Wrong option deselected.';
}
select.multiselect('deselect', [2, 3]);
select.multiselect('deselect', ['2', '3']);
if ($('option:selected', select).length > 0) {
return 'Just deselected two additional options - options not marked deselected.';
......@@ -403,11 +390,11 @@
var maxHeight = function(select, tr) {
select.multiselect({
maxHeight: 100,
maxHeight: 100
});
var height = $('.multiselect-container', tr).css('max-height');
if (height != '100px') {
if (height !== '100px') {
return 'Max height not set correctly (set: ' + height + ').';
}
......@@ -418,38 +405,6 @@
$('#test-max-height-tr').removeClass('success').addClass('danger');
$('#test-max-height-tr td').last().html(maxHeight);
}
var selectAll = function(select, tr) {
var value = 'multiselect-select-all';
select.multiselect({
includeSelectAllOption: true,
selectAllValue: value
});
if ($('option:selected', select).length > 0) {
return 'Test expected 0 selected options as initial state (found ' + $('option:selected', select).length + ').';
}
// Trigger select all.
$('.multiselect-container input[value="' + value + '"]', tr).click();
if ($('option:selected', select).length != $('option', select).length) {
return 'Not all options selected.';
}
$('.multiselect-container input[value="' + value + '"]', tr).click();
if ($('option:selected', select).length > 0) {
return 'There is some option selected (0 expected).';
}
return false;
}($('#test-select-all-select'), $('#test-select-all-tr'));
if (selectAll) {
$('#test-select-all-tr').removeClass('success').addClass('danger');
$('#test-select-all-tr td').last().html(selectAll);
}
});
</script>
</body>
......
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