-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.js
919 lines (804 loc) · 24.5 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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/**
* Bootstrap 5 autocomplete
* MIT License
* Copyright (c) 2022 Thomas Portelange
* https://github.com/lekoala/bootstrap5-autocomplete/blob/master/LICENSE
*
*/
// #region config
/**
* @callback RenderCallback
* @param {Object} item
* @param {String} label
* @param {Autocomplete} inst
* @returns {string}
*/
/**
* @callback ItemCallback
* @param {Object} item
* @param {Autocomplete} inst
* @returns {void}
*/
/**
* @callback ServerCallback
* @param {Response} response
* @returns {Promise}
*/
/**
* @typedef Config
* @property {Boolean} showAllSuggestions Show all suggestions even if they don't match
* @property {Number} suggestionsThreshold Number of chars required to show suggestions
* @property {Number} maximumItems Maximum number of items to display
* @property {Boolean} autoselectFirst Always select the first item
* @property {Boolean} updateOnSelect Update input value on selection (doesn't play nice with autoselectFirst)
* @property {Boolean} highlightTyped Highlight matched part of the label
* @property {Boolean} fullWidth Match the width on the input field
* @property {Boolean} fixed Use fixed positioning (solve overflow issues)
* @property {Array} activeClasses By default: ["bg-primary", "text-white"]
* @property {String} labelField Key for the label
* @property {String} valueField Key for the value
* @property {String} queryParam Key for the query parameter for server
* @property {Array|Object} items An array of label/value objects or an object with key/values
* @property {Function} source A function that provides the list of items
* @property {String} datalist The id of the source datalist
* @property {String} server Endpoint for data provider
* @property {String} serverMethod HTTP request method for data provider, default is GET
* @property {String|Object} serverParams Parameters to pass along to the server
* @property {Object} fetchOptions Any other fetch options (https://developer.mozilla.org/en-US/docs/Web/API/fetch#syntax)
* @property {Boolean} liveServer Should the endpoint be called each time on input
* @property {Boolean} noCache Prevent caching by appending a timestamp
* @property {Number} debounceTime Debounce time for live server
* @property {String} notFoundMessage Display a no suggestions found message. Leave empty to disable
* @property {RenderCallback} onRenderItem Callback function that returns the label
* @property {ItemCallback} onSelectItem Callback function to call on selection
* @property {ServerCallback} onServerResponse Callback function to process server response. Must return a Promise
*/
/**
* @type {Config}
*/
const DEFAULTS = {
showAllSuggestions: false,
suggestionsThreshold: 1,
maximumItems: 0,
autoselectFirst: true,
updateOnSelect: false,
highlightTyped: false,
fullWidth: false,
fixed: false,
activeClasses: ["bg-primary", "text-white"],
labelField: "label",
valueField: "value",
queryParam: "query",
items: [],
source: null,
datalist: "",
server: "",
serverMethod: "GET",
serverParams: {},
fetchOptions: {},
liveServer: false,
noCache: true,
debounceTime: 300,
notFoundMessage: "",
onRenderItem: (item, label, inst) => {
return label;
},
onSelectItem: (item, inst) => {},
onServerResponse: (response) => {
return response.json();
},
};
// #endregion
// #region constants
const LOADING_CLASS = "is-loading";
const ACTIVE_CLASS = "is-active";
const SHOW_CLASS = "show";
const NEXT = "next";
const PREV = "prev";
const INSTANCE_MAP = new WeakMap();
let counter = 0;
// #endregion
// #region functions
/**
* @param {Function} func
* @param {number} timeout
* @returns {Function}
*/
function debounce(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
//@ts-ignore
func.apply(this, args);
}, timeout);
};
}
/**
* @param {String} str
* @returns {String}
*/
function removeDiacritics(str) {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
/**
* @param {HTMLElement} el
* @param {HTMLElement} newEl
* @returns {HTMLElement}
*/
function insertAfter(el, newEl) {
return el.parentNode.insertBefore(newEl, el.nextSibling);
}
// #endregion
class Autocomplete {
/**
* @param {HTMLInputElement} el
* @param {Config|Object} config
*/
constructor(el, config = {}) {
if (!(el instanceof HTMLElement)) {
console.error("Invalid element", el);
return;
}
INSTANCE_MAP.set(el, this);
counter++;
this._searchInput = el;
this._configure(config);
// Private vars
this._preventInput = false;
this._keyboardNavigation = false;
this._searchFunc = debounce(() => {
this._loadFromServer(true);
}, this._config.debounceTime);
// Create html
this._configureSearchInput();
this._configureDropElement();
if (this._config.fixed) {
document.addEventListener("scroll", this, true);
window.addEventListener("resize", this);
}
// Add listeners (remove then on dispose()). See handleEvent.
this._searchInput.addEventListener("focus", this);
this._searchInput.addEventListener("blur", this);
this._searchInput.addEventListener("input", this);
this._searchInput.addEventListener("keydown", this);
this._dropElement.addEventListener("mousemove", this);
this._fetchData();
}
// #region Core
/**
* Attach to all elements matched by the selector
* @param {string} selector
* @param {Config|Object} config
*/
static init(selector = "input.autocomplete", config = {}) {
/**
* @type {NodeListOf<HTMLInputElement>}
*/
const nodes = document.querySelectorAll(selector);
nodes.forEach((el) => {
this.getOrCreateInstance(el, config);
});
}
/**
* @param {HTMLInputElement} el
*/
static getInstance(el) {
return INSTANCE_MAP.has(el) ? INSTANCE_MAP.get(el) : null;
}
/**
* @param {HTMLInputElement} el
* @param {Object} config
*/
static getOrCreateInstance(el, config = {}) {
return this.getInstance(el) || new this(el, config);
}
dispose() {
this._searchInput.removeEventListener("focus", this);
this._searchInput.removeEventListener("blur", this);
this._searchInput.removeEventListener("input", this);
this._searchInput.removeEventListener("keydown", this);
this._dropElement.removeEventListener("mousemove", this);
if (this._config.fixed) {
document.removeEventListener("scroll", this, true);
window.removeEventListener("resize", this);
}
this._dropElement.parentElement.removeChild(this._dropElement);
INSTANCE_MAP.delete(this._searchInput);
}
/**
* @link https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4#handling-events
* @param {Event} event
*/
handleEvent(event) {
this[`on${event.type}`](event);
}
/**
* @param {Config|Object} config
*/
_configure(config = {}) {
this._config = Object.assign({}, DEFAULTS);
// Handle options, using arguments first and data attr as override
const o = { ...config, ...this._searchInput.dataset };
// Allow 1/0, true/false as strings
const parseBool = (value) => ["true", "false", "1", "0", true, false].includes(value) && !!JSON.parse(value);
// Typecast provided options based on defaults types
for (const [key, defaultValue] of Object.entries(DEFAULTS)) {
// Check for undefined keys
if (o[key] === void 0) {
continue;
}
const value = o[key];
switch (typeof defaultValue) {
case "number":
this._config[key] = parseInt(value);
break;
case "boolean":
this._config[key] = parseBool(value);
break;
case "string":
this._config[key] = value.toString();
break;
case "object":
// Arrays have a type object in js
if (Array.isArray(defaultValue)) {
if (typeof value === "string") {
const separator = value.includes("|") ? "|" : ",";
this._config[key] = value.split(separator);
} else {
this._config[key] = value;
}
} else {
this._config[key] = typeof value === "string" ? JSON.parse(value) : value;
}
break;
case "function":
this._config[key] = typeof value === "string" ? window[value] : value;
break;
default:
this._config[key] = value;
break;
}
}
}
// #endregion
// #region Html
_configureSearchInput() {
this._searchInput.autocomplete = "off";
this._searchInput.spellcheck = false;
// @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-autocomplete
this._searchInput.ariaAutoComplete = "list";
// @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-expanded
// use the aria-expanded state on the element with role combobox to communicate that the list is displayed.
this._searchInput.ariaExpanded = "false";
// include aria-haspopup matching the role of the element that contains the collection of suggested values.
this._searchInput.ariaHasPopup = "menu";
this._searchInput.setAttribute("role", "combobox");
}
_configureDropElement() {
this._dropElement = document.createElement("ul");
this._dropElement.setAttribute("id", "ac-menu-" + counter);
this._dropElement.setAttribute("role", "menu");
this._dropElement.classList.add(...["dropdown-menu", "autocomplete-menu", "p-0"]);
this._dropElement.style.maxHeight = "280px";
if (!this._config.fullWidth) {
this._dropElement.style.maxWidth = "360px";
}
if (this._config.fixed) {
this._dropElement.style.position = "fixed";
}
this._dropElement.style.overflowY = "auto";
// Prevent scrolling the menu from scrolling the page
// @link https://developer.mozilla.org/en-US/docs/Web/CSS/overscroll-behavior
this._dropElement.style.overscrollBehavior = "contain";
this._dropElement.style.textAlign = "unset"; // otherwise RTL is not good
insertAfter(this._searchInput, this._dropElement);
// include aria-controls with the value of the id of the suggested list of values.
this._searchInput.setAttribute("aria-controls", this._dropElement.getAttribute("id"));
}
// #endregion
// #region Events
oninput(e) {
if (this._preventInput) {
return;
}
this.showOrSearch();
}
onblur(e) {
this.hideSuggestions();
}
onfocus(e) {
this.showOrSearch();
}
/**
* keypress doesn't send arrow keys, so we use keydown
* @param {KeyboardEvent} e
*/
onkeydown(e) {
const key = e.keyCode || e.key;
switch (key) {
case 13:
case "Enter":
if (this.isDropdownVisible()) {
e.preventDefault();
const selection = this.getSelection();
if (selection) {
selection.click();
}
}
break;
case 38:
case "ArrowUp":
e.preventDefault();
this._keyboardNavigation = true;
this._moveSelection(PREV);
break;
case 40:
case "ArrowDown":
e.preventDefault();
this._keyboardNavigation = true;
if (this.isDropdownVisible()) {
this._moveSelection(NEXT);
} else {
// show menu regardless of input length
this.showOrSearch(false);
}
break;
case 27:
case "Escape":
if (this.isDropdownVisible()) {
this._searchInput.focus();
this.hideSuggestions();
}
break;
}
}
onmousemove(e) {
// Moving the mouse means no longer using keyboard
this._keyboardNavigation = false;
}
onscroll(e) {
this._positionMenu();
}
onresize(e) {
this._positionMenu();
}
// #endregion
// #region Api
/**
* @param {String} k
* @returns {Config}
*/
getConfig(k = null) {
if (k !== null) {
return this._config[k];
}
return this._config;
}
/**
* @param {String} k
* @param {*} v
*/
setConfig(k, v) {
this._config[k] = v;
}
setData(src) {
this._items = {};
this._addItems(src);
}
enable() {
this._searchInput.setAttribute("disabled", "");
}
disable() {
this._searchInput.removeAttribute("disabled");
}
/**
* @returns {boolean}
*/
isDisabled() {
return this._searchInput.hasAttribute("disabled") || this._searchInput.disabled || this._searchInput.hasAttribute("readonly");
}
/**
* @returns {boolean}
*/
isDropdownVisible() {
return this._dropElement.classList.contains(SHOW_CLASS);
}
// #endregion
// #region Selection management
/**
* @returns {HTMLElement}
*/
getSelection() {
return this._dropElement.querySelector("a." + ACTIVE_CLASS);
}
removeSelection() {
const selection = this.getSelection();
if (selection) {
selection.classList.remove(...this._activeClasses());
}
}
/**
* @returns {Array}
*/
_activeClasses() {
return [...this._config.activeClasses, ...[ACTIVE_CLASS]];
}
/**
* @param {String} dir
* @returns {HTMLElement}
*/
_moveSelection(dir = NEXT) {
const active = this.getSelection();
/**
* @type {*}
*/
let sel = null;
// select first li
if (!active) {
sel = this._dropElement.firstChild;
} else {
const sibling = dir === NEXT ? "nextSibling" : "previousSibling";
// Iterate over visible li
sel = active.parentNode;
do {
sel = sel[sibling];
} while (sel && sel.style.display == "none");
// We have a new selection
if (sel) {
// Change classes
active.classList.remove(...this._activeClasses());
// Scroll if necessary
if (dir === PREV) {
// Don't use scrollIntoView as it scrolls the whole window
sel.parentNode.scrollTop = sel.offsetTop - sel.parentNode.offsetTop;
} else {
// This is the equivalent of scrollIntoView(false) but only for parent node
if (sel.offsetTop > sel.parentNode.offsetHeight - sel.offsetHeight) {
sel.parentNode.scrollTop += sel.offsetHeight;
}
}
} else if (active) {
sel = active.parentElement;
}
}
if (sel) {
const a = sel.querySelector("a");
a.classList.add(...this._activeClasses());
this._searchInput.setAttribute("aria-activedescendant", a.getAttribute("id"));
if (this._config.updateOnSelect) {
this._searchInput.value = a.dataset.label;
}
} else {
this._searchInput.setAttribute("aria-activedescendant", "");
}
return sel;
}
// #endregion
// #region Implementation
/**
* Do we have enough input to show suggestions ?
* @returns {Boolean}
*/
_shouldShow() {
if (this.isDisabled()) {
return false;
}
return this._searchInput.value.length >= this._config.suggestionsThreshold;
}
/**
* Show suggestions or load them
* @param {Boolean} check
*/
showOrSearch(check = true) {
if (check && !this._shouldShow()) {
this.hideSuggestions();
return;
}
if (this._config.liveServer) {
this._searchFunc();
} else if (this._config.source) {
this._config.source(this._searchInput.value, (items) => {
this.setData(items);
this._showSuggestions();
});
} else {
this._showSuggestions();
}
}
/**
* @param {String} lookup
* @param {Object} item
* @returns {HTMLElement}
*/
_createItem(lookup, item) {
let label = item.label;
if (this._config.highlightTyped) {
const idx = removeDiacritics(label).toLowerCase().indexOf(lookup);
label =
label.substring(0, idx) +
`<mark>${label.substring(idx, idx + lookup.length)}</mark>` +
label.substring(idx + lookup.length, label.length);
}
label = this._config.onRenderItem(item, label, this);
const newChild = document.createElement("li");
newChild.setAttribute("role", "presentation");
const newChildLink = document.createElement("a");
newChild.append(newChildLink);
newChildLink.setAttribute("id", this._dropElement.getAttribute("id") + "-" + this._dropElement.children.length);
newChildLink.classList.add(...["dropdown-item", "text-truncate"]);
newChildLink.setAttribute("data-value", item.value);
newChildLink.setAttribute("data-label", item.label);
// Behave like a datalist (tab doesn't allow item selection)
// @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist
newChildLink.setAttribute("tabindex", "-1");
newChildLink.setAttribute("role", "menuitem");
newChildLink.setAttribute("href", "#");
newChildLink.innerHTML = label;
if (item.data) {
for (const [key, value] of Object.entries(item.data)) {
newChildLink.dataset[key] = value;
}
}
// Hover sets active item
newChildLink.addEventListener("mouseenter", (event) => {
// Don't trigger enter if using arrows
if (this._keyboardNavigation) {
return;
}
this.removeSelection();
newChild.querySelector("a").classList.add(...this._activeClasses());
});
// Prevent searchInput losing focus and close the menu
newChildLink.addEventListener("mousedown", (event) => {
event.preventDefault();
});
// Apply value
newChildLink.addEventListener("click", (event) => {
event.preventDefault();
// Prevent input otherwise it might trigger autocomplete due to value change
this._preventInput = true;
this._searchInput.value = item.label;
this._config.onSelectItem(item, this);
this.hideSuggestions();
this._preventInput = false;
});
return newChild;
}
/**
* Show drop menu with suggestions
*/
_showSuggestions() {
const lookup = removeDiacritics(this._searchInput.value).toLowerCase();
this._dropElement.innerHTML = "";
const keys = Object.keys(this._items);
let count = 0;
let firstItem = null;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const entry = this._items[key];
const text = removeDiacritics(entry.label).toLowerCase();
const isMatched = lookup.length > 0 ? text.indexOf(lookup) >= 0 : true;
if (this._config.showAllSuggestions || isMatched) {
count++;
const newItem = this._createItem(lookup, entry);
if (!firstItem) {
firstItem = newItem;
}
this._dropElement.appendChild(newItem);
if (this._config.maximumItems > 0 && count >= this._config.maximumItems) {
break;
}
}
}
if (firstItem && this._config.autoselectFirst) {
this._moveSelection(NEXT);
}
if (count === 0) {
if (this._config.notFoundMessage) {
const newChild = document.createElement("li");
newChild.setAttribute("role", "presentation");
newChild.innerHTML = `<span class="dropdown-item">${this._config.notFoundMessage}</span>`;
this._dropElement.appendChild(newChild);
this._showDropdown();
} else {
// Remove dropdown if not found
this.hideSuggestions();
}
} else {
// Or show it if necessary
this._showDropdown();
}
}
/**
* Show and position dropdown
*/
_showDropdown() {
this._dropElement.classList.add(SHOW_CLASS);
this._searchInput.ariaExpanded = "true";
this._positionMenu();
}
/**
* Show or hide suggestions
* @param {Boolean} check
*/
toggleSuggestions(check = true) {
if (this._dropElement.classList.contains(SHOW_CLASS)) {
this.hideSuggestions();
} else {
this.showOrSearch(check);
}
}
/**
* Hide the dropdown menu
*/
hideSuggestions() {
this._dropElement.classList.remove(SHOW_CLASS);
this._searchInput.ariaExpanded = "false";
this.removeSelection();
}
/**
* @returns {HTMLInputElement}
*/
getInput() {
return this._searchInput;
}
/**
* @returns {HTMLUListElement}
*/
getDropMenu() {
return this._dropElement;
}
/**
* Checks if parent is fixed for boundary checks
* @returns {Boolean}
*/
_hasFixedParent() {
let parent = this._searchInput.parentElement;
while (parent && parent instanceof HTMLElement) {
if (parent.style.position === "fixed") {
return true;
}
parent = parent.parentElement;
}
return false;
}
/**
* Position the dropdown menu
*/
_positionMenu() {
const styles = window.getComputedStyle(this._searchInput);
const bounds = this._searchInput.getBoundingClientRect();
const fixedParent = this._hasFixedParent();
const isRTL = styles.direction === "rtl";
// Don't position left if not fixed since it may not work in all situations
// due to offsetParent margin or in tables
let left = null;
let top = null;
if (this._config.fixed) {
left = bounds.x;
top = bounds.y + bounds.height;
// Align end
if (isRTL && !this._config.fullWidth) {
left -= this._dropElement.offsetWidth - bounds.width;
}
}
// Reset any height overflow adjustement
this._dropElement.style.transform = "unset";
// Use full holder width
if (this._config.fullWidth) {
this._dropElement.style.width = this._searchInput.offsetWidth + "px";
}
// Position element
if (left !== null) {
this._dropElement.style.left = left + "px";
}
if (top !== null) {
this._dropElement.style.top = top + "px";
}
// Overflow height
const dropBounds = this._dropElement.getBoundingClientRect();
const h = fixedParent ? window.innerHeight : document.body.offsetHeight;
const vdiff = h - 1 - (dropBounds.y + dropBounds.height);
// We display above input if we have more space there
if (vdiff < 0 && bounds.y > h / 2) {
this._dropElement.style.transform = "translateY(calc(-100% - " + this._searchInput.offsetHeight + "px))";
}
}
_fetchData() {
this._items = {};
// From an array of items or an object
this._addItems(this._config.items);
// From a datalist
if (this._config.datalist) {
const datalist = document.querySelector(`#${this._config.datalist}`);
if (datalist) {
const items = Array.from(datalist.children).map((o) => {
const value = o.getAttribute("value") ?? o.innerHTML.toLowerCase();
const label = o.innerHTML;
return {
value: value,
label: label,
};
});
this._addItems(items);
}
}
// From an external source
if (this._config.server && !this._config.liveServer) {
this._loadFromServer();
}
}
_addItems(src) {
const keys = Object.keys(src);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const entry = src[key];
const item = typeof entry === "string" ? {} : entry;
// Normalize entry
item.label = entry[this._config.labelField] ?? entry;
item.value = entry[this._config.valueField] ?? key;
this._items[item.value] = item;
}
}
/**
* @param {boolean} show
*/
_loadFromServer(show = false) {
if (this._abortController) {
this._abortController.abort();
}
this._abortController = new AbortController();
const params = Object.assign({}, this._config.serverParams);
// Pass current value
params[this._config.queryParam] = this._searchInput.value;
// Prevent caching
if (this._config.noCache) {
params.t = Date.now();
}
// We have a related field
if (params.related) {
/**
* @type {HTMLInputElement}
*/
//@ts-ignore
const input = document.getElementById(params.related);
if (input) {
params.related = input.value;
}
}
const urlParams = new URLSearchParams(params);
let url = this._config.server;
let fetchOptions = Object.assign(this._config.fetchOptions, {
method: this._config.serverMethod || "GET",
signal: this._abortController.signal,
});
if (fetchOptions.method === "POST") {
fetchOptions.body = urlParams;
} else {
url += "?" + urlParams.toString();
}
this._searchInput.classList.add(LOADING_CLASS);
fetch(url, fetchOptions)
.then((r) => this._config.onServerResponse(r))
.then((suggestions) => {
const data = suggestions.data || suggestions;
this.setData(data);
this._abortController = null;
if (show) {
this._showSuggestions();
}
})
.catch((e) => {
if (e.name === "AbortError") {
return;
}
console.error(e);
})
.finally((e) => {
this._searchInput.classList.remove(LOADING_CLASS);
});
}
// #endregion
}
export default Autocomplete;