2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-29 08:14:06 +00:00

Track hintValue and signal hint only when hintValue changes.

This commit is contained in:
Tomas Kirda 2013-06-07 17:52:54 -05:00
parent adfbf7a4e9
commit 5a6eaca541

View File

@ -26,17 +26,14 @@
var var
utils = (function () { utils = (function () {
return { return {
escapeRegExChars: function (value) {
extend: function (target, source) { return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
return $.extend(target, source);
}, },
createNode: function (html) { createNode: function (html) {
var div = document.createElement('div'); var div = document.createElement('div');
div.innerHTML = html; div.innerHTML = html;
return div.firstChild; return div.firstChild;
} }
}; };
}()), }()),
@ -102,6 +99,7 @@
suggestion: 'autocomplete-suggestion' suggestion: 'autocomplete-suggestion'
}; };
that.hint = null; that.hint = null;
that.hintValue = '';
// Initialize and set options: // Initialize and set options:
that.initialize(); that.initialize();
@ -113,8 +111,7 @@
$.Autocomplete = Autocomplete; $.Autocomplete = Autocomplete;
Autocomplete.formatResult = function (suggestion, currentValue) { Autocomplete.formatResult = function (suggestion, currentValue) {
var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g'), var pattern = '(' + utils.escapeRegExChars(currentValue) + ')';
pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>'); return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
}; };
@ -191,7 +188,7 @@
var that = this, var that = this,
options = that.options; options = that.options;
utils.extend(options, suppliedOptions); $.extend(options, suppliedOptions);
that.isLocal = $.isArray(options.lookup); that.isLocal = $.isArray(options.lookup);
@ -521,7 +518,8 @@
if (suggestion) { if (suggestion) {
hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length); hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length);
} }
if (that.hint !== suggestion) { if (that.hintValue !== hintValue) {
that.hintValue = hintValue;
that.hint = suggestion; that.hint = suggestion;
(this.options.onHint || $.noop)(hintValue); (this.options.onHint || $.noop)(hintValue);
} }