forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
380 lines (338 loc) · 8.74 KB
/
index.js
File metadata and controls
380 lines (338 loc) · 8.74 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
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
import "./style.scss";
/**
* @typedef {Object} HintObj
* @property {string} value
* @property {string} text
*/
/**
* @typedef {HintObj|string} Hint
*/
/**
* @typedef {Object} HintModification
* @property {(hint:Hint, index:number)=>void} add
* @property {(hint:Hint)=>void} remove
* @property {(index:number)=>void} removeIndex
*/
/**
* @typedef {(setHints:(hints:Array<Hint>)=>void, modification: HintModification) => void} HintCallback
*/
/**
* Generate a list of hints for an input field
* @param {HTMLInputElement} $input Input field
* @param {Array<Hint>|HintCallback} hints Hints or a callback to generate hints
* @param {(value: string) => void} onSelect Callback to call when a hint is selected
* @returns {{getSelected: ()=>HTMLLIElement, container: HTMLUListElement}}
*/
export default function inputhints($input, hints, onSelect) {
/**@type {HTMLUListElement} */
const $ul = <Ul />;
const LIMIT = 100;
let preventUpdate = false;
let updateUlTimeout;
let pages = 0;
let currentHints = [];
$input.addEventListener("focus", onfocus);
if (typeof hints === "function") {
const cb = hints;
hints = [];
$ul.content = [<Hint hint={{ value: "", text: strings["loading..."] }} />];
cb(setHints, hintModification());
} else {
setHints(hints);
}
/**
* Retain the focus on the input field
*/
function handleMouseDown() {
preventUpdate = true;
}
function handleMouseUp() {
$input.focus();
preventUpdate = false;
}
/**
* Handle click event
* @param {MouseEvent} e Event
*/
function handleClick(e) {
const $el = e.target;
const action = $el.getAttribute("action");
if (action !== "hint") return;
const value = $el.getAttribute("value");
if (!value) {
onblur();
return;
}
$input.value = $el.textContent;
if (onSelect) onSelect(value);
preventUpdate = false;
onblur();
}
/**
* Handle keypress event
* @param {KeyboardEvent} e Event
*/
function handleKeypress(e) {
if (e.key !== "Enter") return;
e.preventDefault();
e.stopPropagation();
const activeHint = $ul.get(".active");
if (!activeHint) return;
const value = activeHint.getAttribute("value");
if (onSelect) onSelect(value);
else $input.value = value;
}
/**
* Handle keydown event
* @param {KeyboardEvent} e Event
*/
function handleKeydown(e) {
const code = e.key;
if (code === "ArrowUp" || code === "ArrowDown") {
e.preventDefault();
e.stopPropagation();
}
updateHintFocus(code);
}
/**
* Moves the active hint up or down
* @param {"ArrowDown" | "ArrowUp"} key Direction to move
*/
function updateHintFocus(key) {
let nextHint;
let activeHint = $ul.get(".active");
if (!activeHint) activeHint = $ul.firstChild;
if (key === "ArrowDown") {
nextHint = activeHint.nextElementSibling;
if (!nextHint) nextHint = $ul.firstElementChild;
} else if (key === "ArrowUp") {
nextHint = activeHint.previousElementSibling;
if (!nextHint) nextHint = $ul.lastElementChild;
}
if (nextHint) {
activeHint.classList.remove("active");
nextHint.classList.add("active");
nextHint.scrollIntoView();
}
}
/**
* @this {HTMLInputElement}
*/
function oninput() {
const { value: toTest } = this;
const matched = [];
const regexp = new RegExp(toTest, "i");
for (const hint of hints) {
const { value, text } = hint;
if (regexp.test(value) || regexp.test(text)) {
matched.push(hint);
}
}
updateUl(matched);
}
function onfocus() {
if (preventUpdate) return;
$input.addEventListener("keypress", handleKeypress);
$input.addEventListener("keydown", handleKeydown);
$input.addEventListener("blur", onblur);
$input.addEventListener("input", oninput);
window.addEventListener("resize", position);
ulAddEventListeners();
app.append($ul);
position();
}
/**
* Event listener for blur
* @returns
*/
function onblur() {
if (preventUpdate) return;
clearTimeout(updateUlTimeout);
$input.removeEventListener("keypress", handleKeypress);
$input.removeEventListener("keydown", handleKeydown);
$input.removeEventListener("blur", onblur);
$input.removeEventListener("input", oninput);
window.removeEventListener("resize", position);
ulRemoveEventListeners();
$ul.remove();
}
/**
* Update the position of the hint list
* @param {boolean} append Append the list to the body or not
*/
function position() {
const activeHint = $ul.get(".active");
const { firstElementChild } = $ul;
if (!activeHint && firstElementChild)
firstElementChild.classList.add("active");
const client = $input.getBoundingClientRect();
const inputTop = client.top - 5;
const inputBottom = client.bottom + 5;
const inputLeft = client.left;
const bottomHeight = window.innerHeight - inputBottom;
const mid = window.innerHeight / 2;
if (bottomHeight >= mid) {
$ul.classList.remove("bottom");
$ul.style.top = `${inputBottom}px`;
$ul.style.bottom = "auto";
} else {
$ul.classList.add("bottom");
$ul.style.top = "auto";
$ul.style.bottom = `${inputTop}px`;
}
$ul.style.left = `${inputLeft}px`;
$ul.style.width = `${client.width}px`;
}
/**
* Set hint items
* @param {Array<Hint>} list Hint items
*/
function setHints(list) {
if (Array.isArray(list)) {
hints = list;
} else {
hints = [];
}
updateUl(hints);
$ul.classList.remove("loading");
}
function hintModification() {
return {
add(item, index) {
if (index) {
hints.splice(index, 0, item);
const child = $ul.children[index];
if (child) {
$ul.insertBefore(child, $ul.children[index]);
}
return;
}
hints.push(item);
},
remove(item) {
const index = hints.indexOf(item);
if (index > -1) {
hints.splice(index, 1);
}
},
removeIndex(index) {
hints.splice(index, 1);
},
};
}
function ulAddEventListeners() {
window.addEventListener("resize", position);
$ul.addEventListener("click", handleClick);
$ul.addEventListener("mousedown", handleMouseDown);
$ul.addEventListener("mouseup", handleMouseUp);
$ul.addEventListener("touchstart", handleMouseDown);
$ul.addEventListener("touchend", handleMouseUp);
$ul.addEventListener("scroll", updatePage);
}
function ulRemoveEventListeners() {
window.removeEventListener("resize", position);
$ul.removeEventListener("click", handleClick);
$ul.removeEventListener("mousedown", handleMouseDown);
$ul.removeEventListener("mouseup", handleMouseUp);
$ul.removeEventListener("touchstart", handleMouseDown);
$ul.removeEventListener("touchend", handleMouseUp);
$ul.removeEventListener("scroll", updatePage);
}
function updatePage() {
const offset = (pages + 1) * LIMIT;
const hasMore = offset < currentHints.length;
// if the scroll is at the bottom
if ($ul.scrollTop + $ul.clientHeight >= $ul.scrollHeight && hasMore) {
pages++;
updateUlNow(currentHints, pages);
}
}
/**
* First time updates the hint instantly, then debounce
* @param {Array<HintObj>} hints
*/
function updateUl(hints) {
updateUlNow(hints);
updateUl = updateUlDebounce;
}
/**
* Update the hint list after a delay
* @param {Array<HintObj>} hints
*/
function updateUlDebounce(hints) {
clearTimeout(updateUlTimeout);
updateUlTimeout = setTimeout(updateUlNow, 300, hints);
}
/**
* Update the hint list instantly
* @param {Array<HintObj>} hints
* @param {number} page
*/
function updateUlNow(hints, page = 0) {
// render only first 500 hints
currentHints = hints;
const offset = page * LIMIT;
const end = offset + LIMIT;
const list = hints.slice(offset, end);
let scrollTop = $ul.scrollTop;
//if (!list.length) return;
$ul.remove();
if (!hints.length) {
$ul.content = [<Hint hint={{ value: "", text: "No matches found" }} />];
} else if (!list.length) {
// No more hints to load
return;
} else {
if (!page) {
scrollTop = 0;
$ul.content = list.map((hint) => <Hint hint={hint} />);
} else {
$ul.append(...list.map((hint) => <Hint hint={hint} />));
}
}
app.append($ul);
$ul.scrollTop = scrollTop;
position(); // Update the position of the new list
}
return {
getSelected() {
$ul.get(".active");
},
get container() {
return $ul;
},
};
}
/**
* Create a hint item
* @param {object} param0 Hint item
* @param {HintObj} param0.hint Hint item
* @returns {HTMLLIElement}
*/
function Hint({ hint }) {
let value = "";
let text = "";
if (typeof hint === "string") {
value = hint;
text = hint;
} else {
value = hint.value;
text = hint.text;
}
return <li attr-action="hint" attr-value={value} innerHTML={text} />;
}
/**
* Create a hint list
* @param {object} param0 Attributes
* @param {Array<Hint>} param0.hints Hint items
* @returns {HTMLUListElement}
*/
function Ul({ hints = [] }) {
return (
<ul id="hints" className="scroll">
{hints.map((hint) => (
<Hint hint={hint} />
))}
</ul>
);
}