2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-29 16:24:05 +00:00

Update dist files

This commit is contained in:
Tomas Kirda 2013-01-15 12:25:11 -06:00
parent 32857e7132
commit bd0829c7bf
3 changed files with 207 additions and 146 deletions

View File

@ -1,20 +1,31 @@
/** /**
* Ajax Autocomplete for jQuery, version 1.2 * Ajax Autocomplete for jQuery, version 1.2.1
* (c) 2012 Tomas Kirda * (c) 2013 Tomas Kirda
* *
* Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
* For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/ * For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
* *
* Last Review: 12/19/2012 * Last Review: 01/15/2013
*/ */
/*jslint browser: true, white: true, plusplus: true, vars: true */ /*jslint browser: true, white: true, plusplus: true */
/*global window: true, document: true, clearInterval: true, setInterval: true, jQuery: true */ /*global define, window, document, jQuery */
(function ($) { // Expose plugin as an AMD module if AMD loader is present:
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
'use strict'; 'use strict';
var utils = (function () { var
utils = (function () {
return { return {
extend: function (target, source) { extend: function (target, source) {
@ -46,10 +57,19 @@
} }
}; };
}()); }()),
keys = {
ESC: 27,
TAB: 9,
RETURN: 13,
UP: 38,
DOWN: 40
};
function Autocomplete(el, options) { function Autocomplete(el, options) {
var that = this, var noop = function () { },
that = this,
defaults = { defaults = {
serviceUrl: null, serviceUrl: null,
lookup: null, lookup: null,
@ -63,7 +83,13 @@
delimiter: null, delimiter: null,
zIndex: 9999, zIndex: 9999,
type: 'GET', type: 'GET',
noCache: false noCache: false,
onSearchStart: noop,
onSearchComplete: noop,
containerClass: 'autocomplete-suggestions',
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
}
}; };
// Shared variables: // Shared variables:
@ -108,28 +134,29 @@
initialize: function () { initialize: function () {
var that = this, var that = this,
suggestionSelector = '.' + that.classes.suggestion; suggestionSelector = '.' + that.classes.suggestion,
container;
// Remove autocomplete attribute to prevent native suggestions: // Remove autocomplete attribute to prevent native suggestions:
this.element.setAttribute('autocomplete', 'off'); that.element.setAttribute('autocomplete', 'off');
this.killerFn = function (e) { that.killerFn = function (e) {
if ($(e.target).closest('.autocomplete').length === 0) { if ($(e.target).closest('.' + that.options.containerClass).length === 0) {
that.killSuggestions(); that.killSuggestions();
that.disableKillerFn(); that.disableKillerFn();
} }
}; };
// Determine suggestions width: // Determine suggestions width:
if (!this.options.width || this.options.width === 'auto') { if (!that.options.width || that.options.width === 'auto') {
this.options.width = this.el.outerWidth(); that.options.width = that.el.outerWidth();
} }
this.suggestionsContainer = Autocomplete.utils.createNode('<div class="autocomplete-suggestions" style="position: absolute; display: none;"></div>'); that.suggestionsContainer = Autocomplete.utils.createNode('<div class="' + that.options.containerClass + '" style="position: absolute; display: none;"></div>');
var container = $(this.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 () {
@ -141,18 +168,18 @@
that.select($(this).data('index')); that.select($(this).data('index'));
}); });
this.fixPosition(); that.fixPosition();
// Opera does not like keydown: // Opera does not like keydown:
if (window.opera) { if (window.opera) {
this.el.on('keypress', function (e) { that.onKeyPress(e); }); that.el.on('keypress', function (e) { that.onKeyPress(e); });
} else { } else {
this.el.on('keydown', function (e) { that.onKeyPress(e); }); that.el.on('keydown', function (e) { that.onKeyPress(e); });
} }
this.el.on('keyup', function (e) { that.onKeyUp(e); }); that.el.on('keyup', function (e) { that.onKeyUp(e); });
this.el.on('blur', function () { that.onBlur(); }); that.el.on('blur', function () { that.onBlur(); });
this.el.on('focus', function () { that.fixPosition(); }); that.el.on('focus', function () { that.fixPosition(); });
}, },
onBlur: function () { onBlur: function () {
@ -160,18 +187,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
@ -223,37 +251,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 === 40 && 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 27: //KEY_ESC: case keys.ESC:
this.el.val(this.currentValue); that.el.val(that.currentValue);
this.hide(); that.hide();
break; break;
case 9: //KEY_TAB: case keys.TAB:
case 13: //KEY_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 === 9) { if (e.keyCode === keys.TAB) {
return; return;
} }
break; break;
case 38: //KEY_UP: case keys.UP:
this.moveUp(); that.moveUp();
break; break;
case 40: //KEY_DOWN: case keys.DOWN:
this.moveDown(); that.moveDown();
break; break;
default: default:
return; return;
@ -265,18 +295,18 @@
}, },
onKeyUp: function (e) { onKeyUp: function (e) {
if (this.disabled) { var that = this;
if (that.disabled) {
return; return;
} }
switch (e.keyCode) { switch (e.keyCode) {
case 38: //KEY_UP: case keys.UP:
case 40: //KEY_DOWN: case keys.DOWN:
return; return;
} }
var that = this;
clearInterval(that.onChangeInterval); clearInterval(that.onChangeInterval);
if (that.currentValue !== that.el.val()) { if (that.currentValue !== that.el.val()) {
@ -292,21 +322,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);
} }
}, },
@ -321,12 +354,14 @@
return $.trim(parts[parts.length - 1]); return $.trim(parts[parts.length - 1]);
}, },
getSuggestionsLocal: function (q) { getSuggestionsLocal: function (query) {
q = q.toLowerCase(); var that = this,
queryLowerCase = query.toLowerCase(),
filter = that.options.lookupFilter;
return { return {
suggestions: $.grep(this.options.lookup, function (suggestion) { suggestions: $.grep(that.options.lookup, function (suggestion) {
return suggestion.value.toLowerCase().indexOf(q) !== -1; return filter(suggestion, query, queryLowerCase);
}) })
}; };
}, },
@ -342,7 +377,8 @@
that.suggestions = response.suggestions; that.suggestions = response.suggestions;
that.suggest(); that.suggest();
} else if (!that.isBadQuery(q)) { } else if (!that.isBadQuery(q)) {
that.options.params.query = q; options.onSearchStart.call(that.element, q);
options.params.query = q;
$.ajax({ $.ajax({
url: options.serviceUrl, url: options.serviceUrl,
data: options.params, data: options.params,
@ -350,6 +386,7 @@
dataType: 'text' dataType: 'text'
}).done(function (txt) { }).done(function (txt) {
that.processResponse(txt); that.processResponse(txt);
options.onSearchComplete.call(that.element, q);
}); });
} }
}, },
@ -368,9 +405,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 () {
@ -379,23 +417,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);
}, },
@ -411,37 +450,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;
} }
@ -450,41 +491,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,
@ -495,16 +542,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) {
@ -558,5 +605,4 @@
} }
}); });
}; };
}));
}(jQuery));

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long