Skip to content

Commit 21b90eb

Browse files
author
Tomas Kirda
committed
Expose default options, closes #478
1 parent 43a3150 commit 21b90eb

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ The standard jquery.autocomplete.js file is around 13KB when minified.
6767
* `onInvalidateSelection`: `function () {}` called when input is altered after selection has been made. `this` is bound to input element.
6868
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
6969

70+
## Default Options
71+
72+
Default options for all instances can be accessed via `$.Autocomplete.defaults`.
73+
7074
## Instance Methods
7175

7276
Autocomplete instance has following methods:

src/jquery.autocomplete.js

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,51 +49,12 @@
4949
UP: 38,
5050
RIGHT: 39,
5151
DOWN: 40
52-
};
52+
},
53+
54+
noop = $.noop;
5355

5456
function Autocomplete(el, options) {
55-
var noop = $.noop,
56-
that = this,
57-
defaults = {
58-
ajaxSettings: {},
59-
autoSelectFirst: false,
60-
appendTo: document.body,
61-
serviceUrl: null,
62-
lookup: null,
63-
onSelect: null,
64-
width: 'auto',
65-
minChars: 1,
66-
maxHeight: 300,
67-
deferRequestBy: 0,
68-
params: {},
69-
formatResult: Autocomplete.formatResult,
70-
formatGroup: Autocomplete.formatGroup,
71-
delimiter: null,
72-
zIndex: 9999,
73-
type: 'GET',
74-
noCache: false,
75-
onSearchStart: noop,
76-
onSearchComplete: noop,
77-
onSearchError: noop,
78-
preserveInput: false,
79-
containerClass: 'autocomplete-suggestions',
80-
tabDisabled: false,
81-
dataType: 'text',
82-
currentRequest: null,
83-
triggerSelectOnValidInput: true,
84-
preventBadQueries: true,
85-
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
86-
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
87-
},
88-
paramName: 'query',
89-
transformResult: function (response) {
90-
return typeof response === 'string' ? $.parseJSON(response) : response;
91-
},
92-
showNoSuggestionNotice: false,
93-
noSuggestionNotice: 'No results',
94-
orientation: 'bottom',
95-
forceFixPosition: false
96-
};
57+
var that = this;
9758

9859
// Shared variables:
9960
that.element = el;
@@ -109,7 +70,7 @@
10970
that.isLocal = false;
11071
that.suggestionsContainer = null;
11172
that.noSuggestionsContainer = null;
112-
that.options = $.extend({}, defaults, options);
73+
that.options = $.extend({}, Autocomplete.defaults, options);
11374
that.classes = {
11475
selected: 'autocomplete-selected',
11576
suggestion: 'autocomplete-suggestion'
@@ -127,7 +88,52 @@
12788

12889
$.Autocomplete = Autocomplete;
12990

130-
Autocomplete.formatResult = function (suggestion, currentValue) {
91+
Autocomplete.defaults = {
92+
ajaxSettings: {},
93+
autoSelectFirst: false,
94+
appendTo: document.body,
95+
serviceUrl: null,
96+
lookup: null,
97+
onSelect: null,
98+
width: 'auto',
99+
minChars: 1,
100+
maxHeight: 300,
101+
deferRequestBy: 0,
102+
params: {},
103+
formatResult: _formatResult,
104+
formatGroup: _formatGroup,
105+
delimiter: null,
106+
zIndex: 9999,
107+
type: 'GET',
108+
noCache: false,
109+
onSearchStart: noop,
110+
onSearchComplete: noop,
111+
onSearchError: noop,
112+
preserveInput: false,
113+
containerClass: 'autocomplete-suggestions',
114+
tabDisabled: false,
115+
dataType: 'text',
116+
currentRequest: null,
117+
triggerSelectOnValidInput: true,
118+
preventBadQueries: true,
119+
lookupFilter: _lookupFilter,
120+
paramName: 'query',
121+
transformResult: _transformResult,
122+
showNoSuggestionNotice: false,
123+
noSuggestionNotice: 'No results',
124+
orientation: 'bottom',
125+
forceFixPosition: false
126+
};
127+
128+
function _lookupFilter(suggestion, originalQuery, queryLowerCase) {
129+
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
130+
};
131+
132+
function _transformResult(response) {
133+
return typeof response === 'string' ? $.parseJSON(response) : response;
134+
};
135+
136+
function _formatResult(suggestion, currentValue) {
131137
// Do not replace anything if there current value is empty
132138
if (!currentValue) {
133139
return suggestion.value;
@@ -144,7 +150,7 @@
144150
.replace(/&lt;(\/?strong)&gt;/g, '<$1>');
145151
};
146152

147-
Autocomplete.formatGroup = function (suggestion, category) {
153+
function _formatGroup(suggestion, category) {
148154
return '<div class="autocomplete-group"><strong>' + category + '</strong></div>';
149155
};
150156

0 commit comments

Comments
 (0)