Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Scrolling + Carousel Option #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions bootstrap3-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@
var next = active.next();

if (!next.length) {
next = $(this.$menu.find($(this.options.item || this.theme.item).prop('tagName'))[0]);
if (this.options.carousel) {
next = $(this.$menu.find($(this.options.item || this.theme.item).prop('tagName'))[0]);
} else {
next = active; // stop at end of list
}
}

while (next.hasClass('divider') || next.hasClass('dropdown-header')) {
Expand All @@ -419,14 +423,26 @@
if (this.changeInputOnMove) {
this.$element.val(this.displayText(newVal) || newVal);
}

// added to scroll parent container
var offset_in_list = next.offset().top - next.closest('ul').offset().top;
var item_height = next.outerHeight();

if (offset_in_list > next.closest('ul').height()) {
next.closest('ul').scrollTop(next.closest('ul').scrollTop() + item_height);
}
},

prev: function (event) {
var active = this.$menu.find('.active').removeClass('active');
var prev = active.prev();

if (!prev.length) {
prev = this.$menu.find($(this.options.item || this.theme.item).prop('tagName')).last();
if (this.options.carousel) {
prev = this.$menu.find($(this.options.item || this.theme.item).prop('tagName')).last();
} else {
prev = active; // stop at start of list
}
}

while (prev.hasClass('divider') || prev.hasClass('dropdown-header')) {
Expand All @@ -439,6 +455,14 @@
if (this.changeInputOnMove) {
this.$element.val(this.displayText(newVal) || newVal);
}

// added to scroll parent container
var offset_in_list = prev.offset().top - prev.closest('ul').offset().top;
var item_height = prev.outerHeight();

if (offset_in_list < 0) {
prev.closest('ul').scrollTop(prev.closest('ul').scrollTop() - item_height);
}
},

listen: function () {
Expand Down Expand Up @@ -716,6 +740,7 @@
Typeahead.defaults = {
source: [],
items: 8,
carousel: true,
minLength: 1,
scrollHeight: 0,
autoSelect: true,
Expand Down