2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-25 14:17:45 +00:00

Hide suggestions on blur, remove document click listeners. Fixes #447.

This commit is contained in:
Tomas Kirda 2017-03-05 20:35:32 -06:00
parent 1eff0300eb
commit d556a873e7

View File

@ -156,8 +156,6 @@
Autocomplete.prototype = { Autocomplete.prototype = {
killerFn: null,
initialize: function () { initialize: function () {
var that = this, var that = this,
suggestionSelector = '.' + that.classes.suggestion, suggestionSelector = '.' + that.classes.suggestion,
@ -168,13 +166,6 @@
// Remove autocomplete attribute to prevent native suggestions: // Remove autocomplete attribute to prevent native suggestions:
that.element.setAttribute('autocomplete', 'off'); 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 // html() deals with many types: htmlString or Element or Array or jQuery
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>') that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
.html(this.options.noSuggestionNotice).get(0); .html(this.options.noSuggestionNotice).get(0);
@ -201,12 +192,16 @@
container.children('.' + selected).removeClass(selected); container.children('.' + selected).removeClass(selected);
}); });
// Listen for click event on suggestions list: // Listen for click event on suggestions list:
container.on('click.autocomplete', suggestionSelector, function () { container.on('click.autocomplete', suggestionSelector, function () {
that.select($(this).data('index')); that.select($(this).data('index'));
return false;
}); });
container.on('click.autocomplete', function () {
clearTimeout(that.blurTimeoutId);
})
that.fixPositionCapture = function () { that.fixPositionCapture = function () {
if (that.visible) { if (that.visible) {
that.fixPosition(); that.fixPosition();
@ -234,7 +229,13 @@
}, },
onBlur: function () { 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 () { abortAjax: function () {
@ -350,39 +351,6 @@
$container.css(styles); $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 () { isCursorAtEnd: function () {
var that = this, var that = this,
valLength = that.el.val().length, valLength = that.el.val().length,
@ -876,7 +844,6 @@
var that = this; var that = this;
that.hide(); that.hide();
that.onSelect(i); that.onSelect(i);
that.disableKillerFn();
}, },
moveUp: function () { moveUp: function () {
@ -979,7 +946,6 @@
dispose: function () { dispose: function () {
var that = this; var that = this;
that.el.off('.autocomplete').removeData('autocomplete'); that.el.off('.autocomplete').removeData('autocomplete');
that.disableKillerFn();
$(window).off('resize.autocomplete', that.fixPositionCapture); $(window).off('resize.autocomplete', that.fixPositionCapture);
$(that.suggestionsContainer).remove(); $(that.suggestionsContainer).remove();
} }