2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-25 14:17:45 +00:00

Expose default options, closes #478

This commit is contained in:
Tomas Kirda 2017-03-05 19:28:49 -06:00
parent 43a31502c4
commit 21b90eb7e6
2 changed files with 56 additions and 46 deletions

View File

@ -67,6 +67,10 @@ The standard jquery.autocomplete.js file is around 13KB when minified.
* `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element. * `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element.
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion. * `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
## Default Options
Default options for all instances can be accessed via `$.Autocomplete.defaults`.
## Instance Methods ## Instance Methods
Autocomplete instance has following methods: Autocomplete instance has following methods:

View File

@ -49,51 +49,12 @@
UP: 38, UP: 38,
RIGHT: 39, RIGHT: 39,
DOWN: 40 DOWN: 40
}; },
noop = $.noop;
function Autocomplete(el, options) { function Autocomplete(el, options) {
var noop = $.noop, var that = this;
that = this,
defaults = {
ajaxSettings: {},
autoSelectFirst: false,
appendTo: document.body,
serviceUrl: null,
lookup: null,
onSelect: null,
width: 'auto',
minChars: 1,
maxHeight: 300,
deferRequestBy: 0,
params: {},
formatResult: Autocomplete.formatResult,
formatGroup: Autocomplete.formatGroup,
delimiter: null,
zIndex: 9999,
type: 'GET',
noCache: false,
onSearchStart: noop,
onSearchComplete: noop,
onSearchError: noop,
preserveInput: false,
containerClass: 'autocomplete-suggestions',
tabDisabled: false,
dataType: 'text',
currentRequest: null,
triggerSelectOnValidInput: true,
preventBadQueries: true,
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},
paramName: 'query',
transformResult: function (response) {
return typeof response === 'string' ? $.parseJSON(response) : response;
},
showNoSuggestionNotice: false,
noSuggestionNotice: 'No results',
orientation: 'bottom',
forceFixPosition: false
};
// Shared variables: // Shared variables:
that.element = el; that.element = el;
@ -109,7 +70,7 @@
that.isLocal = false; that.isLocal = false;
that.suggestionsContainer = null; that.suggestionsContainer = null;
that.noSuggestionsContainer = null; that.noSuggestionsContainer = null;
that.options = $.extend({}, defaults, options); that.options = $.extend({}, Autocomplete.defaults, options);
that.classes = { that.classes = {
selected: 'autocomplete-selected', selected: 'autocomplete-selected',
suggestion: 'autocomplete-suggestion' suggestion: 'autocomplete-suggestion'
@ -127,7 +88,52 @@
$.Autocomplete = Autocomplete; $.Autocomplete = Autocomplete;
Autocomplete.formatResult = function (suggestion, currentValue) { Autocomplete.defaults = {
ajaxSettings: {},
autoSelectFirst: false,
appendTo: document.body,
serviceUrl: null,
lookup: null,
onSelect: null,
width: 'auto',
minChars: 1,
maxHeight: 300,
deferRequestBy: 0,
params: {},
formatResult: _formatResult,
formatGroup: _formatGroup,
delimiter: null,
zIndex: 9999,
type: 'GET',
noCache: false,
onSearchStart: noop,
onSearchComplete: noop,
onSearchError: noop,
preserveInput: false,
containerClass: 'autocomplete-suggestions',
tabDisabled: false,
dataType: 'text',
currentRequest: null,
triggerSelectOnValidInput: true,
preventBadQueries: true,
lookupFilter: _lookupFilter,
paramName: 'query',
transformResult: _transformResult,
showNoSuggestionNotice: false,
noSuggestionNotice: 'No results',
orientation: 'bottom',
forceFixPosition: false
};
function _lookupFilter(suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
};
function _transformResult(response) {
return typeof response === 'string' ? $.parseJSON(response) : response;
};
function _formatResult(suggestion, currentValue) {
// Do not replace anything if there current value is empty // Do not replace anything if there current value is empty
if (!currentValue) { if (!currentValue) {
return suggestion.value; return suggestion.value;
@ -144,7 +150,7 @@
.replace(/&lt;(\/?strong)&gt;/g, '<$1>'); .replace(/&lt;(\/?strong)&gt;/g, '<$1>');
}; };
Autocomplete.formatGroup = function (suggestion, category) { function _formatGroup(suggestion, category) {
return '<div class="autocomplete-group"><strong>' + category + '</strong></div>'; return '<div class="autocomplete-group"><strong>' + category + '</strong></div>';
}; };