This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjquery-autocomplete.js
executable file
·636 lines (582 loc) · 23.1 KB
/
jquery-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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
* Auto-complete plugin for jQuery
* Licence : GPL v3 : http://www.gnu.org/licenses/gpl.html
* Author : DEMONTE Jean-Baptiste
* Contact : [email protected]
* Url : https://github.com/jbdemonte/autocomplete
*/
(function ($, undef) {
//*************************************************
// global variables
//*************************************************
var namespace = "autocomplete", // used to store the autocomplete object in $.data() and to create class name
win = $(window),
defaults = {
ajax: { // options for $.ajax
url: document.URL,
type: "POST",
dataType: "json",
data: {}
},
cb: { // callback
populate: undef, // popupate data to send in $.ajax, if not define, data name is input name or id
cast: undef, // cast an item <mixed> to string in order to display it to the completion
filter: undef, // filtering function
process: undef, // after getting the result, it allows to manipulate data before displaying the completion
preselect: undef, // on highlight item
select: undef, // on select item
unselect: undef, // on validate a non item value
force: undef // enter on a non item (enter after an empty list)
},
width: "auto", // auto : min-width = width of the input, false : width of the input else value
offset: undef, // display offset
delay: 250, // delay in ms after key pressed and before post
name: undef, // post key : name, else input[name], else input[id]
minLength: 1, // min lenght to complete : 0 / false : not used, > 0 : min length
cache: true, // ajax : cache result to save exchange
once: false, // ajax : false : idle, true : only require ajax exchange once => data source don't change : set filter to true if not defined in init
source: undef, // undef => ajax, [], string or callback function
match: true, // run match filter
prefix: true, // match by prefix of source data
splitChr: undef, // used character to split data (default is \n)
autohide: false, // autohide if not hover : 0 / false : not used, > 0 : delay in ms
loop: true, // up / down loop
selectFirst: true, // select first element on show
className : namespace
},
ua = navigator.userAgent.toLowerCase(),
opera = ua.match(/opera/),
msie = ua.match(/msie/);
//*************************************************
// Mixed functions
//*************************************************
function clone(mixed) {
var result;
if ($.isArray(mixed)) {
result = [];
$.each(mixed, function (i, value) {
result.push(value);
});
} else if (typeof mixed === "object") {
result = $.extend(true, {}, mixed);
} else {
result = mixed;
}
return result;
}
// split data using splitChar or default
function splitData(data, splitChr) {
if (splitChr) {
return data.split(splitChr);
} else {
return data.split(/\r\n|\r|\n/);
}
}
//*************************************************
// class Autocomplete
//*************************************************
function Autocomplete(element) {
var toComplete, toAutoHide, // timeout
dropbox, // jQuery dropbox
options = {}, // options of the autocomplete = user define + default
iHover = -1, // index of highlighted element
current, // store current data to return real object instead of "toString" values
keys = [], // current keys
count = 0, // current count (item count in dropbox)
cache = {}, // ajax cache => [ input value ] = ajax result
binded = false, // events on <input> are binded or not => enable / disable autocomplete
scrolling = false, // true before starting to scroll by using up / down key and false after onScroll event => needed to disable mouse over item event which highlight overed item
handlers = { // functions to bind
key: keyPressed,
focusout: function () {
if (!$(this).data(namespace + "-focus")) {
hide(true);
}
},
dblclick: function () {
if (!dropbox) {
updateToComplete(false);
}
},
resize: function () {
if (dropbox) {
relocate();
}
}
};
// run callback or return source
function getSource(source) {
if (typeof source === "function") {
return getSource(source.call(element, element.val())); // result of the callback is re-processed (in case of result string ...)
} else if (typeof source === "string") {
return splitData(source, options.splitChr);
}
return source;
}
// bind events
function bind() {
if (!binded) {
element[opera ? "keypress" : "keydown"](handlers.key);
element.focusout(handlers.focusout);
element.dblclick(handlers.dblclick);
binded = true;
}
}
// unbind events
function unbind() {
if (binded) {
element.unbind(opera ? "keypress" : "keydown", handlers.key);
element.unbind("focusout", handlers.focusout);
element.unbind("dblclick", handlers.dblclick);
binded = false;
}
}
function relocate() {
var offset = element.offset(),
optOffset = typeof options.offset === "function" ? options.offset() : options.offset;
dropbox.offset({
top: offset.top + (optOffset && optOffset.top ? optOffset.top : element.outerHeight() + 1),
left: offset.left + (optOffset && optOffset.left ? optOffset.left : 0)
});
}
// restart the timeout to run autohide
function updateToAutoHide() {
if (!options.autohide) {
return;
}
stopToAutoHide();
toAutoHide = setTimeout(
function () {
hide(true);
},
options.autohide
);
}
// stop the autohide
function stopToAutoHide() {
if (toAutoHide) {
clearTimeout(toAutoHide);
toAutoHide = undef;
}
}
// restart the timeout to run the autocompletion
function updateToComplete(noWait) {
clearTimeout(toComplete);
toComplete = setTimeout(
complete,
noWait ? 0 : options.delay
);
}
// highlight on/off an item by its index (0..n-1)
function hoverize(i, visible) {
var li = dropbox ? $("li", dropbox).eq(i) : undef;
if (li) {
li[(visible ? "add" : "remove") + "Class"]("hover");
if (visible) {
scroll(li);
}
}
}
// scroll to make visible if needed the selected item
function scroll(target) {
var top = dropbox.scrollTop(),
height = dropbox.innerHeight(),
eTop = target.position().top,
eHeight = target.outerHeight();
if (eTop < 0) {
scrolling = true;
dropbox.scrollTop(top + eTop);
} else if (eTop + eHeight > height) {
scrolling = true;
dropbox.scrollTop(top + eTop - height + eHeight);
}
}
// locate next index to highlight
function getPageUpDownItem(up) {
if (!dropbox) {
return false;
}
var height = dropbox.innerHeight(),
pageCount = 0,
next = iHover;
// count visible element to process pageUp/Down
$("li", dropbox).each(function () {
var li = $(this);
pageCount += (li.position().top >= 0) && (li.position().top + li.outerHeight() <= height) ? 1 : 0;
});
if (iHover < 0) { // not highlighted
return (up ? count : pageCount) - 1; // up : last item, down : last of first pageCount
}
next += up ? -pageCount : pageCount;
next = Math.max(0, next);
next = Math.min(next, count - 1);
if (options.loop && (next === iHover)) { // borders
next = next === 0 ? count - 1 : 0;
}
return next;
}
// manage key pressed
function keyPressed(e) {
var next, li,
c = e.which;
if (c === 9) { // tab
return;
}
if (!dropbox && (c !== 27) && (c !== 13)) { // completion empty and not [esc] or [enter]
updateToComplete(false);
} else if ((c === 38) || (c === 40)) { // up / down
next = iHover + (c === 38 ? -1 : 1);
if (options.loop) {
if (next < 0) {
next = count - 1;
} else if (next > count - 1) {
next = 0;
}
}
next = Math.max(0, next);
next = Math.min(next, count - 1);
preselect(next);
e.preventDefault();
} else if ((c === 33) || (c === 34)) { // page up / down
next = getPageUpDownItem(c === 33);
if (next !== false) {
preselect(next);
}
e.preventDefault();
} else if (c === 13 || c === 39) { // enter or right arrow
if (iHover !== -1) {
li = $("li", dropbox).eq(iHover);
select(iHover, options.cb.cast ? options.cb.cast(current[li.data("key")]) : li.text());
e.preventDefault();
e.stopImmediatePropagation();
} else {
hide(true);
if (c === 13 && options.cb.force) {
options.cb.force.call(element);
}
}
} else if (c === 27) { // esc
preselect(-1);
hide(true);
} else {
updateToComplete(false);
}
}
// create the data object to send in $.ajax
function getData() {
var data, name = "value";
if (options.cb.populate) {
data = $.extend(true, {}, options.ajax.data, options.cb.populate.call(element));
} else {
data = $.extend(true, {}, options.ajax.data);
if (options.name && options.name.length) {
name = options.name;
} else if (element.attr("name") && element.attr("name").length) {
name = element.attr("name");
} else if (element.attr("id") && element.attr("id").length) {
name = element.attr("id");
}
data[name] = element.val();
}
return data;
}
// branch complete : ajax or use local source
function complete() {
var value = element.val();
// check min length required to run completion
if (options.minLength && (options.minLength > value.length)) {
if (hide(true)) {
preselect(-1);
}
return;
}
if (options.source) {
completeSource();
} else {
completeAjax();
}
}
/**
* filter data to match with user input
* @param data {Array|Object}
* @param cast {function}
* @return {Object}
*/
function matchFilter(data, cast) {
var val = element.val(),
re = new RegExp((options.prefix ? "^" : "") + val.replace(/[\-\[\]{}()*+?.,\\\^\$\|#\s]/g, "\\$&"), "i"), //escape regular expression
result = [];
// value is empty and minLenght is 0 (else, can't reach this filter)
if (!val.length) {
return data;
}
$.each(data, function (key, value) {
if (re.test(cast(value))) {
result.push(value);
}
});
return result;
}
// run the completion : use local source
function completeSource() {
show(getSource(options.source), options.match);
}
// run the completion : use cache or call $.ajax
function completeAjax() {
var settings, data,
value = element.val();
// use cache if available
if (cache && ((options.once && !$.isEmptyObject(cache)) || (options.cache && (typeof cache[value] !== "undefined")))) {
data = options.once ? clone(cache) : clone(cache[value]);
// user process
if (options.cb.process) {
data = options.cb.process.call(element, data, options.once ? "once" : "cache");
}
if (typeof data === "string") {
data = splitData(data, options.splitChr);
}
show(data, options.match);
return;
}
settings = $.extend(true, {}, options.ajax);
settings.success = function (data, textStatus, jqXHR) {
// store result if it will be re-used
if (options.once) {
cache = clone(data);
} else if (options.cache) {
cache[value] = clone(data);
}
// user process
if (options.cb.process) {
data = options.cb.process.call(element, data, textStatus, jqXHR);
}
if (typeof data === "string") {
data = splitData(data, options.splitChr);
}
show(data, options.match);
};
settings.data = getData();
$.ajax(settings);
}
// preselect an item (highlight : off the previous, on the new + run callback)
function preselect(next) {
var key;
updateToAutoHide();
if (iHover === next) {
return;
}
hoverize(iHover, false);
iHover = next;
hoverize(iHover, true);
if (options.cb.preselect) {
if (iHover === -1) {
options.cb.preselect.call(element);
} else {
key = keys[iHover];
options.cb.preselect.call(element, current[key], key, iHover);
}
}
}
// select an item : select data in textbox, run the callback
function select(i, value) {
var key = keys[i];
stopToAutoHide();
if (value !== undef) {
element.val(value);
}
hide();
element.focus();
if (options.cb.select) {
options.cb.select.call(element, current[key], key, i);
}
}
// use data receive from post or cache to display the selectbox
function show(data, match) {
var position = element.position(),
width = msie ? element.outerWidth() : element.width(),
cast = options.cb.cast || function (s) { return s; };
hide();
if (options.cb && options.cb.filter) {
data = options.cb.filter(data);
}
if (!data) {
return;
}
if ((typeof match === "undefined" && options.filter) || match) {
data = matchFilter(data, cast);
}
current = data;
dropbox = $(document.createElement("ul")).addClass(options.className);
dropbox.css({
position: "absolute",
left: position.left + "px",
top: (position.top + element.outerHeight()) + "px"
});
dropbox.scroll(function () {
scrolling = false;
});
// adjust width
if (options.width === "auto") {
dropbox.css(msie ? "width" : "minWidth", width + "px");
} else if (options.width === false) {
dropbox.css({
width: width + "px",
overflow: "hidden"
});
} else {
dropbox.css({
width: typeof options.width === "function" ? options.width() : options.width,
overflow: "hidden"
});
}
// add items
iHover = -1;
count = 0;
keys = [];
$.each(current, function (key, value) {
var li = $(document.createElement("li")),
a = $(document.createElement("a")),
i = count;
a.click(function (event) {
event.stopPropagation();
select(i, cast(value));
});
li.data("key", key);
li.click(function (event) {
event.stopPropagation();
select(i, cast(value));
});
li.hover(function () {
if (!scrolling) { // on manual scrolling (up / down key), if mouse is over item, this event must be disable
preselect(i);
}
});
dropbox.append(li.append(a.append(cast(value, true))));
keys[i] = key;
count += 1;
});
if (!count) {
dropbox.remove();
return;
}
// while clicking on an item, element trigger the focusout, so the item click is lost
dropbox.hover(
function () {
element.data(namespace + "-focus", true);
stopToAutoHide();
},
function () {
element.data(namespace + "-focus", false);
updateToAutoHide();
if (!element.is(":focus")) {
element.trigger("focusout");
}
}
);
$("body").append(dropbox);
win.on("resize", handlers.resize);
relocate();
// manage min-width, min-height, max-width, max-height for IE
if (msie) {
$.each(["min", "max"], function (isMax, type) {
$.each(["Width", "Height"], function (i, property) {
var v = parseInt(dropbox.css(type + property), 10);
if (!isNaN(v) && ((dropbox[property.toLowerCase()]() < v) ^ isMax)) {
dropbox.css(property.toLowerCase(), v + "px");
}
});
});
}
if (options.selectFirst) {
preselect(0);
}
updateToAutoHide();
}
// look for value in dropbox
function reverse(value) {
var result = undef;
$("li", dropbox).each(function (i, li) {
if (result === undef && $(li).text() === value) {
result = i;
}
});
return result;
}
// hide the select box
function hide(checkReverse) {
if (dropbox) {
if (checkReverse) { // user escape or not select any item, but value is in the list, so run callback
var value = element.val(),
index = !value.length || (options.minLength && (options.minLength > value.length)) ? undef : reverse(value);
if (index !== undef) {
select(index);
return false;
}
}
if (iHover >= 0 && options.cb.unselect) {
options.cb.unselect.call(element);
}
stopToAutoHide();
dropbox.remove();
win.off("resize", handlers.resize);
dropbox = undef;
iHover = -1;
return true;
}
return false;
}
return {
init: function (opts) {
// extends defaults options
options = $.extend(true, {}, defaults, opts);
// initialise source data
if (typeof options.source === "string") {
options.source = splitData(options.source, options.splitChr);
}
// some browsers use key "down" to make their own autocompletion (Opera)
element.attr("autocomplete", "off");
// bind events
bind();
},
flushCache: function () {
cache = {};
},
enable: function () {
bind();
},
disable: function () {
unbind();
preselect(-1);
hide();
},
close: function () {
hide();
},
trigger: function () {
updateToComplete(true);
},
display: function (source, match) {
show(getSource(source), match);
}
};
}
//*************************************************
// Plugin jQuery
//*************************************************
$.fn.autocomplete = function (p1, p2, p3) {
$.each(this, function () { // loop on each jQuery objects
var element = $(this),
current = element.data(namespace);
if (!current) {
current = new Autocomplete(element);
element.data(namespace, current);
}
if (typeof p1 === "string" && current.hasOwnProperty(p1)) {
current[p1](p2, p3);
} else {
current.init(p1);
}
});
return this;
};
}(jQuery));