Skip to content

Commit

Permalink
Hide suggestions on blur, remove document click listeners. Fixes #447.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kirda committed Mar 6, 2017
1 parent 1eff030 commit d556a87
Showing 1 changed file with 12 additions and 46 deletions.
58 changes: 12 additions & 46 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@

Autocomplete.prototype = {

killerFn: null,

initialize: function () {
var that = this,
suggestionSelector = '.' + that.classes.suggestion,
Expand All @@ -168,13 +166,6 @@
// Remove autocomplete attribute to prevent native suggestions:
that.element.setAttribute('autocomplete', 'off');

that.killerFn = function (e) {
if (!$(e.target).closest('.' + that.options.containerClass).length) {
that.killSuggestions();
that.disableKillerFn();
}
};

// html() deals with many types: htmlString or Element or Array or jQuery
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
.html(this.options.noSuggestionNotice).get(0);
Expand All @@ -201,12 +192,16 @@
container.children('.' + selected).removeClass(selected);
});


// Listen for click event on suggestions list:
container.on('click.autocomplete', suggestionSelector, function () {
that.select($(this).data('index'));
return false;
});

container.on('click.autocomplete', function () {
clearTimeout(that.blurTimeoutId);
})

that.fixPositionCapture = function () {
if (that.visible) {
that.fixPosition();
Expand Down Expand Up @@ -234,7 +229,13 @@
},

onBlur: function () {
this.enableKillerFn();
var that = this;

// If user clicked on a suggestion, hide() will
// be canceled, otherwise close suggestions
that.blurTimeoutId = setTimeout(function () {
that.hide();
}, 200);
},

abortAjax: function () {
Expand Down Expand Up @@ -350,39 +351,6 @@
$container.css(styles);
},

enableKillerFn: function () {
var that = this;
$(document).on('click.autocomplete', that.killerFn);
},

disableKillerFn: function () {
var that = this;
$(document).off('click.autocomplete', that.killerFn);
},

killSuggestions: function () {
var that = this;
that.stopKillSuggestions();
that.timeoutId = setTimeout(function () {
if (that.visible) {
// No need to restore value when
// preserveInput === true,
// because we did not change it
if (!that.options.preserveInput) {
that.el.val(that.currentValue);
}

that.hide();
}

that.stopKillSuggestions();
}, 50);
},

stopKillSuggestions: function () {
clearTimeout(this.timeoutId);
},

isCursorAtEnd: function () {
var that = this,
valLength = that.el.val().length,
Expand Down Expand Up @@ -876,7 +844,6 @@
var that = this;
that.hide();
that.onSelect(i);
that.disableKillerFn();
},

moveUp: function () {
Expand Down Expand Up @@ -979,7 +946,6 @@
dispose: function () {
var that = this;
that.el.off('.autocomplete').removeData('autocomplete');
that.disableKillerFn();
$(window).off('resize.autocomplete', that.fixPositionCapture);
$(that.suggestionsContainer).remove();
}
Expand Down

0 comments on commit d556a87

Please sign in to comment.