Skip to content

Commit d556a87

Browse files
author
Tomas Kirda
committed
Hide suggestions on blur, remove document click listeners. Fixes #447.
1 parent 1eff030 commit d556a87

File tree

1 file changed

+12
-46
lines changed

1 file changed

+12
-46
lines changed

src/jquery.autocomplete.js

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@
156156

157157
Autocomplete.prototype = {
158158

159-
killerFn: null,
160-
161159
initialize: function () {
162160
var that = this,
163161
suggestionSelector = '.' + that.classes.suggestion,
@@ -168,13 +166,6 @@
168166
// Remove autocomplete attribute to prevent native suggestions:
169167
that.element.setAttribute('autocomplete', 'off');
170168

171-
that.killerFn = function (e) {
172-
if (!$(e.target).closest('.' + that.options.containerClass).length) {
173-
that.killSuggestions();
174-
that.disableKillerFn();
175-
}
176-
};
177-
178169
// html() deals with many types: htmlString or Element or Array or jQuery
179170
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
180171
.html(this.options.noSuggestionNotice).get(0);
@@ -201,12 +192,16 @@
201192
container.children('.' + selected).removeClass(selected);
202193
});
203194

195+
204196
// Listen for click event on suggestions list:
205197
container.on('click.autocomplete', suggestionSelector, function () {
206198
that.select($(this).data('index'));
207-
return false;
208199
});
209200

201+
container.on('click.autocomplete', function () {
202+
clearTimeout(that.blurTimeoutId);
203+
})
204+
210205
that.fixPositionCapture = function () {
211206
if (that.visible) {
212207
that.fixPosition();
@@ -234,7 +229,13 @@
234229
},
235230

236231
onBlur: function () {
237-
this.enableKillerFn();
232+
var that = this;
233+
234+
// If user clicked on a suggestion, hide() will
235+
// be canceled, otherwise close suggestions
236+
that.blurTimeoutId = setTimeout(function () {
237+
that.hide();
238+
}, 200);
238239
},
239240

240241
abortAjax: function () {
@@ -350,39 +351,6 @@
350351
$container.css(styles);
351352
},
352353

353-
enableKillerFn: function () {
354-
var that = this;
355-
$(document).on('click.autocomplete', that.killerFn);
356-
},
357-
358-
disableKillerFn: function () {
359-
var that = this;
360-
$(document).off('click.autocomplete', that.killerFn);
361-
},
362-
363-
killSuggestions: function () {
364-
var that = this;
365-
that.stopKillSuggestions();
366-
that.timeoutId = setTimeout(function () {
367-
if (that.visible) {
368-
// No need to restore value when
369-
// preserveInput === true,
370-
// because we did not change it
371-
if (!that.options.preserveInput) {
372-
that.el.val(that.currentValue);
373-
}
374-
375-
that.hide();
376-
}
377-
378-
that.stopKillSuggestions();
379-
}, 50);
380-
},
381-
382-
stopKillSuggestions: function () {
383-
clearTimeout(this.timeoutId);
384-
},
385-
386354
isCursorAtEnd: function () {
387355
var that = this,
388356
valLength = that.el.val().length,
@@ -876,7 +844,6 @@
876844
var that = this;
877845
that.hide();
878846
that.onSelect(i);
879-
that.disableKillerFn();
880847
},
881848

882849
moveUp: function () {
@@ -979,7 +946,6 @@
979946
dispose: function () {
980947
var that = this;
981948
that.el.off('.autocomplete').removeData('autocomplete');
982-
that.disableKillerFn();
983949
$(window).off('resize.autocomplete', that.fixPositionCapture);
984950
$(that.suggestionsContainer).remove();
985951
}

0 commit comments

Comments
 (0)