|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | /*jslint browser: true, white: true, plusplus: true */ |
11 | | -/*global define, window, document, jQuery */ |
| 11 | +/*global define, window, document, jQuery, exports */ |
12 | 12 |
|
13 | 13 | // Expose plugin as an AMD module if AMD loader is present: |
14 | 14 | (function (factory) { |
15 | 15 | 'use strict'; |
16 | 16 | if (typeof define === 'function' && define.amd) { |
17 | 17 | // AMD. Register as an anonymous module. |
18 | 18 | define(['jquery'], factory); |
| 19 | + } else if (typeof exports === 'object' && typeof require === 'function') { |
| 20 | + // Browserify |
| 21 | + factory(require('jquery')); |
19 | 22 | } else { |
20 | 23 | // Browser globals |
21 | 24 | factory(jQuery); |
|
138 | 141 | suggestionSelector = '.' + that.classes.suggestion, |
139 | 142 | selected = that.classes.selected, |
140 | 143 | options = that.options, |
141 | | - container, |
142 | | - noSuggestionsContainer; |
| 144 | + container; |
143 | 145 |
|
144 | 146 | // Remove autocomplete attribute to prevent native suggestions: |
145 | 147 | that.element.setAttribute('autocomplete', 'off'); |
|
276 | 278 | if (orientation == 'auto') { |
277 | 279 | var viewPortHeight = $(window).height(), |
278 | 280 | scrollTop = $(window).scrollTop(), |
279 | | - top_overflow = -scrollTop + offset.top - containerHeight, |
280 | | - bottom_overflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); |
| 281 | + topOverflow = -scrollTop + offset.top - containerHeight, |
| 282 | + bottomOverflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight); |
281 | 283 |
|
282 | | - if (Math.max(top_overflow, bottom_overflow) === top_overflow) |
283 | | - orientation = 'top'; |
284 | | - else |
285 | | - orientation = 'bottom'; |
| 284 | + orientation = (Math.max(topOverflow, bottomOverflow) === topOverflow) |
| 285 | + ? 'top' |
| 286 | + : 'bottom'; |
286 | 287 | } |
287 | 288 |
|
288 | | - if (orientation === 'top') |
| 289 | + if (orientation === 'top') { |
289 | 290 | styles.top += -containerHeight; |
290 | | - else |
| 291 | + } else { |
291 | 292 | styles.top += height; |
| 293 | + } |
292 | 294 |
|
293 | 295 | // If container is not positioned to body, |
294 | 296 | // correct its position using offset parent offset |
295 | 297 | if(containerParent !== document.body) { |
296 | 298 | var opacity = $container.css('opacity'), |
297 | 299 | parentOffsetDiff; |
298 | | - if (!that.visible) |
299 | | - $container.css('opacity', 0).show(); |
| 300 | + |
| 301 | + if (!that.visible){ |
| 302 | + $container.css('opacity', 0).show(); |
| 303 | + } |
300 | 304 |
|
301 | 305 | parentOffsetDiff = $container.offsetParent().offset(); |
302 | 306 | styles.top -= parentOffsetDiff.top; |
303 | 307 | styles.left -= parentOffsetDiff.left; |
304 | 308 |
|
305 | | - if (!that.visible) |
| 309 | + if (!that.visible){ |
306 | 310 | $container.css('opacity', opacity).hide(); |
| 311 | + } |
307 | 312 | } |
308 | 313 |
|
309 | 314 | // -2px to account for suggestions border. |
|
444 | 449 | query = that.getQuery(value), |
445 | 450 | index; |
446 | 451 |
|
447 | | - if (that.selection) { |
| 452 | + if (that.selection && that.currentValue !== query) { |
448 | 453 | that.selection = null; |
449 | 454 | (options.onInvalidateSelection || $.noop).call(that.element); |
450 | 455 | } |
|
588 | 593 | }, |
589 | 594 |
|
590 | 595 | hide: function () { |
591 | | - var that = this; |
| 596 | + var that = this, |
| 597 | + container = $(that.suggestionsContainer); |
| 598 | + |
| 599 | + if ($.isFunction(this.options.onHide) && that.visible) { |
| 600 | + |
| 601 | + this.options.onHide.call(that.element, container); |
| 602 | + } |
| 603 | + |
592 | 604 | that.visible = false; |
593 | 605 | that.selectedIndex = -1; |
594 | 606 | $(that.suggestionsContainer).hide(); |
|
597 | 609 |
|
598 | 610 | suggest: function () { |
599 | 611 | if (this.suggestions.length === 0) { |
600 | | - this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide(); |
| 612 | + this.options.showNoSuggestionNotice ? this.noSuggestions() : this.hide(); |
601 | 613 | return; |
602 | 614 | } |
603 | 615 |
|
|
611 | 623 | noSuggestionsContainer = $(that.noSuggestionsContainer), |
612 | 624 | beforeRender = options.beforeRender, |
613 | 625 | html = '', |
614 | | - index, |
615 | | - width; |
| 626 | + index; |
616 | 627 |
|
617 | 628 | if (options.triggerSelectOnValidInput) { |
618 | 629 | index = that.findSuggestionIndex(value); |
|
627 | 638 | html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>'; |
628 | 639 | }); |
629 | 640 |
|
630 | | - this.adjustContainerWidth(); |
| 641 | + this.adjustContainerWidth(); |
631 | 642 |
|
632 | 643 | noSuggestionsContainer.detach(); |
633 | 644 | container.html(html); |
|
731 | 742 |
|
732 | 743 | validateOrientation: function(orientation, fallback) { |
733 | 744 | orientation = $.trim(orientation || '').toLowerCase(); |
| 745 | + |
734 | 746 | if($.inArray(orientation, ['auto', 'bottom', 'top']) === -1){ |
735 | 747 | orientation = fallback; |
736 | 748 | } |
737 | | - return orientation |
| 749 | + |
| 750 | + return orientation; |
738 | 751 | }, |
739 | 752 |
|
740 | 753 | processResponse: function (result, originalQuery, cacheKey) { |
|
897 | 910 | }; |
898 | 911 |
|
899 | 912 | // Create chainable jQuery plugin: |
900 | | - $.fn.autocomplete = function (options, args) { |
| 913 | + $.fn.autocomplete = $.fn.devbridgeAutocomplete = function (options, args) { |
901 | 914 | var dataKey = 'autocomplete'; |
902 | 915 | // If function invoked without argument return |
903 | 916 | // instance of the first matched element: |
|
0 commit comments