Commit 614bd4b5 authored by David Stutz's avatar David Stutz

Merge pull request #439 from Tyf0x/shift-select-improvement

Shift + Click range selection improvements. Fixes #431.
parents 847873e4 1bb4af91
......@@ -99,6 +99,7 @@
this.originalOptions = this.$select.clone()[0].options;
this.query = '';
this.searchTimeout = null;
this.lastToggledInput = null
this.options.multiple = this.$select.attr('multiple') === "multiple";
this.options.onChange = $.proxy(this.options.onChange, this);
......@@ -261,7 +262,7 @@
ul: '<ul class="multiselect-container dropdown-menu"></ul>',
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>',
filterClearBtn: '<span class="input-group-btn"><button class="btn btn-default multiselect-clear-filter" type="button"><i class="glyphicon glyphicon-remove-circle"></i></button></span>',
li: '<li><a href="javascript:void(0);"><label></label></a></li>',
li: '<li><a tabindex="0"><label></label></a></li>',
divider: '<li class="multiselect-item divider"></li>',
liGroup: '<li class="multiselect-item multiselect-group"><label></label></li>'
}
......@@ -451,50 +452,69 @@
}
}, this));
$('li a', this.$ul).on('touchstart click', function(event) {
$('li a', this.$ul).on('mousedown', function(e) {
if (e.shiftKey) {
// Prevent selecting text by Shift+click
return false;
}
});
$('li a', this.$ul).on('touchstart click', $.proxy(function(event) {
event.stopPropagation();
var $target = $(event.target);
if (document.getSelection().type === 'Range') {
var $input = $(this).find("input:first");
$input.prop("checked", !$input.prop("checked"))
.trigger("change");
}
if (event.shiftKey) {
if (event.shiftKey && this.options.multiple) {
if($target.is("label")){ // Handles checkbox selection manually (see https://github.com/davidstutz/bootstrap-multiselect/issues/431)
event.preventDefault();
$target = $target.find("input");
$target.prop("checked", !$target.prop("checked"));
}
var checked = $target.prop('checked') || false;
if (checked) {
var prev = $target.closest('li')
.siblings('li[class="active"]:first');
var currentIdx = $target.closest('li')
.index();
var prevIdx = prev.index();
if (currentIdx > prevIdx) {
$target.closest("li").prevUntil(prev).each(
function() {
$(this).find("input:first").prop("checked", true)
.trigger("change");
}
);
if (this.lastToggledInput !== null && this.lastToggledInput !== $target) { // Make sure we actually have a range
var from = $target.closest("li").index();
var to = this.lastToggledInput.closest("li").index();
if (from > to) { // Swap the indices
var tmp = to;
to = from;
from = tmp;
}
else {
$target.closest("li").nextUntil(prev).each(
function() {
$(this).find("input:first").prop("checked", true)
.trigger("change");
}
);
// Make sure we grab all elements since slice excludes the last index
++to;
// Change the checkboxes and underlying options
var range = this.$ul.find("li").slice(from, to).find("input");
range.prop('checked', checked);
if (this.options.selectedClass) {
range.closest('li')
.toggleClass(this.options.selectedClass, checked);
}
for (var i = 0, j = range.length; i < j; i++) {
var $checkbox = $(range[i]);
var $option = this.getOptionByValue($checkbox.val());
$option.prop('selected', checked);
}
}
// Trigger the select "change" event
$target.trigger("change");
}
// Remembers last clicked option
if($target.is("input") && !$target.closest("li").is(".multiselect-item")){
this.lastToggledInput = $target;
}
$target.blur();
});
}, this));
// Keyboard support.
this.$container.off('keydown.multiselect').on('keydown.multiselect', $.proxy(function(event) {
......
......@@ -596,7 +596,7 @@ describe('Bootstrap Multiselect Specific Issues', function() {
selection.addRange(range);
if (document.getSelection().type === 'Range') {
$('#multiselect-container').find('a:first').trigger('click');
$('#multiselect-container').find('a:first label').trigger('click');
expect($('#multiselect-container').find('input:first').prop('checked')).toBe(true);
}
......
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