-
Notifications
You must be signed in to change notification settings - Fork 20
/
jquery.ui.autocomplete.js
413 lines (383 loc) · 13.5 KB
/
jquery.ui.autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* @fileoverview jQuery Autocomplete
* Version 1.2 (13/03/2010)
* Written by Yehuda Katz ([email protected]) and Rein Henrichs ([email protected])
* Additional contributions from Emmanuel Gomez, Austin King,
* Nikos Dimitrakopoulos, Javier Gonel
* @requires jQuery v1.2, jQuery dimensions plugin
*
* Copyright 2007-2010 Yehuda Katz, Rein Henrichs
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* @description Form autocomplete plugin using preloaded or Ajax JSON data source
*
* Simple autocomplete with basic JSON data source
* <code>$('input#user-name').autocomplete({list: ["quentin", "adam", "admin"]});</code>
*
* Simple autocomplete with Ajax loaded JSON data source
* <code>$('input#user-name').autocomplete({ajax: "/usernames.js"});</code>
*
*/
(function($) {
/** @namespace */
$.ui = $.ui || {};
/** @namespace */
$.ui.autocomplete = $.ui.autocomplete || {};
var active = {};
var KEY = {
ESC: 27,
RETURN: 13,
TAB: 9,
BS: 8,
DEL: 46,
UP: 38,
DOWN: 40
};
$.fn.autocomplete = function(opt) {
/* Default options */
opt = $.extend({}, {
/**
* Milliseconds after the last keystroke to (re-)filter the list
*/
timeout: 500,
/**
* How many options are too many?
* if there are more matches than threshold, the list will not be displayed
*/
threshold: 100,
/**
* By default the with of the input box is used for the autocomplete
* suggestions. If you want to change that, specify here a width in pixels.
*/
adjustWidth: true,
/**
* Maximum number of items to show. By default show all.
*/
maxResults: undefined,
/**
* Minumum number of characters needed before starting the autocomplete
* If set to 'undefined', the empty string will be passed to the
* 'filterList' function. This can be used to display
* a full list when no characters are introduced.
*/
minCharacters: 0,
/**
* Get the complete list of items
* override this to control how to get the list of potential matches
* default is to use the 'list' option passed during initialization
* and trigger "updateList" event with list as data
*
* @param input the text input being autocompleted
*/
getList: function(input) {
input.triggerHandler("updateList", [opt.list]);
},
/**
* Called to determine if a given data item matches the user's input
*
* @param {string} item data item being tested for match
* @param {RegExp} matcher regex to test the item with
* @return {boolean} true if this data item matches user input
*/
match: function(item, matcher) {
// Do not use !==
return (item.match(matcher) != undefined);
},
/**
* Called to build the matcher
*
* @param typed the text entered by user in the text input
* @return regex used to filter the complete list
*/
matcher: function(typed) {
return new RegExp(typed);
},
/**
* Update the list of matching items
* override this to control how the filtered list is generated from the complete list
*
* @param list complete list to be filtered
* @param val text entered in the text input
* @return filtered list with items that match _val_ (however matching is defined)
*/
filterList: function(list, val) {
var matcher = opt.matcher(val),
grepCallback = function(text, i) {
return opt.match(text, matcher);
},
index = 0;
if (this.maxResults) {
grepCallback = function(text, i) {
if (index>this.maxResults) {
return false;
}
index++;
return opt.match(text, matcher);
};
}
return $.grep(list, grepCallback);
},
/**
* Update the list of matching items
* override this to control how markup is built from the list of matches
*
* @param unfilteredList unfiltered list of potential matches
* @param val the text in the input field
*
* @return container (which should be positioned and visible)
*/
updateList: function(unfilteredList, val) {
if (opt.minCharacters && val.length <= opt.minCharacters) {
return false;
}
var list = opt.filterList(unfilteredList, val);
if(list.length === 0 || list.length > opt.threshold) {return false;}
return opt.buildList(list);
},
/**
* Build the list of matches
* override this to control how markup is built from the list of matches
* make sure the elements in the list have the matching object
* in their $.fn.data as "originalObject"
*
* @param list list of items matching user input
* @return container
*/
buildList: function(list) {
var listItems = $(list).map(function() {
var node = $(opt.template(this))[0];
$.data(node, "originalObject", this);
return node;
});
// IE seems to wrap the wrapper in a random div wrapper so
// drill down to the node in opt.wrapper.
var container = $(opt.wrapper).append(listItems);// .parents(":last").children();
// TODO: need to verify that parents(":last").children really does the below
var wrapTag = $(opt.wrapper)[0].tagName;
while (container[0].tagName !== wrapTag) {
container = container.children(':first');
}
return container;
},
wrapper: "<ul class='jq-ui-autocomplete'></ul>",
/**
* Display the list of matches
* override this to control the container position or size
* or to skip appending the list to the html body
*
* @param input the text input being autocompleted
* @param container the container of the list of matches (typically an ol/ul)
*
* @return container (which should be positioned and visible)
*/
displayList: function(input, container) {
var offset = input.offset();
container
.css({
top: offset.top + input.outerHeight(),
left: offset.left,
width: this.adjustWidth?input.width():undefined
})
.appendTo("body");
return container;
},
/**
* Dismiss the list of matches
* override this to control how the list container is dismissed
* default is to remove the element
*
* @param container the container of the list of matches (typically an ol/ul)
*/
dismissList: function(container) {
container.remove();
},
template: function(str) { return "<li>" + opt.insertText(str) + "</li>"; },
/**
* Provide a value for the text input from the active object,
* also used to fill the li element in the default _template_ implementation
*
* @param item active item in the list
*/
insertText: function(item) { return item; }
}, opt);
/*
* Additional options from autocomplete.ext (for example 'ajax', and 'templateText')
* if these options where passed in the opt object and the $.ui.autocomplete.ext is present.
*/
if($.ui.autocomplete.ext) {
for(var ext in $.ui.autocomplete.ext) {
if(opt[ext]) {
opt = $.extend(opt, $.ui.autocomplete.ext[ext](opt));
delete opt[ext];
}
}
}
function preventTabInAutocompleteMode(e) {
var k = e.which || e.keycode;
if ($.data(document.body, "autocompleteMode") && k == KEY.TAB) {
e.preventDefault();
}
}
function startTypingTimeout(e, element) {
$.data(element, "typingTimeout", window.setTimeout(function() {
$(e.target || e.srcElement).triggerHandler("autocomplete");
}, opt.timeout));
}
function handleKeyDownUp(e) {
var k = e.which || e.keycode;
if ((k == KEY.UP || k == KEY.DOWN) && !$.data(this, "typingTimeout")) {
startTypingTimeout(e, this);
} else if (k == KEY.BS || k == KEY.DEL) {
var typingTimeout = $.data(this, "typingTimeout");
if (typingTimeout) {
window.clearInterval(typingTimeout);
}
startTypingTimeout(e, this);
} else {
preventTabInAutocompleteMode(e);
}
}
return this.each(function() {
$(this)
.attr("autocomplete", "off")
.keydown(handleKeyDownUp)
.keyup(handleKeyDownUp)
.keypress(function(e) {
var typingTimeout = $.data(this, "typingTimeout");
var k = e.keyCode || e.which; // keyCode == 0 in Gecko/FF on keypress
if(typingTimeout) {
window.clearInterval(typingTimeout);
}
if($.data(document.body, "suppressKey")) {
return $.data(document.body, "suppressKey", false);
} else if($.data(document.body, "autocompleteMode") && k < 32 && k != KEY.BS && k != KEY.DEL) {
return false;
} else if (k == KEY.BS || k == KEY.DEL || k > 32) { // more than ESC and RETURN and the like
startTypingTimeout(e, this);
}
})
.bind("autocomplete", function() {
var self = $(this);
self.one("updateList", function(e, completeList, matchVal) {
var container = opt.updateList(completeList, matchVal || self.val());
// turn off autcomplete mode even if the list is empty (container === false)
$("body").triggerHandler("off.autocomplete");
if (container === false) { return false; }
opt.container = opt.displayList(self, container);
$("body").autocompleteMode(opt.container, self, container.find("li").length, opt);
});
opt.getList(self);
});
if (typeof opt.init == "function") { opt.init(self); }
});
};
$.fn.autocompleteMode = function(container, input, size, opt) {
var original = input.val(),
selected = -1,
mouseDown = false;
$.data(document.body, "autocompleteMode", true);
$("body").one("cancel.autocomplete", function() {
input.triggerHandler("cancelled.autocomplete");
$("body").triggerHandler("off.autocomplete");
input.val(original);
});
$("body").bind("activate.autocomplete", function(e) {
console.log('activate');
// Try hitting return to activate autocomplete and then hitting it again on blank input
// to close it. w/o checking the active object first this input.triggerHandler() will barf.
if (active.length) {
// if activate and selected -> clicked!
input.triggerHandler("activated.autocomplete", [$.data(active[0], "originalObject"), active]);
}
// if nothing active when activate event and we had a mousedown event before
// then the user is dragging something in the list. Ignore action.
else if(mouseDown) {
mouseDown = false;
if(document.activeElement && document.activeElement != input)
input.trigger("focus");
return false;
}
$("body").triggerHandler("off.autocomplete");
});
$("body").one("off.autocomplete", function(e, reset) {
opt.dismissList(container);
$.data(document.body, "autocompleteMode", false);
input.unbind("keydown.autocomplete");
$("body").add(window)
.unbind("click.autocomplete")
.unbind("cancel.autocomplete")
.unbind("activate.autocomplete");
});
// If a click bubbles all the way up to the window, close the autocomplete
$(window).bind("click.autocomplete", function() {
$("body").triggerHandler("cancel.autocomplete");
});
var select = function() {
active = container.find("li")
.removeClass("active")
.filter(":visible")
.slice(selected, selected + 1)
.addClass("active");
if (active.length) {
input.triggerHandler("itemSelected.autocomplete", [$.data(active[0], "originalObject"), active]);
input.val(opt.insertText($.data(active[0], "originalObject")));
} else {
input.triggerHandler("noneSelected.autocomplete");
input.val(original);
}
};
container
.mouseover(function(e) {
// If you hover over the container, but not its children, return
if(e.target == container[0]) { return; }
var selectedItem = $(e.target).is('li') ? $(e.target)[0] : $(e.target).parents('li')[0];
// Set the selected item to the item hovered over and make it active
selected = container.find("li").index(selectedItem);
select();
})
// IE8 was triggering spurious activate events when clicking outside
// the container when a list item was active. I couldn't figure out where
// the activate events were coming from, so I'm deactivating the list
.mouseout(function(e) {
selected = -1;
select();
})
.mousedown(function() { mouseDown = true; console.log('mousedown');})
.bind("click.autocomplete", function(e) {
$("body").triggerHandler("activate.autocomplete");
$.data(document.body, "suppressKey", false);
});
input
.bind("keydown.autocomplete", function(e) {
var k = e.which || e.keyCode; // in IE e.which is undefined
switch(k) {
case KEY.ESC:
$("body").triggerHandler("cancel.autocomplete");
break;
// TAB & Return behave in the same way.
case KEY.TAB:
case KEY.RETURN:
if (selected == -1) {
// If nothing is selected, select the first
selected = selected >= size - 1 ? 0 : selected + 1;
select();
}
$("body").triggerHandler("activate.autocomplete");
break;
case KEY.DOWN:
selected = selected >= size - 1 ? 0 : selected + 1;
select();
break;
case KEY.UP:
selected = selected <= 0 ? size - 1 : selected - 1;
select();
break;
default:
return true;
}
$.data(document.body, "suppressKey", true);
});
};
})(jQuery);