From d556a873e7fa6138e4f630d3859f9aed9b975a29 Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Sun, 5 Mar 2017 20:35:32 -0600 Subject: [PATCH] Hide suggestions on blur, remove document click listeners. Fixes #447. --- src/jquery.autocomplete.js | 58 ++++++++------------------------------ 1 file changed, 12 insertions(+), 46 deletions(-) diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index c9ade6c6..6c562bcf 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -156,8 +156,6 @@ Autocomplete.prototype = { - killerFn: null, - initialize: function () { var that = this, suggestionSelector = '.' + that.classes.suggestion, @@ -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 = $('
') .html(this.options.noSuggestionNotice).get(0); @@ -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(); @@ -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 () { @@ -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, @@ -876,7 +844,6 @@ var that = this; that.hide(); that.onSelect(i); - that.disableKillerFn(); }, moveUp: function () { @@ -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(); }