Skip to content

Commit

Permalink
Merge pull request #1604 from andrews05/keydown
Browse files Browse the repository at this point in the history
Keydown improvements
  • Loading branch information
caseyjhol authored Feb 7, 2017
2 parents 52e0c47 + ccd0f80 commit 7448cc9
Showing 1 changed file with 15 additions and 57 deletions.
72 changes: 15 additions & 57 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1538,11 +1538,6 @@
$items,
that = $parent.data('this'),
index,
next,
first,
last,
prev,
nextPrev,
prevIndex,
isActive,
selector = ':not(.disabled, .hidden, .dropdown-header, .divider)',
Expand Down Expand Up @@ -1597,11 +1592,6 @@
105: '9'
};

if (that.options.liveSearch) $parent = $this.parent().parent();

if (that.options.container) $parent = that.$menu;

$items = $('[role="listbox"] li', $parent);

isActive = that.$newElement.hasClass('open');

Expand All @@ -1624,60 +1614,29 @@
that.$menuInner.click();
that.$button.focus();
}
// $items contains li elements when liveSearch is enabled
$items = $('[role="listbox"] li' + selector, $parent);
if (!$this.val() && !/(38|40)/.test(e.keyCode.toString(10))) {
if ($items.filter('.active').length === 0) {
$items = that.$menuInner.find('li');
if (that.options.liveSearchNormalize) {
$items = $items.filter(':a' + that._searchStyle() + '(' + normalizeToBase(keyCodeMap[e.keyCode]) + ')');
} else {
$items = $items.filter(':' + that._searchStyle() + '(' + keyCodeMap[e.keyCode] + ')');
}
}
}
}

if (!$items.length) return;

if (/(38|40)/.test(e.keyCode.toString(10))) {
index = $items.index($items.find('a').filter(':focus').parent());
first = $items.filter(selector).first().index();
last = $items.filter(selector).last().index();
next = $items.eq(index).nextAll(selector).eq(0).index();
prev = $items.eq(index).prevAll(selector).eq(0).index();
nextPrev = $items.eq(next).prevAll(selector).eq(0).index();
$items = that.$lis.filter(selector);
if (!$items.length) return;

if (that.options.liveSearch) {
$items.each(function (i) {
if (!$(this).hasClass('disabled')) {
$(this).data('index', i);
}
});
if (!that.options.liveSearch) {
index = $items.index($items.find('a').filter(':focus').parent());
} else {
index = $items.index($items.filter('.active'));
first = $items.first().data('index');
last = $items.last().data('index');
next = $items.eq(index).nextAll().eq(0).data('index');
prev = $items.eq(index).prevAll().eq(0).data('index');
nextPrev = $items.eq(next).prevAll().eq(0).data('index');
}

prevIndex = $this.data('prevIndex');
prevIndex = that.$menuInner.data('prevIndex');

if (e.keyCode == 38) {
if (that.options.liveSearch) index--;
if (index != nextPrev && index > prev) index = prev;
if (index < first) index = first;
if (index == prevIndex) index = last;
if ((that.options.liveSearch || index == prevIndex) && index != -1) index--;
if (index < 0) index += $items.length;
} else if (e.keyCode == 40) {
if (that.options.liveSearch) index++;
if (index == -1) index = 0;
if (index != nextPrev && index < next) index = next;
if (index > last) index = last;
if (index == prevIndex) index = first;
if (that.options.liveSearch || index == prevIndex) index++;
index = index % $items.length;
}

$this.data('prevIndex', index);
that.$menuInner.data('prevIndex', index);

if (!that.options.liveSearch) {
$items.eq(index).children('a').focus();
Expand All @@ -1694,11 +1653,10 @@
count,
prevKey;

$items.each(function () {
if (!$(this).hasClass('disabled')) {
if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
keyIndex.push($(this).index());
}
$items = that.$lis.filter(selector);
$items.each(function (i) {
if ($.trim($(this).children('a').text().toLowerCase()).substring(0, 1) == keyCodeMap[e.keyCode]) {
keyIndex.push(i);
}
});

Expand Down

0 comments on commit 7448cc9

Please sign in to comment.