Skip to content

Commit 4924da0

Browse files
author
Tomas Kirda
committed
Rev for 1.2.9 release
1 parent 6dad728 commit 4924da0

File tree

4 files changed

+114
-57
lines changed

4 files changed

+114
-57
lines changed

devbridge-autocomplete.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"ajax",
77
"autocomplete"
88
],
9-
"version": "1.2.8",
9+
"version": "1.2.9",
1010
"author": {
1111
"name": "Tomas Kirda",
1212
"url": "https://github.com/tkirda"

dist/jquery.autocomplete.js

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Ajax Autocomplete for jQuery, version 1.2.8
2+
* Ajax Autocomplete for jQuery, version 1.2.9
33
* (c) 2013 Tomas Kirda
44
*
55
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
@@ -75,6 +75,7 @@
7575
tabDisabled: false,
7676
dataType: 'text',
7777
currentRequest: null,
78+
triggerSelectOnValidInput: true,
7879
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
7980
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
8081
},
@@ -92,7 +93,7 @@
9293
that.selectedIndex = -1;
9394
that.currentValue = that.element.value;
9495
that.intervalId = 0;
95-
that.cachedResponse = [];
96+
that.cachedResponse = {};
9697
that.onChangeInterval = null;
9798
that.onChange = null;
9899
that.isLocal = false;
@@ -219,7 +220,7 @@
219220
},
220221

221222
clearCache: function () {
222-
this.cachedResponse = [];
223+
this.cachedResponse = {};
223224
this.badQueries = [];
224225
},
225226

@@ -390,80 +391,123 @@
390391

