forked from jbdemonte/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-autocomplete.js
executable file
·615 lines (550 loc) · 19.8 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
/*
* Autocomplete Plugin for JQuery
* Version :
* Date :
* Licence : GPL v3 : http://www.gnu.org/licenses/gpl.html
* Author : DEMONTE Jean-Baptiste
* Contact : [email protected]
* Web site :
*/
(function ($) {
//*************************************************
// global variables
//*************************************************
var publics = ['enable', 'disable', 'flushCache', 'trigger', 'display', 'close'],
global = this,
namespace = 'autocomplete'; // used to store the autocomplete object in $.data() and to create class name
//*************************************************
// default options
//*************************************************
var defOptions = {
ajax:{ // options for $.ajax
url: document.URL,
type: "POST",
dataType: "json",
data:{}
},
cb:{ // callback
populate:null, // popupate data to send in $.ajax, if not define, data name is input name or id
cast:null, // cast an item<mixed> to string in order to display it to the completion
process:null, // after getting the result, it allows to manipulate data before displaying the completion
preselect:null, // on highlight item
select: null, // on select item
unselect: null // on validate a non item value
},
width:'auto', // auto : min-width = width of the input, false : width of the input, "other" : "other"
delay: 250, // delay in ms after key pressed and before post
name: null, // 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
filter: false, // ajax : after ajax, run match filter
source:null, // null => ajax, [], string or callback function
prefix:true, // match by prefix of source data
splitChr:null, // 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
className : namespace
};
//*************************************************
// 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;
}
//*************************************************
// class Autocomplete
//*************************************************
function Autocomplete($this){
var options = {}, // options of the autocomplete = user define + default
toComplete, toAutoHide, // timeout
$list, // jQuery dropbox
iHover = -1, // index of highlighted element
dataCount = 0, // item count in dropbox
gData, // store current data to return real object instead of "toString" values
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
that = this,
handlers = { // functions to bind
key: function(e){
that.key.apply(that, [e]);
},
focusout: function(e){
if (!$(this).data(namespace + '-focus')){
that.hide(true);
}
},
dblclick: function(){
if (!$list){
that.updateTOComplete();
}
}
};
// initialize the completion
this.init = function(opts){
// extends defaults options
options = $.extend(true, {}, defOptions, opts);
// initialise source data
if (typeof(options.source) === 'string'){
options.source = this.splitData(options.source);
}
// if 'filter' not defined and 'once' defined to true, default 'filter' is true
// => used to set a flat file as data source
if ( opts && (typeof(opts) === 'object') && (typeof(opts['once']) !== 'undefined') && opts.once ){
if (typeof(opts['filter']) === 'undefined'){
options.filter = true;
}
}
// some browsers use key "down" to make their own autocompletion (Opera)
$this.attr('autocomplete', 'off');
// bind events
this.bind();
}
// split data using splitChar or default
this.splitData = function(data){
if (options.splitChr){
return data.split(options.splitChr);
} else {
return data.split(/\r\n|\r|\n/);
}
}
// run callback or return source
this.getSource = function(source){
if (typeof(source) === 'function'){
return this.getSource.apply(this, [source.apply($this, [$this.val()])]); // result of the callback is re-processed (in case of result string ...)
} else if (typeof(source) === 'string'){
return this.splitData(source)
}
return source;
}
// flush cache
this.flush = function(){
cache = {};
}
// bind events
this.bind = function(){
if (!binded){
$this[$.browser.opera ? 'keypress' : 'keydown'](handlers.key);
$this.focusout(handlers.focusout);
$this.dblclick(handlers.dblclick);
binded = true;
}
}
// unbind events
this.unbind = function(){
if (binded){
$this.unbind($.browser.opera ? 'keypress' : 'keydown', handlers.key);
$this.unbind('focusout', handlers.focusout);
$this.unbind('dblclick', handlers.dblclick);
binded = false;
}
}
// restart the timeout to run autohide
this.updateToAutoHide = function(){
if (!options.autohide){
return;
}
this.stopToAutoHide();
toAutoHide = setTimeout(function(){that.hide(that, [true]);}, options.autohide);
}
// stop the autohide
this.stopToAutoHide = function(){
if (toAutoHide){
clearTimeout(toAutoHide);
toAutoHide = null;
}
}
// restart the timeout to run the autocompletion
this.updateTOComplete = function(noWait){
var that = this;
clearTimeout(toComplete);
toComplete = setTimeout(function(){that.complete.apply(that, []);}, noWait ? 0 : options.delay);
}
// highlight on/off an item by its index (0..n-1)
this.hover = function(i, show){
var $li = $list ? $('li', $list).eq(i) : null;
if ($li){
$li[(show ? 'add' : 'remove')+'Class']('hover');
if (show){
this.scroll($li);
}
}
}
// scroll to make visible if needed the selected item
this.scroll = function($element){
var top = $list.scrollTop(),
height = $list.innerHeight(),
eTop = $element.position().top,
eHeight = $element.outerHeight();
if (eTop < 0){
scrolling = true;
$list.scrollTop(top + eTop);
} else if (eTop + eHeight > height){
scrolling = true;
$list.scrollTop(top + eTop - height + eHeight);
}
}
// locate next index to highlight
this.getPageUpDownItem = function(up){
if (!$list){
return false;
}
var height = $list.innerHeight(),
pageCount = 0, next = iHover;
// count visible element to process pageUp/Down
$('li', $list).each(function(i, element){
var $element = $(element);
pageCount += ($element.position().top >= 0) && ($element.position().top + $element.outerHeight() <= height) ? 1 : 0;
});
if (iHover < 0){ // not highlighted
return (up ? dataCount : pageCount) - 1; // up : last item, down : last of first pageCount
}
next += up ? -pageCount : pageCount;
next = Math.max(0, next);
next = Math.min(next, dataCount-1);
if (options.loop && (next == iHover)){ // borders
next = next === 0 ? dataCount-1 : 0;
}
return next;
}
// manage key pressed
this.key = function(e){
var c = e.keyCode, next;
if (c === 9) { // tab
// do nothing
} else if (!$list && (c !== 27) && (c !== 13)){ // completion empty and not [esc] or [enter]
this.updateTOComplete();
} else if ( (c === 38) || (c === 40) ){ // up / down
next = iHover + (c === 38 ? -1 : 1);
if (options.loop){
if (next < 0){
next = dataCount - 1;
} else if (next > dataCount - 1){
next = 0;
}
}
next = Math.max(0, next);
next = Math.min(next, dataCount-1);
this.preselect(next);
e.preventDefault();
} else if ( (c === 33) || (c === 34) ){ // page up / down
next = this.getPageUpDownItem(c === 33);
if (next !== false){
this.preselect(next);
}
e.preventDefault();
} else if (c === 13){ // enter
if (iHover !== -1){
this.select(iHover, $('li', $list).eq(iHover).text());
e.preventDefault();
e.stopImmediatePropagation();
} else {
this.hide(true);
}
} else if (c === 27){ // esc
this.preselect(-1);
this.hide(true);
} else {
this.updateTOComplete();
}
}
// create the data object to send in $.ajax
this.data = function(){
var data, name = 'value';
if (typeof(options.cb.populate) === 'function'){
data = $.extend(true, {}, options.ajax.data, options.cb.populate.apply($this, []));
} else {
data = $.extend(true, {}, options.ajax.data);
if (options.name && options.name.length){
name = options.name ;
} else if ($this.attr('name') && $this.attr('name').length) {
name = $this.attr('name');
} else if ($this.attr('id') && $this.attr('id').length) {
name = $this.attr('id');
}
data[ name ] = $this.val();
}
return data;
}
// branch complete : ajax or use local source
this.complete = function (){
var value = $this.val();
// check min length required to run completion
if (options.minLength && (options.minLength > value.length)){
if (this.hide(true)){
this.preselect(-1);
}
return;
}
if (options.source){
this.completeSource();
} else {
this.completeAjax();
}
}
// filter data to match with user input
this.filterData = function(/*array*/data, /*function*/cast){
var re = new RegExp((options.prefix ? '^' : '') + $this.val().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i"), //escape regular expression
result = [],
i;
for(i=0; i<data.length; i++){
if (re.test(cast(data[i]))){
result.push(data[i]);
}
}
return result;
}
// run the completion : use local source
this.completeSource = function(){
this.show(this.getSource(options.source));
}
// run the completion : use cache or call $.ajax
this.completeAjax = function(){
var that = this,
value = $this.val(),
settings;
// use cache if available
if (cache && ((options.once && !$.isEmptyObject(cache)) || (options.cache && (typeof(cache[value]) !== 'undefined')))){
var data = options.once ? clone(cache) : clone(cache[value]);
// user process
if (typeof(options.cb.process) === 'function'){
data = options.cb.process.apply($this, [data, options.once ? 'once' : 'cache']);
}
if (typeof(data) === 'string'){
data = that.splitData(data);
}
this.show(data, options.filter);
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 (typeof(options.cb.process) === 'function'){
data = options.cb.process.apply($this, [data, textStatus, jqXHR]);
}
if (typeof(data) === 'string'){
data = that.splitData(data);
}
that.show.apply(that, [data, options.filter]);
}
settings.data = this.data();
$.ajax(settings);
}
// preselect an item (highlight : off the previous, on the new + run callback)
this.preselect = function(next){
this.updateToAutoHide();
if (iHover === next){
return;
}
this.hover(iHover, false);
iHover = next;
this.hover(iHover, true);
if (typeof(options.cb.preselect) === 'function'){
options.cb.preselect.apply($this, [iHover===-1?null:gData[iHover], iHover]);
}
}
// select an item : select data in textbox, run the callback
this.select = function(i, value){
this.stopToAutoHide();
if (value === null){
value = $this.val();
} else {
$this.val(value);
}
this.hide();
if (typeof(options.cb.select) === 'function'){
options.cb.select.apply($this,[gData[i], i]);
}
}
// use data receive from post or cache to display the selectbox
this.show = function(data, filter){
var that = this,
position = $this.offset(),
width = $.browser.msie ? $this.outerWidth() : $this.width(),
cast = options.cb.cast || function(s){return s};
gData = data;
this.hide();
if (!data || (typeof(data) !== 'object') || !data.length){
return;
}
if ( (typeof(filter) === 'undefined' && options.filter) || filter){
gData = data = this.filterData(data, cast);
}
$list = $('<ul class="'+options.className+'"></ul>')
.css('position', 'absolute')
.css('left', position.left + 'px')
.css('top', (position.top + $this.outerHeight()) + 'px')
.scroll(function(){
scrolling = false;
});
// adjust width
if (options.width === 'auto'){
$list.css($.browser.msie ? 'width' : 'minWidth', width + 'px');
} else if (options.width === false){
$list
.css('width', width + 'px')
.css('overflow', 'hidden');
} else {
$list
.css('width', options.width)
.css('overflow', 'hidden');
}
// add items
iHover = -1;
dataCount = data.length;
$.each(data, function(i, value){
var $li = $('<li></li>'), $a = $('<a></a>');
$a.click(function(){
that.select.apply(that, [i, cast(value)]);
});
$li.hover(
function(){
if (!scrolling){ // on manual scrolling (up / down key), if mouse is over item, this event must be disable
that.preselect(i);
}
}
);
$list.append($li.append($a.append(cast(value))));
});
// while clicking on an item, $this trigger the focusout, so the item click is lost
$list.hover(
function(){
$this.data(namespace + '-focus', true);
that.stopToAutoHide();
},
function(){
$this.data(namespace + '-focus', false);
that.updateToAutoHide();
if (!$this.is(':focus')){
$this.trigger('focusout');
}
}
);
$this.after($list);
// manage min-width, min-height, max-width, max-height for IE
if ($.browser.msie){
$.each("min max".split(" "), function(isMax, type) {
$.each("Width Height".split(" "), function(i, property) {
var v = parseInt($list.css(type + property));
if (!isNaN(v) && ( ($list[property.toLowerCase()]() < v) ^ isMax) ){
$list.css(property.toLowerCase(), v + 'px');
}
});
});
}
this.updateToAutoHide();
}
// look for value in $list
this.reverse = function(value){
var result = null ;
$('li', $list).each(function(i, element){
if ( (result === null) && ($(element).text() === value)){
result = i;
}
});
return result;
}
// hide the selectbox
this.hide = function(reverse){
if ($list) {
if (reverse){ // user escape or not select any item, but value is in the list, so run callback
var value = $this.val(),
index = !value.length || (options.minLength && (options.minLength > value.length)) ? null : this.reverse(value);
if (index !== null){
this.select(index);
return ;
} else if (typeof(options.cb.unselect) === 'function'){
options.cb.unselect.apply($this, []);
}
}
this.stopToAutoHide();
$list.remove();
$list = null;
iHover = -1;
return true;
}
return false;
}
// check if user can call an internal function
this.isPublic = function(name){
for(var i = 0; i < publics.length; i++){
if (publics[i] === name) {
return true;
}
}
return false;
}
// process jQuery call
this.process = function(){
var p = [];
for(var i=0; i<arguments.length; i++){
p.push(arguments[i]);
}
if (p.length && typeof(p[0]) === 'string'){
var fn = p.shift();
if ( this.isPublic(fn) ){
this[fn].apply(this, p)
}
} else {
this.init.apply(this, p);
}
}
}
//*************************************************
// class Autocomplete : Public functions
//*************************************************
Autocomplete.prototype.flushCache = function(){
this.flush();
}
Autocomplete.prototype.enable = function(){
this.bind();
}
Autocomplete.prototype.disable = function(){
this.unbind();
this.preselect(-1);
this.hide();
}
Autocomplete.prototype.trigger = function(){
this.updateTOComplete(true);
}
Autocomplete.prototype.display = function(source, filter){
this.show(this.getSource(source), filter);
}
Autocomplete.prototype.close = function(){
this.hide();
}
//*************************************************
// Plugin jQuery
//*************************************************
$.fn.autocomplete = function(){
var args = arguments;
$.each(this, function() { // loop on each jQuery objects
var $this = $(this),
current = $this.data(namespace);
if (!current){
current = new Autocomplete($this);
$this.data(namespace, current);
}
current.process.apply(current, args);
});
return this;
}
}(jQuery));