Repository navigation
Expand file tree
/
Copy pathautocomplete.js
More file actions
298 lines (278 loc) · 7.77 KB
/
Copy pathautocomplete.js
File metadata and controls
298 lines (278 loc) · 7.77 KB
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
/* https://github.com/vufind-org/autocomplete.js (v2.1.3) */
function Autocomplete(_settings) {
const _DEFAULTS = {
delay: 250,
limit: 20,
loadingString: "Loading...",
minInputLength: 3,
rtl: false,
};
if (typeof _settings === "undefined") {
_settings = {};
}
const settings = Object.assign({}, _DEFAULTS, _settings);
let list;
let _currentItems;
let _currentListEls;
let _currentIndex = -1;
function _debounce(func, delay) {
let timeout;
return function debounced() {
const context = this;
const args = [].slice.call(arguments);
clearTimeout(timeout);
timeout = setTimeout(function() {
func.apply(context, args);
}, delay);
};
}
function _align(input) {
const inputBox = input.getBoundingClientRect();
list.style.minWidth = inputBox.width + "px";
list.style.top = inputBox.bottom + window.scrollY + "px";
let anchorRight =
settings.rtl ||
(inputBox.left + list.offsetWidth >=
document.documentElement.offsetWidth &&
inputBox.right - list.offsetWidth > 0);
if (anchorRight) {
const posFromRight =
document.documentElement.offsetWidth - inputBox.right;
list.style.left = "auto"; // fixes width estimation
list.style.right = posFromRight + "px";
} else {
list.style.right = "auto";
list.style.left = inputBox.left + "px";
}
}
let lastInput = false;
function _show(input) {
lastInput = input;
list.style.left = "-100%"; // hide offscreen
list.classList.add("open");
}
function _hide(e) {
if (
typeof e !== "undefined" &&
!!e.relatedTarget &&
e.relatedTarget.hasAttribute("href")
) {
return;
}
list.classList.remove("open");
_currentIndex = -1;
lastInput = false;
}
function _selectItem(item, input) {
if (typeof item._disabled !== "undefined" && item._disabled) {
return;
}
// Broadcast
var event = document.createEvent("CustomEvent");
// CustomEvent: name, canBubble, cancelable, detail
event.initCustomEvent("ac-select", true, true, item);
input.dispatchEvent(event);
// Copy value
if (typeof item === "string" || typeof item === "number") {
input.value = item;
} else if (typeof item.value === "undefined") {
input.value = item.text;
} else {
input.value = item.value;
}
if (typeof item.href !== "undefined") {
window.location.assign(item.href);
}
_hide();
}
function _renderItem(item, input) {
let el = document.createElement("div");
el.classList.add("ac-item");
if (typeof item === "string" || typeof item === "number") {
el.innerHTML = item;
} else if (typeof item._header !== "undefined") {
el.innerHTML = item._header;
el.classList.add("ac-header");
return el;
} else {
el.innerHTML = item.text;
if (typeof item.sub !== "undefined") {
let sub = document.createElement("small");
sub.classList.add("ac-sub");
sub.innerHTML = item.sub;
el.appendChild(sub);
}
if (typeof item._disabled !== "undefined" && item._disabled) {
el.setAttribute("disabled", true);
if (typeof item._disabled !== "boolean") {
el.innerHTML = item._disabled;
}
}
}
el.addEventListener(
"mousedown",
e => {
if (e.which === 1) {
e.preventDefault();
_selectItem(item, input);
} else {
return true;
}
},
false
);
return el;
}
function _searchCallback(items, input) {
// Render
if (items.length > settings.limit) {
items = items.slice(0, settings.limit);
}
const listEls = items.map(item => _renderItem(item, input));
list.innerHTML = "";
listEls.map(el => list.appendChild(el));
// Setup keyboard information
_currentItems = items.slice().filter(item => {
return (
typeof item._header === "undefined" &&
typeof item._disabled === "undefined"
);
});
_currentListEls = listEls.filter(
el =>
!el.classList.contains("ac-header") &&
!el.classList.contains("ac-disabled")
);
_currentIndex = -1;
}
let lastCB;
function _search(handler, input) {
if (input.value.length < settings.minInputLength) {
_hide();
return;
}
let loadingEl = _renderItem({ _header: settings.loadingString });
list.innerHTML = loadingEl.outerHTML;
_show(input);
_align(input);
let thisCB = new Date().getTime();
lastCB = thisCB;
handler(input.value, function callback(items) {
if (thisCB !== lastCB || items === false || items.length === 0) {
_hide();
return;
}
_searchCallback(items, input);
_align(input);
});
}
function _keydown(handler, input, event) {
// - Ignore control functions
if (event.ctrlKey || event.which === 17) {
return;
}
switch (event.which) {
// arrow keys through items
case 38: // up key
event.preventDefault();
if (_currentIndex === -1) {
return;
}
_currentListEls[_currentIndex].classList.remove("is-selected");
_currentIndex -= 1;
if (_currentIndex === -1) {
return;
}
_currentListEls[_currentIndex].classList.add("is-selected");
break;
case 40: // down key
event.preventDefault();
if (lastInput === false) {
_search(handler, input);
return;
}
if (_currentIndex === _currentItems.length - 1) {
return;
}
if (_currentIndex > -1) {
_currentListEls[_currentIndex].classList.remove("is-selected");
}
_currentIndex += 1;
_currentListEls[_currentIndex].classList.add("is-selected");
break;
// enter to nav or populate
case 9:
case 13:
if (_currentIndex === -1) {
return;
}
event.preventDefault();
_selectItem(_currentItems[_currentIndex], input);
break;
// hide on escape
case 27:
_hide();
break;
}
}
return function Factory(input, handler) {
if (!input) {
return false;
}
if (typeof handler === "undefined") {
throw new Error(
"Autocomplete needs handler to return items based on a query: function(query, callback) {}"
);
}
// Create list element
if (typeof list === "undefined") {
list = document.querySelector(".autocomplete-results");
if (!list) {
list = document.createElement("div");
list.classList.add("autocomplete-results");
document.body.appendChild(list);
window.addEventListener(
"resize",
function acresize() {
if (lastInput === false) {
return;
}
_align(lastInput);
},
false
);
}
}
// Activation / De-activation
input.setAttribute("autocomplete", "off");
input.addEventListener("focus", _ => _search(handler, input), false);
input.addEventListener("click", _ => _search(handler, input), false);
input.addEventListener("blur", _hide, false);
// Input typing
const debounceSearch = _debounce(_search, settings.delay);
input.addEventListener(
"input",
event => {
if (
event.inputType === "insertFromPaste" ||
event.inputType === "insertFromDrop"
) {
_search(handler, input);
} else {
debounceSearch(handler, input);
}
},
false
);
// Checking control characters
input.addEventListener(
"keydown",
event => _keydown(handler, input, event),
false
);
return input;
};
}
if (typeof module !== "undefined") {
module.exports = Autocomplete;
}