|
4 | 4 | * |
5 | 5 | * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. |
6 | 6 | * For details, see the web site: https://github.com/devbridge/jQuery-Autocomplete |
7 | | -* |
8 | 7 | */ |
9 | 8 |
|
10 | 9 | /*jslint browser: true, white: true, plusplus: true */ |
11 | | -/*global define, window, document, jQuery */ |
| 10 | +/*global define, window, document, jQuery, exports */ |
12 | 11 |
|
13 | 12 | // Expose plugin as an AMD module if AMD loader is present: |
14 | 13 | (function (factory) { |
15 | 14 | 'use strict'; |
16 | 15 | if (typeof define === 'function' && define.amd) { |
17 | 16 | // AMD. Register as an anonymous module. |
18 | 17 | define(['jquery'], factory); |
| 18 | + } else if (typeof exports === 'object' && typeof require === 'function') { |
| 19 | + // Browserify |
| 20 | + factory(require('jquery')); |
19 | 21 | } else { |
20 | 22 | // Browser globals |
21 | 23 | factory(jQuery); |
|
138 | 140 | suggestionSelector = '.' + that.classes.suggestion, |
139 | 141 | selected = that.classes.selected, |
140 | 142 | options = that.options, |
141 | | - container, |
142 | | - noSuggestionsContainer; |
| 143 | + container; |
143 | 144 |
|
144 | 145 | // Remove autocomplete attribute to prevent native suggestions: |
145 | 146 | that.element.setAttribute('autocomplete', 'off'); |
|
276 | 277 | if (orientation == 'auto') { |
277 | 278 | var viewPortHeight = $(window).height(), |
278 | 279 | scrollTop = $(window).scrollTop(), |
279 | | - top_overflow = -scrollTop + offset.top - containerHeight, |
280 | | - bottom_overflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); |
| 280 | + topOverflow = -scrollTop + offset.top - containerHeight, |
| 281 | + bottomOverflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); |
281 | 282 |
|
282 | | - if (Math.max(top_overflow, bottom_overflow) === top_overflow) |
283 | | - orientation = 'top'; |
284 | | - else |
285 | | - orientation = 'bottom'; |
| 283 | + orientation = (Math.max(topOverflow, bottomOverflow) === topOverflow) |
| 284 | + ? 'top' |
| 285 | + : 'bottom'; |
286 | 286 | } |
287 | 287 |
|
288 | | - if (orientation === 'top') |
| 288 | + if (orientation === 'top') { |
289 | 289 | styles.top += -containerHeight; |
290 | | - else |
| 290 | + } else { |
291 | 291 | styles.top += height; |
| 292 | + } |
292 | 293 |
|
293 | 294 | // If container is not positioned to body, |
294 | 295 | // correct its position using offset parent offset |
295 | 296 | if(containerParent !== document.body) { |
296 | 297 | var opacity = $container.css('opacity'), |
297 | 298 | parentOffsetDiff; |
298 | | - if (!that.visible) |
299 | | - $container.css('opacity', 0).show(); |
| 299 | + |
| 300 | + if (!that.visible){ |
| 301 | + $container.css('opacity', 0).show(); |
| 302 | + } |
300 | 303 |
|
301 | 304 | parentOffsetDiff = $container.offsetParent().offset(); |
302 | 305 | styles.top -= parentOffsetDiff.top; |
303 | 306 | styles.left -= parentOffsetDiff.left; |
304 | 307 |
|
305 | | - if (!that.visible) |
| 308 | + if (!that.visible){ |
306 | 309 | $container.css('opacity', opacity).hide(); |
| 310 | + } |
307 | 311 | } |
308 | 312 |
|
309 | 313 | // -2px to account for suggestions border. |
|
444 | 448 | query = that.getQuery(value), |
445 | 449 | index; |
446 | 450 |
|
447 | | - if (that.selection) { |
| 451 | + if (that.selection && that.currentValue !== query) { |
448 | 452 | that.selection = null; |
449 | 453 | (options.onInvalidateSelection || $.noop).call(that.element); |
450 | 454 | } |
|
611 | 615 | noSuggestionsContainer = $(that.noSuggestionsContainer), |
612 | 616 | beforeRender = options.beforeRender, |
613 | 617 | html = '', |
614 | | - index, |
615 | | - width; |
| 618 | + index; |
616 | 619 |
|
617 | 620 | if (options.triggerSelectOnValidInput) { |
618 | 621 | index = that.findSuggestionIndex(value); |
|
731 | 734 |
|
732 | 735 | validateOrientation: function(orientation, fallback) { |
733 | 736 | orientation = $.trim(orientation || '').toLowerCase(); |
| 737 | + |
734 | 738 | if($.inArray(orientation, ['auto', 'bottom', 'top']) === -1){ |
735 | 739 | orientation = fallback; |
736 | 740 | } |
737 | | - return orientation |
| 741 | + |
| 742 | + return orientation; |
738 | 743 | }, |
739 | 744 |
|
740 | 745 | processResponse: function (result, originalQuery, cacheKey) { |
|
765 | 770 | activeItem, |
766 | 771 | selected = that.classes.selected, |
767 | 772 | container = $(that.suggestionsContainer), |
768 | | - children = container.children(); |
| 773 | + children = container.find('.' + that.classes.suggestion); |
769 | 774 |
|
770 | | - container.children('.' + selected).removeClass(selected); |
| 775 | + container.find('.' + selected).removeClass(selected); |
771 | 776 |
|
772 | 777 | that.selectedIndex = index; |
773 | 778 |
|
|
897 | 902 | }; |
898 | 903 |
|
899 | 904 | // Create chainable jQuery plugin: |
900 | | - $.fn.autocomplete = function (options, args) { |
| 905 | + $.fn.autocomplete = $.fn.devbridgeAutocomplete = function (options, args) { |
901 | 906 | var dataKey = 'autocomplete'; |
902 | 907 | // If function invoked without argument return |
903 | 908 | // instance of the first matched element: |
|
0 commit comments