2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2025-01-10 01:17:49 +00:00

Map this to that to achieve better compression.

This commit is contained in:
Tomas Kirda 2013-01-15 11:50:00 -06:00
parent 21760cee19
commit d7102f4001

View File

@ -153,7 +153,7 @@
container = $(that.suggestionsContainer); container = $(that.suggestionsContainer);
container.appendTo('body').width(this.options.width); container.appendTo('body').width(that.options.width);
// Listen for mouse over event on suggestions list: // Listen for mouse over event on suggestions list:
container.on('mouseover', suggestionSelector, function () { container.on('mouseover', suggestionSelector, function () {
@ -184,18 +184,19 @@
}, },
setOptions: function (suppliedOptions) { setOptions: function (suppliedOptions) {
var options = this.options; var that = this,
options = that.options;
utils.extend(options, suppliedOptions); utils.extend(options, suppliedOptions);
this.isLocal = $.isArray(options.lookup); that.isLocal = $.isArray(options.lookup);
if (this.isLocal) { if (that.isLocal) {
options.lookup = this.verifySuggestionsFormat(options.lookup); options.lookup = that.verifySuggestionsFormat(options.lookup);
} }
// Adjust height, width and z-index: // Adjust height, width and z-index:
$(this.suggestionsContainer).css({ $(that.suggestionsContainer).css({
'max-height': options.maxHeight + 'px', 'max-height': options.maxHeight + 'px',
'width': options.width + 'px', 'width': options.width + 'px',
'z-index': options.zIndex 'z-index': options.zIndex
@ -247,37 +248,39 @@
}, },
onKeyPress: function (e) { onKeyPress: function (e) {
var that = this;
// If suggestions are hidden and user presses arrow down, display suggestions: // If suggestions are hidden and user presses arrow down, display suggestions:
if (!this.disabled && !this.visible && e.keyCode === keys.DOWN && this.currentValue) { if (!that.disabled && !that.visible && e.keyCode === keys.DOWN && that.currentValue) {
this.suggest(); that.suggest();
return; return;
} }
if (this.disabled || !this.visible) { if (that.disabled || !that.visible) {
return; return;
} }
switch (e.keyCode) { switch (e.keyCode) {
case keys.ESC: case keys.ESC:
this.el.val(this.currentValue); that.el.val(that.currentValue);
this.hide(); that.hide();
break; break;
case keys.TAB: case keys.TAB:
case keys.RETURN: case keys.RETURN:
if (this.selectedIndex === -1) { if (that.selectedIndex === -1) {
this.hide(); that.hide();
return; return;
} }
this.select(this.selectedIndex); that.select(that.selectedIndex);
if (e.keyCode === keys.TAB) { if (e.keyCode === keys.TAB) {
return; return;
} }
break; break;
case keys.UP: case keys.UP:
this.moveUp(); that.moveUp();
break; break;
case keys.DOWN: case keys.DOWN:
this.moveDown(); that.moveDown();
break; break;
default: default:
return; return;
@ -289,7 +292,9 @@
}, },
onKeyUp: function (e) { onKeyUp: function (e) {
if (this.disabled) { var that = this;
if (that.disabled) {
return; return;
} }
@ -299,8 +304,6 @@
return; return;
} }
var that = this;
clearInterval(that.onChangeInterval); clearInterval(that.onChangeInterval);
if (that.currentValue !== that.el.val()) { if (that.currentValue !== that.el.val()) {
@ -316,21 +319,24 @@
}, },
onValueChange: function () { onValueChange: function () {
clearInterval(this.onChangeInterval); var that = this,
this.currentValue = this.element.value; q;
var q = this.getQuery(this.currentValue); clearInterval(that.onChangeInterval);
this.selectedIndex = -1; that.currentValue = that.element.value;
if (this.ignoreValueChange) { q = that.getQuery(that.currentValue);
this.ignoreValueChange = false; that.selectedIndex = -1;
if (that.ignoreValueChange) {
that.ignoreValueChange = false;
return; return;
} }
if (q === '' || q.length < this.options.minChars) { if (q === '' || q.length < that.options.minChars) {
this.hide(); that.hide();
} else { } else {
this.getSuggestions(q); that.getSuggestions(q);
} }
}, },
@ -394,9 +400,10 @@
}, },
hide: function () { hide: function () {
this.visible = false; var that = this;
this.selectedIndex = -1; that.visible = false;
$(this.suggestionsContainer).hide(); that.selectedIndex = -1;
$(that.suggestionsContainer).hide();
}, },
suggest: function () { suggest: function () {
@ -405,23 +412,24 @@
return; return;
} }
var formatResult = this.options.formatResult, var that = this,
value = this.getQuery(this.currentValue), formatResult = that.options.formatResult,
className = this.classes.suggestion, value = that.getQuery(that.currentValue),
classSelected = this.classes.selected, className = that.classes.suggestion,
container = $(this.suggestionsContainer), classSelected = that.classes.selected,
container = $(that.suggestionsContainer),
html = ''; html = '';
// Build suggestions inner HTML: // Build suggestions inner HTML:
$.each(this.suggestions, function (i, suggestion) { $.each(that.suggestions, function (i, suggestion) {
html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>'; html += '<div class="' + className + '" data-index="' + i + '">' + formatResult(suggestion, value) + '</div>';
}); });
container.html(html).show(); container.html(html).show();
this.visible = true; that.visible = true;
// Select first value by default: // Select first value by default:
this.selectedIndex = 0; that.selectedIndex = 0;
container.children().first().addClass(classSelected); container.children().first().addClass(classSelected);
}, },
@ -437,37 +445,39 @@
}, },
processResponse: function (text) { processResponse: function (text) {
var response = $.parseJSON(text); var that = this,
response = $.parseJSON(text);
response.suggestions = this.verifySuggestionsFormat(response.suggestions); response.suggestions = that.verifySuggestionsFormat(response.suggestions);
// Cache results if cache is not disabled: // Cache results if cache is not disabled:
if (!this.options.noCache) { if (!that.options.noCache) {
this.cachedResponse[response.query] = response; that.cachedResponse[response.query] = response;
if (response.suggestions.length === 0) { if (response.suggestions.length === 0) {
this.badQueries.push(response.query); that.badQueries.push(response.query);
} }
} }
// Display suggestions only if returned query matches current value: // Display suggestions only if returned query matches current value:
if (response.query === this.getQuery(this.currentValue)) { if (response.query === that.getQuery(that.currentValue)) {
this.suggestions = response.suggestions; that.suggestions = response.suggestions;
this.suggest(); that.suggest();
} }
}, },
activate: function (index) { activate: function (index) {
var activeItem, var that = this,
selected = this.classes.selected, activeItem,
container = $(this.suggestionsContainer), selected = that.classes.selected,
container = $(that.suggestionsContainer),
children = container.children(); children = container.children();
container.children('.' + selected).removeClass(selected); container.children('.' + selected).removeClass(selected);
this.selectedIndex = index; that.selectedIndex = index;
if (this.selectedIndex !== -1 && children.length > this.selectedIndex) { if (that.selectedIndex !== -1 && children.length > that.selectedIndex) {
activeItem = children.get(this.selectedIndex); activeItem = children.get(that.selectedIndex);
$(activeItem).addClass(selected); $(activeItem).addClass(selected);
return activeItem; return activeItem;
} }
@ -476,41 +486,47 @@
}, },
select: function (i) { select: function (i) {
var selectedValue = this.suggestions[i]; var that = this,
selectedValue = that.suggestions[i];
if (selectedValue) { if (selectedValue) {
this.el.val(selectedValue); that.el.val(selectedValue);
this.ignoreValueChange = true; that.ignoreValueChange = true;
this.hide(); that.hide();
this.onSelect(i); that.onSelect(i);
} }
}, },
moveUp: function () { moveUp: function () {
if (this.selectedIndex === -1) { var that = this;
if (that.selectedIndex === -1) {
return; return;
} }
if (this.selectedIndex === 0) { if (that.selectedIndex === 0) {
$(this.suggestionsContainer).children().first().removeClass(this.classes.selected); $(that.suggestionsContainer).children().first().removeClass(that.classes.selected);
this.selectedIndex = -1; that.selectedIndex = -1;
this.el.val(this.currentValue); that.el.val(that.currentValue);
return; return;
} }
this.adjustScroll(this.selectedIndex - 1); that.adjustScroll(that.selectedIndex - 1);
}, },
moveDown: function () { moveDown: function () {
if (this.selectedIndex === (this.suggestions.length - 1)) { var that = this;
if (that.selectedIndex === (that.suggestions.length - 1)) {
return; return;
} }
this.adjustScroll(this.selectedIndex + 1); that.adjustScroll(that.selectedIndex + 1);
}, },
adjustScroll: function (index) { adjustScroll: function (index) {
var activeItem = this.activate(index), var that = this,
activeItem = that.activate(index),
offsetTop, offsetTop,
upperBound, upperBound,
lowerBound, lowerBound,
@ -521,16 +537,16 @@
} }
offsetTop = activeItem.offsetTop; offsetTop = activeItem.offsetTop;
upperBound = $(this.suggestionsContainer).scrollTop(); upperBound = $(that.suggestionsContainer).scrollTop();
lowerBound = upperBound + this.options.maxHeight - heightDelta; lowerBound = upperBound + that.options.maxHeight - heightDelta;
if (offsetTop < upperBound) { if (offsetTop < upperBound) {
$(this.suggestionsContainer).scrollTop(offsetTop); $(that.suggestionsContainer).scrollTop(offsetTop);
} else if (offsetTop > lowerBound) { } else if (offsetTop > lowerBound) {
$(this.suggestionsContainer).scrollTop(offsetTop - this.options.maxHeight + heightDelta); $(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta);
} }
this.el.val(this.getValue(this.suggestions[index].value)); that.el.val(that.getValue(that.suggestions[index].value));
}, },
onSelect: function (index) { onSelect: function (index) {