This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
forked from vufind-org/vufind
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathautocomplete.js
421 lines (404 loc) · 14.1 KB
/
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
/* https://github.com/vufind-org/autocomplete.js 1.0b2 */
(function autocomplete($) {
var element = false,
cache = {},
optionStorage = {},
xhr = false;
function Factory(_input, settings) {
return function acClosure() {
var input = $(this);
var options = optionStorage[input.data('ac-id')];
var _align = function _align() {
var position = input.offset();
element.css({
top: position.top + input.outerHeight(),
minWidth: input.width()
});
if (options.rtl) {
element.css("right", document.body.offsetWidth - position.left - input.outerWidth());
} else {
element.css("left", position.left);
}
};
var _show = function _show() {
element.removeClass(options.hidingClass);
};
var hide = function hide() {
element.addClass(options.hidingClass);
};
var _populate = function _populate(item, eventType) {
input.trigger('ac:select', [item, eventType]);
var ret;
if (options.callback) {
ret = options.callback(item, input, eventType);
if (ret === true && typeof item.href !== 'undefined') {
return window.location.assign(item.href);
}
if (ret === false) {
return false;
}
} else if (typeof item.href !== 'undefined') {
return window.location.assign(item.href);
}
input.val(typeof ret === 'undefined' ? item.value : ret);
// Reset
element.find('.ac-item.selected').removeClass('selected');
$(this).data('ac-selected', -1);
setTimeout(function acPopulateDelay() {
input.focus();
hide();
}, 10);
};
var _listToHTML = function _listToHTML(list, regex) {
var shell = $('<div/>');
for (var i = 0; i < list.length; i++) {
if (typeof list[i] === 'string') {
list[i] = { value: list[i] };
}
var content = list[i].label || list[i].value;
if (options.highlight) {
content = content.replace(regex, '<b>$1</b>');
}
var item = typeof list[i].href === 'undefined' ? $('<div/>') : $('<a/>').attr('href', list[i].href);
// list
item
.data(list[i])
.addClass('ac-item')
.html(content);
if (typeof list[i].description !== 'undefined') {
item.append(
$('<small/>').html(
options.highlight ? list[i].description.replace(regex, '<b>$1</b>') : list[i].description
)
);
}
shell.append(item);
}
input.trigger('ac:render', [shell[0]]);
return shell;
};
var _createList = function _createList(data) {
// highlighting setup
// escape term for regex - https://github.com/sindresorhus/escape-string-regexp/blob/master/index.js
var escapedTerm = input.val().replace(/[|\\{}()\[\]\^$+*?.]/g, '\\$&');
var regex = new RegExp('(' + escapedTerm + ')', 'ig');
var shell;
if (typeof data.groups === 'undefined') {
shell = _listToHTML(data, regex);
} else {
shell = $('<div/>');
for (var i = 0; i < data.groups.length; i++) {
if (typeof data.groups[i].label !== 'undefined' || i > 0) {
shell.append($('<hr/>', { class: 'ac-section-divider' }));
}
if (typeof data.groups[i].label !== 'undefined') {
shell.append(
$('<header>', {
class: 'ac-section-header',
html: data.groups[i].label
})
);
}
if (typeof data.groups[i].label !== 'undefined' && data.groups[i].items.length > 0) {
shell.append(_listToHTML(data.groups[i].items, regex));
} else if (data.groups[i].length > 0) {
shell.append(_listToHTML(data.groups[i], regex));
}
}
}
element.html(shell);
input.data('ac-length', shell.find('.ac-item').length);
element.find('.ac-item').mousedown(function acItemClick() {
_populate($(this).data(), { mouse: true });
});
_align();
};
var _handleResults = function _handleResults(term, _data) {
// Limit results
var data =
typeof _data.groups === 'undefined'
? _data.slice(0, Math.min(options.maxResults, _data.length))
: _data;
var cid = input.data('ac-id');
cache[cid][term] = data;
if (data.length === 0 || (typeof data.groups !== 'undefined' && data.groups.length === 0)) {
hide();
} else {
_createList(data);
}
};
var _defaultStaticSort = function _defaultStaticSort(a, b) {
return a.match.indexOf(this) - b.match.indexOf(this);
};
var _staticGroups = function _staticGroups(lcterm) {
var matches = [];
function isTermMatch(_item) {
return _item.match.match(lcterm);
}
for (var i = 0; i < options.static.groups.length; i++) {
if (typeof options.static.groups[i].label !== 'undefined') {
var mitems = options.static.groups[i].items.filter(isTermMatch);
if (mitems.length > 0) {
if (typeof options.staticSort === 'function') {
mitems.sort(options.staticSort);
} else {
mitems.sort(_defaultStaticSort.bind(lcterm));
}
matches.push({
label: options.static.groups[i].label,
items: mitems
});
}
} else {
var ms = options.static.groups[i].filter(isTermMatch);
if (ms.length > 0) {
if (typeof options.staticSort === 'function') {
ms.sort(options.staticSort);
} else {
ms.sort(_defaultStaticSort.bind(lcterm));
}
matches.push(ms);
}
}
}
return matches;
};
var search = function search() {
if (xhr) {
xhr.abort();
}
if (input.val().length >= options.minLength) {
element.html('<i class="ac-item loading">' + options.loadingString + '</i>');
_show();
_align();
input.data('ac-selected', -1);
var term = input.val();
// Check cache (only for handler-based setups)
var cid = input.data('ac-id');
if (options.cache && typeof cache[cid][term] !== 'undefined') {
if (cache[cid][term].length === 0) {
hide();
} else {
_createList(cache[cid][term]);
}
// Check for static list
} else if (typeof options.static !== 'undefined') {
var lcterm = term.toLowerCase();
var matches;
if (typeof options.static.groups !== 'undefined') {
matches = { groups: _staticGroups(lcterm) };
} else {
matches = options.static.filter(function staticFilter(_item) {
return _item.match.match(lcterm);
});
if (typeof options.staticSort === 'function') {
matches.sort(options.staticSort);
} else {
matches.sort(_defaultStaticSort.bind(lcterm));
}
}
_handleResults(term, matches);
// Call handler
} else {
options.handler(input, function achandlerCallback(data) {
_handleResults(term, data);
});
}
} else {
hide();
}
};
function preprocessStatic(_item) {
var item = typeof _item === 'string' ? { value: _item } : _item;
item.match = (item.label || item.value).toLowerCase();
return item;
}
var _setup = function _setup(settings) {
var cid = Math.floor(Math.random() * 1000);
input.data('ac-id', cid);
input.data('ac-selected', -1);
input.data('ac-length', 0);
options = $.extend({}, $.fn.autocomplete.defaults, settings);
optionStorage[input.data('ac-id')] = options;
if (options.cache) {
cache[cid] = {};
}
element = $('.autocomplete-results');
if (element.length === 0) {
element = $('<div/>')
.addClass('autocomplete-results ' + options.hidingClass)
.html('<i class="item loading">' + options.loadingString + '</i>');
_align();
$(document.body).append(element);
}
input.blur(function acinputBlur(e) {
if (e.target.acitem) {
setTimeout(hide, 10);
} else {
hide();
}
});
input.click(function acinputClick() {
search();
});
input.focus(function acinputFocus() {
search();
});
input.on('paste', function acinputPaste() {
requestAnimationFrame(search);
});
//fixes the suggestions not appearing on firefox mobile
input.on('input', function acinputInput() {
search();
});
input.keyup(function acinputKeyup(event) {
// Ignore navigation keys
// - Ignore control functions
if (event.ctrlKey || event.which === 17) {
return;
}
// - Function keys (F1 - F15)
if (112 <= event.which && event.which <= 126) {
return;
}
switch (event.which) {
case 9: // tab
case 13: // enter
case 16: // shift
case 20: // caps lock
case 27: // esc
case 33: // page up
case 34: // page down
case 35: // end
case 36: // home
case 37: // arrows
case 38:
case 39:
case 40:
case 45: // insert
case 144: // num lock
case 145: // scroll lock
case 19: // pause/break
return;
}
});
input.keydown(function acinputKeydown(event) {
// - Ignore control functions
if (event.ctrlKey || event.which === 17) {
return;
}
var position = $(this).data('ac-selected');
switch (event.which) {
// arrow keys through items
case 38: // up key
event.preventDefault();
element.find('.ac-item.selected').removeClass('selected');
if (position > -1) {
if (position-- > 0) {
element.find('.ac-item:eq(' + position + ')').addClass('selected');
}
$(this).data('ac-selected', position);
}
break;
case 40: // down key
event.preventDefault();
if (element.hasClass(options.hidingClass)) {
search();
} else if (position < input.data('ac-length') - 1) {
position++;
element.find('.ac-item.selected').removeClass('selected');
element.find('.ac-item:eq(' + position + ')').addClass('selected');
$(this).data('ac-selected', position);
}
break;
// enter to nav or populate
case 9:
case 13:
var selected = element.find('.ac-item.selected');
if (selected.length > 0) {
event.preventDefault();
if (event.which === 13 && selected.attr('href') && options.callback === 'undefined') {
return window.location.assign(selected.attr('href'));
} else {
_populate(selected.data(), { key: true });
}
}
break;
// hide on escape
case 27:
hide();
$(this).data('ac-selected', -1);
break;
}
});
//autcomplete.js 2.0 has a better version of this
//this actually hides the suggestions when the soft keyboard on mobile appears
//window.addEventListener('resize', hide, false);
};
// Setup
if (!input.data('ac-id')) {
if (typeof settings === 'undefined') {
console.error('Autocomplete not initialized, please pass setup parameters.');
}
if (typeof settings.handler === 'undefined' && typeof settings.static === 'undefined') {
console.error('Neither handler function nor static result list provided for autocomplete');
return null;
}
if (typeof settings.static !== 'undefined') {
// Preprocess strings into items
if (typeof settings.static.groups !== 'undefined') {
for (var i = 0; i < settings.static.groups.length; i++) {
if (typeof settings.static.groups[i].label !== 'undefined') {
settings.static.groups[i].items = settings.static.groups[i].items.map(preprocessStatic);
} else {
settings.static.groups[i] = settings.static.groups[i].map(preprocessStatic);
}
}
} else {
settings.static = settings.static.map(preprocessStatic);
}
}
_setup(settings);
}
return {
show: function show() {
_show();
_align();
},
hide: hide,
search: search,
clearCache: function clearCache() {
cache[input.data('ac-id')] = {};
}
};
}.bind(_input)();
}
$.fn.autocomplete = function acJQuery(settings) {
var ac;
this.each(function acJQueryEach() {
ac = Factory(this, settings);
});
return ac;
};
$.fn.autocomplete.defaults = {
cache: true,
hidingClass: 'hidden',
highlight: true,
loadingString: 'Loading...',
maxResults: 20,
minLength: 3,
rtl: false
};
var timer = false;
$.fn.autocomplete.ajax = function acAjax(ops) {
if (timer) {
clearTimeout(timer);
}
if (xhr) {
xhr.abort();
}
timer = setTimeout(function acajaxDelay() {
xhr = $.ajax(ops);
}, 200);
};
})(jQuery);