|
1 | 1 | /** |
2 | | -* Ajax Autocomplete for jQuery, version 1.2.1 |
| 2 | +* Ajax Autocomplete for jQuery, version 1.2.2 |
3 | 3 | * (c) 2013 Tomas Kirda |
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: http://www.devbridge.com/projects/autocomplete/jquery/ |
7 | 7 | * |
8 | | -* Last Review: 01/15/2013 |
9 | 8 | */ |
10 | 9 |
|
11 | 10 | /*jslint browser: true, white: true, plusplus: true */ |
|
71 | 70 | var noop = function () { }, |
72 | 71 | that = this, |
73 | 72 | defaults = { |
| 73 | + autoSelectFirst: false, |
| 74 | + appendTo: 'body', |
74 | 75 | serviceUrl: null, |
75 | 76 | lookup: null, |
76 | 77 | onSelect: null, |
|
87 | 88 | onSearchStart: noop, |
88 | 89 | onSearchComplete: noop, |
89 | 90 | containerClass: 'autocomplete-suggestions', |
| 91 | + tabDisabled: false, |
90 | 92 | lookupFilter: function (suggestion, originalQuery, queryLowerCase) { |
91 | 93 | return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; |
| 94 | + }, |
| 95 | + paramName: 'query', |
| 96 | + transformResult: function (response) { |
| 97 | + return response.suggestions; |
92 | 98 | } |
93 | 99 | }; |
94 | 100 |
|
|
106 | 112 | that.ignoreValueChange = false; |
107 | 113 | that.isLocal = false; |
108 | 114 | that.suggestionsContainer = null; |
109 | | - that.options = defaults; |
| 115 | + that.options = $.extend({}, defaults, options); |
110 | 116 | that.classes = { |
111 | 117 | selected: 'autocomplete-selected', |
112 | 118 | suggestion: 'autocomplete-suggestion' |
|
135 | 141 | initialize: function () { |
136 | 142 | var that = this, |
137 | 143 | suggestionSelector = '.' + that.classes.suggestion, |
| 144 | + selected = that.classes.selected, |
| 145 | + options = that.options, |
138 | 146 | container; |
139 | 147 |
|
140 | 148 | // Remove autocomplete attribute to prevent native suggestions: |
|
148 | 156 | }; |
149 | 157 |
|
150 | 158 | // Determine suggestions width: |
151 | | - if (!that.options.width || that.options.width === 'auto') { |
152 | | - that.options.width = that.el.outerWidth(); |
| 159 | + if (!options.width || options.width === 'auto') { |
| 160 | + options.width = that.el.outerWidth(); |
153 | 161 | } |
154 | 162 |
|
155 | | - that.suggestionsContainer = Autocomplete.utils.createNode('<div class="' + that.options.containerClass + '" style="position: absolute; display: none;"></div>'); |
| 163 | + that.suggestionsContainer = Autocomplete.utils.createNode('<div class="' + options.containerClass + '" style="position: absolute; display: none;"></div>'); |
156 | 164 |
|
157 | 165 | container = $(that.suggestionsContainer); |
158 | 166 |
|
159 | | - container.appendTo('body').width(that.options.width); |
| 167 | + container.appendTo(options.appendTo).width(options.width); |
160 | 168 |
|
161 | 169 | // Listen for mouse over event on suggestions list: |
162 | 170 | container.on('mouseover', suggestionSelector, function () { |
163 | 171 | that.activate($(this).data('index')); |
164 | 172 | }); |
165 | 173 |
|
| 174 | + // Deselect active element when mouse leaves suggestions container: |
| 175 | + container.on('mouseout', function () { |
| 176 | + that.selectedIndex = -1; |
| 177 | + container.children('.' + selected).removeClass(selected); |
| 178 | + }); |
| 179 | + |
166 | 180 | // Listen for click event on suggestions list: |
167 | 181 | container.on('click', suggestionSelector, function () { |
168 | 182 | that.select($(this).data('index')); |
|
220 | 234 | }, |
221 | 235 |
|
222 | 236 | fixPosition: function () { |
223 | | - var offset = this.el.offset(); |
224 | | - $(this.suggestionsContainer).css({ |
225 | | - top: (offset.top + this.el.outerHeight()) + 'px', |
| 237 | + var that = this, |
| 238 | + offset; |
| 239 | + |
| 240 | + // Don't adjsut position if custom container has been specified: |
| 241 | + if (that.options.appendTo !== 'body') { |
| 242 | + return; |
| 243 | + } |
| 244 | + |
| 245 | + offset = that.el.offset(); |
| 246 | + |
| 247 | + $(that.suggestionsContainer).css({ |
| 248 | + top: (offset.top + that.el.outerHeight()) + 'px', |
226 | 249 | left: offset.left + 'px' |
227 | 250 | }); |
228 | 251 | }, |
|
275 | 298 | return; |
276 | 299 | } |
277 | 300 | that.select(that.selectedIndex); |
278 | | - if (e.keyCode === keys.TAB) { |
| 301 | + if (e.keyCode === keys.TAB && this.options.tabDisabled === false) { |
279 | 302 | return; |
280 | 303 | } |
281 | 304 | break; |
|
378 | 401 | that.suggest(); |
379 | 402 | } else if (!that.isBadQuery(q)) { |
380 | 403 | options.onSearchStart.call(that.element, q); |
381 | | - options.params.query = q; |
| 404 | + options.params[options.paramName] = q; |
382 | 405 | $.ajax({ |
383 | 406 | url: options.serviceUrl, |
384 | 407 | data: options.params, |
|
434 | 457 | that.visible = true; |
435 | 458 |
|
436 | 459 | // Select first value by default: |
437 | | - that.selectedIndex = 0; |
438 | | - container.children().first().addClass(classSelected); |
| 460 | + if (that.options.autoSelectFirst) { |
| 461 | + that.selectedIndex = 0; |
| 462 | + container.children().first().addClass(classSelected); |
| 463 | + } |
439 | 464 | }, |
440 | 465 |
|
441 | 466 | verifySuggestionsFormat: function (suggestions) { |
|
453 | 478 | var that = this, |
454 | 479 | response = $.parseJSON(text); |
455 | 480 |
|
456 | | - response.suggestions = that.verifySuggestionsFormat(response.suggestions); |
| 481 | + response.suggestions = that.verifySuggestionsFormat(that.options.transformResult(response)); |
457 | 482 |
|
458 | 483 | // Cache results if cache is not disabled: |
459 | 484 | if (!that.options.noCache) { |
460 | | - that.cachedResponse[response.query] = response; |
| 485 | + that.cachedResponse[response[that.options.paramName]] = response; |
461 | 486 | if (response.suggestions.length === 0) { |
462 | | - that.badQueries.push(response.query); |
| 487 | + that.badQueries.push(response[that.options.paramName]); |
463 | 488 | } |
464 | 489 | } |
465 | 490 |
|
466 | 491 | // Display suggestions only if returned query matches current value: |
467 | | - if (response.query === that.getQuery(that.currentValue)) { |
| 492 | + if (response[that.options.paramName] === that.getQuery(that.currentValue)) { |
468 | 493 | that.suggestions = response.suggestions; |
469 | 494 | that.suggest(); |
470 | 495 | } |
|
0 commit comments