|
156 | 156 |
|
157 | 157 | Autocomplete.prototype = { |
158 | 158 |
|
159 | | - killerFn: null, |
160 | | - |
161 | 159 | initialize: function () { |
162 | 160 | var that = this, |
163 | 161 | suggestionSelector = '.' + that.classes.suggestion, |
|
168 | 166 | // Remove autocomplete attribute to prevent native suggestions: |
169 | 167 | that.element.setAttribute('autocomplete', 'off'); |
170 | 168 |
|
171 | | - that.killerFn = function (e) { |
172 | | - if (!$(e.target).closest('.' + that.options.containerClass).length) { |
173 | | - that.killSuggestions(); |
174 | | - that.disableKillerFn(); |
175 | | - } |
176 | | - }; |
177 | | - |
178 | 169 | // html() deals with many types: htmlString or Element or Array or jQuery |
179 | 170 | that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>') |
180 | 171 | .html(this.options.noSuggestionNotice).get(0); |
|
201 | 192 | container.children('.' + selected).removeClass(selected); |
202 | 193 | }); |
203 | 194 |
|
| 195 | + |
204 | 196 | // Listen for click event on suggestions list: |
205 | 197 | container.on('click.autocomplete', suggestionSelector, function () { |
206 | 198 | that.select($(this).data('index')); |
207 | | - return false; |
208 | 199 | }); |
209 | 200 |
|
| 201 | + container.on('click.autocomplete', function () { |
| 202 | + clearTimeout(that.blurTimeoutId); |
| 203 | + }) |
| 204 | + |
210 | 205 | that.fixPositionCapture = function () { |
211 | 206 | if (that.visible) { |
212 | 207 | that.fixPosition(); |
|
234 | 229 | }, |
235 | 230 |
|
236 | 231 | 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); |
238 | 239 | }, |
239 | 240 |
|
240 | 241 | abortAjax: function () { |
|
350 | 351 | $container.css(styles); |
351 | 352 | }, |
352 | 353 |
|
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 | | - |
386 | 354 | isCursorAtEnd: function () { |
387 | 355 | var that = this, |
388 | 356 | valLength = that.el.val().length, |
|
876 | 844 | var that = this; |
877 | 845 | that.hide(); |
878 | 846 | that.onSelect(i); |
879 | | - that.disableKillerFn(); |
880 | 847 | }, |
881 | 848 |
|
882 | 849 | moveUp: function () { |
|
979 | 946 | dispose: function () { |
980 | 947 | var that = this; |
981 | 948 | that.el.off('.autocomplete').removeData('autocomplete'); |
982 | | - that.disableKillerFn(); |
983 | 949 | $(window).off('resize.autocomplete', that.fixPositionCapture); |
984 | 950 | $(that.suggestionsContainer).remove(); |
985 | 951 | } |
|
0 commit comments