Skip to content

Commit

Permalink
Merge pull request #296 from wincelau/followlink
Browse files Browse the repository at this point in the history
Add options to use href for each items
  • Loading branch information
bassjobsen authored Oct 5, 2017
2 parents d7b18ad + a96b931 commit 4d9b9af
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions bootstrap3-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
this.render = this.options.render || this.render;
this.updater = this.options.updater || this.updater;
this.displayText = this.options.displayText || this.displayText;
this.itemLink = this.options.itemLink || this.itemLink;
this.followLinkOnSelect = this.options.followLinkOnSelect || this.followLinkOnSelect;
this.source = this.options.source;
this.delay = this.options.delay;
this.$menu = $(this.options.menu);
Expand All @@ -66,6 +68,7 @@
this.listen();
this.showHintOnFocus = typeof this.options.showHintOnFocus == 'boolean' || this.options.showHintOnFocus === "all" ? this.options.showHintOnFocus : false;
this.afterSelect = this.options.afterSelect;
this.afterEmptySelect = this.options.afterEmptySelect;
this.addItem = false;
this.value = this.$element.val() || this.$element.text();
this.keyPressed = false;
Expand All @@ -77,22 +80,34 @@
constructor: Typeahead,

select: function () {
var val = this.$menu.find('.active').data('value');
this.$element.data('active', val);
if (this.autoSelect || val) {
var newVal = this.updater(val);
// Updater can be set to any random functions via "options" parameter in constructor above.
// Add null check for cases when updater returns void or undefined.
if (!newVal) {
newVal = '';
var val = this.$menu.find('.active').data('value');

this.$element.data('active', val);
if (this.autoSelect || val) {
var newVal = this.updater(val);
// Updater can be set to any random functions via "options" parameter in constructor above.
// Add null check for cases when updater returns void or undefined.
if (!newVal) {
newVal = '';
}
this.$element
.val(this.displayText(newVal) || newVal)
.text(this.displayText(newVal) || newVal)
.change();
this.afterSelect(newVal);
if(this.followLinkOnSelect && this.itemLink(val)) {
document.location = this.itemLink(val);
this.afterSelect(newVal);
} else if(this.followLinkOnSelect && !this.itemLink(val)) {
this.afterEmptySelect(newVal);
} else {
this.afterSelect(newVal);
}
} else {
this.afterEmptySelect(newVal);
}
this.$element
.val(this.displayText(newVal) || newVal)
.text(this.displayText(newVal) || newVal)
.change();
this.afterSelect(newVal);
}
return this.hide();

return this.hide();
},

updater: function (item) {
Expand Down Expand Up @@ -312,6 +327,9 @@
var text = self.displayText(item);
i = $(that.options.item).data('value', item);
i.find('a').html(that.highlighter(text, item));
if(this.followLinkOnSelect) {
i.find('a').attr('href', self.itemLink(item));
}
if (text == self.$element.val()) {
i.addClass('active');
self.$element.data('active', item);
Expand All @@ -332,6 +350,10 @@
return typeof item !== 'undefined' && typeof item.name != 'undefined' ? item.name : item;
},

itemLink: function (item) {
return null;
},

next: function (event) {
var active = this.$menu.find('.active').removeClass('active');
var next = active.next();
Expand Down Expand Up @@ -602,7 +624,9 @@
scrollHeight: 0,
autoSelect: true,
afterSelect: $.noop,
afterEmptySelect: $.noop,
addItem: false,
followLinkOnSelect: false,
delay: 0,
separator: 'category',
headerHtml: '<li class="dropdown-header"></li>',
Expand Down

0 comments on commit 4d9b9af

Please sign in to comment.