391392
onValueChange: function () {
392393
var that = this,
393-
q;
394+
options = that.options,
395+
value = that.el.val(),
396+
query = that.getQuery(value),
397+
index;
394398

395399
if (that.selection) {
396400
that.selection = null;
397-
(that.options.onInvalidateSelection || $.noop)();
401+
(options.onInvalidateSelection || $.noop).call(that.element);
398402
}
399403

400404
clearInterval(that.onChangeInterval);
401-
that.currentValue = that.el.val();
402-
403-
q = that.getQuery(that.currentValue);
405+
that.currentValue = value;
404406
that.selectedIndex = -1;
405407

406-
if (q.length < that.options.minChars) {
408+
// Check existing suggestion for the match before proceeding:
409+
if (options.triggerSelectOnValidInput) {
410+
index = that.findSuggestionIndex(query);
411+
if (index !== -1) {
412+
that.select(index);
413+
return;
414+
}
415+
}
416+
417+
if (query.length < options.minChars) {
407418
that.hide();
408419
} else {
409-
that.getSuggestions(q);
420+
that.getSuggestions(query);
410421
}
411422
},
412423

424+
findSuggestionIndex: function (query) {
425+
var that = this,
426+
index = -1,
427+
queryLowerCase = query.toLowerCase();
428+
429+
$.each(that.suggestions, function (i, suggestion) {
430+
if (suggestion.value.toLowerCase() === queryLowerCase) {
431+
index = i;
432+
return false;
433+
}
434+
});
435+
436+
return index;
437+
},
438+
413439
getQuery: function (value) {
414440
var delimiter = this.options.delimiter,
415441
parts;
416442

417443
if (!delimiter) {
418-
return $.trim(value);
444+
return value;
419445
}
420446
parts = value.split(delimiter);
421447
return $.trim(parts[parts.length - 1]);
422448
},
423449

424450
getSuggestionsLocal: function (query) {
425451
var that = this,
452+
options = that.options,
426453
queryLowerCase = query.toLowerCase(),
427-
filter = that.options.lookupFilter;
454+
filter = options.lookupFilter,
455+
limit = parseInt(options.lookupLimit, 10),
456+
data;
428457

429-
return {
430-
suggestions: $.grep(that.options.lookup, function (suggestion) {
458+
data = {
459+
suggestions: $.grep(options.lookup, function (suggestion) {
431460
return filter(suggestion, query, queryLowerCase);
432461
})
433462
};
463+
464+
if (limit && data.suggestions.length > limit) {
465+
data.suggestions = data.suggestions.slice(0, limit);
466+
}
467+
468+
return data;
434469
},
435470

436471
getSuggestions: function (q) {
437472
var response,
438473
that = this,
439474
options = that.options,
440-
serviceUrl = options.serviceUrl;
475+
serviceUrl = options.serviceUrl,
476+
data,
477+
cacheKey;
441478

442-
response = that.isLocal ? that.getSuggestionsLocal(q) : that.cachedResponse[q];
479+
options.params[options.paramName] = q;
480+
data = options.ignoreParams ? null : options.params;
481+
482+
if (that.isLocal) {
483+
response = that.getSuggestionsLocal(q);
484+
} else {
485+
if ($.isFunction(serviceUrl)) {
486+
serviceUrl = serviceUrl.call(that.element, q);
487+
}
488+
cacheKey = serviceUrl + '?' + $.param(data || {});
489+
response = that.cachedResponse[cacheKey];
490+
}
443491

444492
if (response && $.isArray(response.suggestions)) {
445493
that.suggestions = response.suggestions;
446494
that.suggest();
447495
} else if (!that.isBadQuery(q)) {
448-
options.params[options.paramName] = q;
449496
if (options.onSearchStart.call(that.element, options.params) === false) {
450497
return;
451498
}
452-
if ($.isFunction(options.serviceUrl)) {
453-
serviceUrl = options.serviceUrl.call(that.element, q);
454-
}
455499
if (that.currentRequest) {
456500
that.currentRequest.abort();
457501
}
458502
that.currentRequest = $.ajax({
459503
url: serviceUrl,
460-
data: options.ignoreParams ? null : options.params,
504+
data: data,
461505
type: options.type,
462506
dataType: options.dataType
463507
}).done(function (data) {
464508
that.currentRequest = null;
465-
that.processResponse(data, q);
466-
options.onSearchComplete.call(that.element, q, data);
509+
that.processResponse(data, q, cacheKey);
510+
options.onSearchComplete.call(that.element, q);
467511
}).fail(function (jqXHR, textStatus, errorThrown) {
468512
options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
469513
});
@@ -498,15 +542,25 @@
498542
}
499543

500544
var that = this,
501-
formatResult = that.options.formatResult,
545+
options = that.options,
546+
formatResult = options.formatResult,
502547
value = that.getQuery(that.currentValue),
503548
className = that.classes.suggestion,
504549
classSelected = that.classes.selected,
505550
container = $(that.suggestionsContainer),
506-
beforeRender = that.options.beforeRender,
551+
beforeRender = options.beforeRender,
507552
html = '',
553+
index,
508554
width;
509555

556+
if (options.triggerSelectOnValidInput) {
557+
index = that.findSuggestionIndex(value);
558+
if (index !== -1) {
559+
that.select(index);
560+
return;
561+
}
562+
}
563+
510564
// Build suggestions inner HTML:
511565
$.each(that.suggestions, function (i, suggestion) {
512566
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
@@ -516,15 +570,15 @@
516570
// because if instance was created before input had width, it will be zero.
517571
// Also it adjusts if input width has changed.
518572
// -2px to account for suggestions border.
519-
if (that.options.width === 'auto') {
573+
if (options.width === 'auto') {
520574
width = that.el.outerWidth() - 2;
521575
container.width(width > 0 ? width : 300);
522576
}
523577

524578
container.html(html);
525579

526580
// Select first value by default:
527-
if (that.options.autoSelectFirst) {
581+
if (options.autoSelectFirst) {
528582
that.selectedIndex = 0;
529583
container.children().first().addClass(classSelected);
530584
}
@@ -583,7 +637,7 @@
583637
return suggestions;
584638
},
585639

586-
processResponse: function (response, originalQuery) {
640+
processResponse: function (response, originalQuery, cacheKey) {
587641
var that = this,
588642
options = that.options,
589643
result = options.transformResult(response, originalQuery);
@@ -592,17 +646,19 @@
592646

593647
// Cache results if cache is not disabled:
594648
if (!options.noCache) {
595-
that.cachedResponse[result[options.paramName]] = result;
649+
that.cachedResponse[cacheKey] = result;
596650
if (result.suggestions.length === 0) {
597-
that.badQueries.push(result[options.paramName]);
651+
that.badQueries.push(cacheKey);
598652
}
599653
}
600654

601-
// Display suggestions only if returned query matches current value:
602-
if (originalQuery === that.getQuery(that.currentValue)) {
603-
that.suggestions = result.suggestions;
604-
that.suggest();
655+
// Return if originalQuery is not matching current query:
656+
if (originalQuery !== that.getQuery(that.currentValue)) {
657+
return;
605658
}
659+
660+
that.suggestions = result.suggestions;
661+
that.suggest();
606662
},
607663

608664
activate: function (index) {

0 commit comments

Comments
 (0)