Skip to content

Commit 3023235

Browse files
author
Tomas Kirda
committed
Release v1.2.2
1 parent 3344e46 commit 3023235

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
lines changed

dist/jquery.autocomplete.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* Ajax Autocomplete for jQuery, version 1.2.1
2+
* Ajax Autocomplete for jQuery, version 1.2.2
33
* (c) 2013 Tomas Kirda
44
*
55
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
66
* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
77
*
8-
* Last Review: 01/15/2013
98
*/
109

1110
/*jslint browser: true, white: true, plusplus: true */
@@ -71,6 +70,8 @@
7170
var noop = function () { },
7271
that = this,
7372
defaults = {
73+
autoSelectFirst: false,
74+
appendTo: 'body',
7475
serviceUrl: null,
7576
lookup: null,
7677
onSelect: null,
@@ -87,8 +88,13 @@
8788
onSearchStart: noop,
8889
onSearchComplete: noop,
8990
containerClass: 'autocomplete-suggestions',
91+
tabDisabled: false,
9092
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
9193
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
94+
},
95+
paramName: 'query',
96+
transformResult: function (response) {
97+
return response.suggestions;
9298
}
9399
};
94100

@@ -106,7 +112,7 @@
106112
that.ignoreValueChange = false;
107113
that.isLocal = false;
108114
that.suggestionsContainer = null;
109-
that.options = defaults;
115+
that.options = $.extend({}, defaults, options);
110116
that.classes = {
111117
selected: 'autocomplete-selected',
112118
suggestion: 'autocomplete-suggestion'
@@ -135,6 +141,8 @@
135141
initialize: function () {
136142
var that = this,
137143
suggestionSelector = '.' + that.classes.suggestion,
144+
selected = that.classes.selected,
145+
options = that.options,
138146
container;
139147

140148
// Remove autocomplete attribute to prevent native suggestions:
@@ -148,21 +156,27 @@
148156
};
149157

150158
// 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();
153161
}
154162

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>');
156164

157165
container = $(that.suggestionsContainer);
158166

159-
container.appendTo('body').width(that.options.width);
167+
container.appendTo(options.appendTo).width(options.width);
160168

161169
// Listen for mouse over event on suggestions list:
162170
container.on('mouseover', suggestionSelector, function () {
163171
that.activate($(this).data('index'));
164172
});
165173

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+
166180
// Listen for click event on suggestions list:
167181
container.on('click', suggestionSelector, function () {
168182
that.select($(this).data('index'));
@@ -220,9 +234,18 @@
220234
},
221235

222236
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',
226249
left: offset.left + 'px'
227250
});
228251
},
@@ -275,7 +298,7 @@
275298
return;
276299
}
277300
that.select(that.selectedIndex);
278-
if (e.keyCode === keys.TAB) {
301+
if (e.keyCode === keys.TAB && this.options.tabDisabled === false) {
279302
return;
280303
}
281304
break;
@@ -378,7 +401,7 @@
378401
that.suggest();
379402
} else if (!that.isBadQuery(q)) {
380403
options.onSearchStart.call(that.element, q);
381-
options.params.query = q;
404+
options.params[options.paramName] = q;
382405
$.ajax({
383406
url: options.serviceUrl,
384407
data: options.params,
@@ -434,8 +457,10 @@
434457
that.visible = true;
435458

436459
// 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+
}
439464
},
440465

441466
verifySuggestionsFormat: function (suggestions) {
@@ -453,18 +478,18 @@
453478
var that = this,
454479
response = $.parseJSON(text);
455480

456-
response.suggestions = that.verifySuggestionsFormat(response.suggestions);
481+
response.suggestions = that.verifySuggestionsFormat(that.options.transformResult(response));
457482

458483
// Cache results if cache is not disabled:
459484
if (!that.options.noCache) {
460-
that.cachedResponse[response.query] = response;
485+
that.cachedResponse[response[that.options.paramName]] = response;
461486
if (response.suggestions.length === 0) {
462-
that.badQueries.push(response.query);
487+
that.badQueries.push(response[that.options.paramName]);
463488
}
464489
}
465490

466491
// 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)) {
468493
that.suggestions = response.suggestions;
469494
that.suggest();
470495
}

0 commit comments

Comments
 (0)