@avlcodemonkey/nilla-components 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/css/alert.css +48 -0
- package/css/autocomplete.css +38 -0
- package/css/dialog.css +61 -0
- package/css/file-to-textarea.css +8 -0
- package/css/html-editor.css +172 -0
- package/css/json.css +44 -0
- package/css/list.css +37 -0
- package/css/nav-toggle.css +92 -0
- package/css/nilla.css +16 -0
- package/css/pjax.css +58 -0
- package/css/slider-input.css +16 -0
- package/css/table.css +99 -0
- package/css/tokens.css +44 -0
- package/css/unsaved-changes.css +10 -0
- package/css/vendor/wysi.css +406 -0
- package/docs/llms.txt +319 -0
- package/package.json +121 -5
- package/src/components/Alert.js +24 -0
- package/src/components/Autocomplete.js +127 -0
- package/src/components/BaseComponent.js +78 -0
- package/src/components/BaseDialog.js +151 -0
- package/src/components/ConfirmDialog.js +34 -0
- package/src/components/CopyToClipboard.js +66 -0
- package/src/components/DateFormatter.js +51 -0
- package/src/components/DateTimeInput.js +63 -0
- package/src/components/DisplayToggle.js +53 -0
- package/src/components/FileToTextarea.js +81 -0
- package/src/components/HtmlEditor.js +309 -0
- package/src/components/InfoDialog.js +38 -0
- package/src/components/JsonFormatter.js +23 -0
- package/src/components/List.js +237 -0
- package/src/components/ListSortable.js +8 -0
- package/src/components/NavToggle.js +119 -0
- package/src/components/PJax.js +888 -0
- package/src/components/Share.js +41 -0
- package/src/components/SliderInput.js +26 -0
- package/src/components/Table.js +758 -0
- package/src/components/TimeOffsetSelect.js +29 -0
- package/src/components/UnsavedChanges.js +187 -0
- package/src/constants/Fetch.js +7 -0
- package/src/constants/HttpHeaders.js +19 -0
- package/src/constants/HttpMethods.js +14 -0
- package/src/constants/SortDirection.js +11 -0
- package/src/constants/TableElements.js +25 -0
- package/src/constants/TableSettings.js +14 -0
- package/src/errors/FetchError.js +7 -0
- package/src/events/ListReorderEvent.js +19 -0
- package/src/events/PJaxErrorEvent.js +38 -0
- package/src/events/PJaxNavigatingEvent.js +46 -0
- package/src/index.js +26 -0
- package/src/locales/en.json +55 -0
- package/src/locales/es.js +9 -0
- package/src/locales/es.json +55 -0
- package/src/models/TableSort.js +20 -0
- package/src/utils/ajaxHeaders.js +10 -0
- package/src/utils/announce.js +40 -0
- package/src/utils/defineElement.js +13 -0
- package/src/utils/editor/blockTools.js +256 -0
- package/src/utils/editor/elements.js +18 -0
- package/src/utils/editor/input.js +350 -0
- package/src/utils/editor/selection.js +125 -0
- package/src/utils/editor/tableTools.js +497 -0
- package/src/utils/escape.js +20 -0
- package/src/utils/fetchJsonArray.js +29 -0
- package/src/utils/filterArray.js +18 -0
- package/src/utils/formActionUrl.js +10 -0
- package/src/utils/formData.js +118 -0
- package/src/utils/index.js +18 -0
- package/src/utils/indexedCompare.js +18 -0
- package/src/utils/isSameOrigin.js +12 -0
- package/src/utils/locale.js +36 -0
- package/src/utils/notifyChange.js +10 -0
- package/src/utils/numberSetting.js +14 -0
- package/src/utils/pageLanguage.js +13 -0
- package/src/utils/parseUtcDate.js +22 -0
- package/src/utils/randomId.js +17 -0
- package/src/utils/registerTabTrap.js +60 -0
- package/src/utils/renderTemplate.js +44 -0
- package/src/utils/requestTimeout.js +12 -0
- package/src/utils/response.js +28 -0
- package/src/utils/sortedCompare.js +67 -0
- package/src/utils/storage.js +40 -0
- package/src/vendor/autocomplete.js +515 -0
- package/src/vendor/formatDate.js +236 -0
- package/src/vendor/licenses/autocomplete.LICENSE.md +21 -0
- package/src/vendor/licenses/fecha.LICENSE.md +21 -0
- package/src/vendor/licenses/pretty-print-json.LICENSE.md +21 -0
- package/src/vendor/licenses/wysi.LICENSE.md +21 -0
- package/src/vendor/prettyPrintJson.js +108 -0
- package/src/vendor/wyzi.js +2024 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Autocomplete function based on
|
|
3
|
+
* autocomplete ~~ https://github.com/denis-taran/autocomplete ~~ MIT License
|
|
4
|
+
*/
|
|
5
|
+
import announce from '../utils/announce.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Generate a very complex textual ID that greatly reduces the chance of a collision with another ID or text.
|
|
9
|
+
* @returns {string} A unique identifier.
|
|
10
|
+
*/
|
|
11
|
+
function uid() {
|
|
12
|
+
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Initializes autocomplete functionality on a given input element.
|
|
17
|
+
* @param {object} settings - Configuration options for autocomplete.
|
|
18
|
+
* @returns {{ destroy: () => void, fetch: () => void }} Object with destroy and fetch methods.
|
|
19
|
+
*/
|
|
20
|
+
const autocomplete = (settings) => {
|
|
21
|
+
const doc = document;
|
|
22
|
+
const container = settings.container || doc.createElement('div');
|
|
23
|
+
const preventSubmit = settings.preventSubmit || 0;
|
|
24
|
+
container.id = container.id || 'autocomplete-' + uid();
|
|
25
|
+
const containerStyle = container.style;
|
|
26
|
+
const debounceWaitMs = settings.debounceWaitMs || 0;
|
|
27
|
+
const disableAutoSelect = settings.disableAutoSelect || false;
|
|
28
|
+
const customContainerParent = container.parentElement;
|
|
29
|
+
let items = [];
|
|
30
|
+
let inputValue = '';
|
|
31
|
+
let minLen = 2;
|
|
32
|
+
const showOnFocus = settings.showOnFocus;
|
|
33
|
+
let selected;
|
|
34
|
+
let fetchCounter = 0;
|
|
35
|
+
let debounceTimer;
|
|
36
|
+
let destroyed = false;
|
|
37
|
+
let suppressAutocomplete = false;
|
|
38
|
+
|
|
39
|
+
if (settings.minLength !== undefined) {
|
|
40
|
+
minLen = settings.minLength;
|
|
41
|
+
}
|
|
42
|
+
if (!settings.input) {
|
|
43
|
+
throw new Error('input undefined');
|
|
44
|
+
}
|
|
45
|
+
const input = settings.input;
|
|
46
|
+
container.className = [container.className, 'autocomplete', settings.className || ''].join(' ').trim();
|
|
47
|
+
container.setAttribute('role', 'listbox');
|
|
48
|
+
input.setAttribute('role', 'combobox');
|
|
49
|
+
input.setAttribute('aria-expanded', 'false');
|
|
50
|
+
input.setAttribute('aria-autocomplete', 'list');
|
|
51
|
+
input.setAttribute('aria-controls', container.id);
|
|
52
|
+
input.setAttribute('aria-haspopup', 'listbox');
|
|
53
|
+
containerStyle.position = 'absolute';
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Detach the container from the DOM.
|
|
57
|
+
*/
|
|
58
|
+
const detach = () => {
|
|
59
|
+
container.remove();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Clear the debounce timer if set.
|
|
64
|
+
*/
|
|
65
|
+
const clearDebounceTimer = () => {
|
|
66
|
+
if (debounceTimer) {
|
|
67
|
+
window.clearTimeout(debounceTimer);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Attach the container to the DOM.
|
|
73
|
+
*/
|
|
74
|
+
const attach = () => {
|
|
75
|
+
if (!container.parentNode) {
|
|
76
|
+
(customContainerParent || doc.body).appendChild(container);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if the autocomplete container is displayed.
|
|
82
|
+
* @returns {boolean} True if container is attached to the DOM.
|
|
83
|
+
*/
|
|
84
|
+
const containerDisplayed = () => !!container.parentNode;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Clear autocomplete state and hide container.
|
|
88
|
+
*/
|
|
89
|
+
const clear = () => {
|
|
90
|
+
fetchCounter++;
|
|
91
|
+
items = [];
|
|
92
|
+
inputValue = '';
|
|
93
|
+
selected = undefined;
|
|
94
|
+
input.removeAttribute('aria-activedescendant');
|
|
95
|
+
input.setAttribute('aria-expanded', 'false');
|
|
96
|
+
detach();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Update the position of the autocomplete container.
|
|
101
|
+
*/
|
|
102
|
+
const updatePosition = () => {
|
|
103
|
+
if (!containerDisplayed()) return;
|
|
104
|
+
input.setAttribute('aria-expanded', 'true');
|
|
105
|
+
containerStyle.height = 'auto';
|
|
106
|
+
containerStyle.width = input.offsetWidth + 'px';
|
|
107
|
+
let maxHeight = 0;
|
|
108
|
+
let inputRect;
|
|
109
|
+
const calc = () => {
|
|
110
|
+
const docEl = doc.documentElement;
|
|
111
|
+
const clientTop = docEl.clientTop || doc.body.clientTop || 0;
|
|
112
|
+
const clientLeft = docEl.clientLeft || doc.body.clientLeft || 0;
|
|
113
|
+
const scrollTop = docEl.scrollTop;
|
|
114
|
+
const scrollLeft = docEl.scrollLeft;
|
|
115
|
+
inputRect = input.getBoundingClientRect();
|
|
116
|
+
const top = inputRect.top + input.offsetHeight + scrollTop - clientTop;
|
|
117
|
+
const left = inputRect.left + scrollLeft - clientLeft;
|
|
118
|
+
containerStyle.top = top + 'px';
|
|
119
|
+
containerStyle.left = left + 'px';
|
|
120
|
+
maxHeight = window.innerHeight - (inputRect.top + input.offsetHeight);
|
|
121
|
+
if (maxHeight < 0) maxHeight = 0;
|
|
122
|
+
containerStyle.top = top + 'px';
|
|
123
|
+
containerStyle.bottom = '';
|
|
124
|
+
containerStyle.left = left + 'px';
|
|
125
|
+
containerStyle.maxHeight = maxHeight + 'px';
|
|
126
|
+
};
|
|
127
|
+
calc();
|
|
128
|
+
calc();
|
|
129
|
+
if (settings.customize && inputRect) {
|
|
130
|
+
settings.customize(input, inputRect, container, maxHeight);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Redraw the autocomplete container with suggestions.
|
|
136
|
+
*/
|
|
137
|
+
const update = () => {
|
|
138
|
+
container.textContent = '';
|
|
139
|
+
input.removeAttribute('aria-activedescendant');
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line no-unused-vars
|
|
142
|
+
let render = (item, _, __) => {
|
|
143
|
+
const itemElement = doc.createElement('div');
|
|
144
|
+
itemElement.textContent = item.label || '';
|
|
145
|
+
return itemElement;
|
|
146
|
+
};
|
|
147
|
+
if (settings.render) {
|
|
148
|
+
render = settings.render;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const fragment = doc.createDocumentFragment();
|
|
152
|
+
for (const [index, item] of items.entries()) {
|
|
153
|
+
const div = render(item, inputValue, index);
|
|
154
|
+
if (div) {
|
|
155
|
+
div.id = container.id + '_' + index;
|
|
156
|
+
div.setAttribute('role', 'option');
|
|
157
|
+
div.addEventListener('click', (ev) => {
|
|
158
|
+
suppressAutocomplete = true;
|
|
159
|
+
try {
|
|
160
|
+
settings.onSelect(item, input);
|
|
161
|
+
} finally {
|
|
162
|
+
suppressAutocomplete = false;
|
|
163
|
+
}
|
|
164
|
+
clear();
|
|
165
|
+
ev.preventDefault();
|
|
166
|
+
ev.stopPropagation();
|
|
167
|
+
});
|
|
168
|
+
if (item === selected) {
|
|
169
|
+
div.className += ' selected';
|
|
170
|
+
div.setAttribute('aria-selected', 'true');
|
|
171
|
+
input.setAttribute('aria-activedescendant', div.id);
|
|
172
|
+
}
|
|
173
|
+
fragment.appendChild(div);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
container.appendChild(fragment);
|
|
177
|
+
if (items.length < 1) {
|
|
178
|
+
if (settings.emptyMsg) {
|
|
179
|
+
const empty = doc.createElement('div');
|
|
180
|
+
empty.id = container.id + '_' + uid();
|
|
181
|
+
empty.className = 'empty';
|
|
182
|
+
empty.textContent = settings.emptyMsg;
|
|
183
|
+
container.appendChild(empty);
|
|
184
|
+
empty.setAttribute('role', 'option');
|
|
185
|
+
empty.setAttribute('aria-disabled', 'true');
|
|
186
|
+
announce(settings.emptyMsg);
|
|
187
|
+
} else {
|
|
188
|
+
clear();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
attach();
|
|
193
|
+
updatePosition();
|
|
194
|
+
updateScroll();
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Update suggestions if the container is displayed.
|
|
199
|
+
*/
|
|
200
|
+
const updateIfDisplayed = () => {
|
|
201
|
+
if (containerDisplayed()) update();
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Handle window resize event.
|
|
206
|
+
*/
|
|
207
|
+
const resizeEventHandler = () => {
|
|
208
|
+
updateIfDisplayed();
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Handle scroll event.
|
|
213
|
+
* @param {Event} e - Scroll event.
|
|
214
|
+
*/
|
|
215
|
+
const scrollEventHandler = (e) => {
|
|
216
|
+
if (e.target === container) {
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
} else {
|
|
219
|
+
updateIfDisplayed();
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Handle input event on the input element.
|
|
225
|
+
*/
|
|
226
|
+
const inputEventHandler = () => {
|
|
227
|
+
if (!suppressAutocomplete) {
|
|
228
|
+
fetchFn(0 /* Keyboard */);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Scrolls the container to ensure the selected item is visible.
|
|
234
|
+
*/
|
|
235
|
+
const updateScroll = () => {
|
|
236
|
+
const elements = container.getElementsByClassName('selected');
|
|
237
|
+
if (elements.length > 0) {
|
|
238
|
+
let element = elements[0];
|
|
239
|
+
if (element.offsetTop < container.scrollTop) {
|
|
240
|
+
container.scrollTop = element.offsetTop;
|
|
241
|
+
} else {
|
|
242
|
+
const selectBottom = element.offsetTop + element.offsetHeight;
|
|
243
|
+
const containerBottom = container.scrollTop + container.offsetHeight;
|
|
244
|
+
if (selectBottom > containerBottom) {
|
|
245
|
+
container.scrollTop += selectBottom - containerBottom;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Select the previous suggestion in the list.
|
|
253
|
+
*/
|
|
254
|
+
const selectPreviousSuggestion = () => {
|
|
255
|
+
const index = items.indexOf(selected);
|
|
256
|
+
selected = index === -1
|
|
257
|
+
? undefined
|
|
258
|
+
: items[(index + items.length - 1) % items.length];
|
|
259
|
+
updateSelectedSuggestion(index);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Select the next suggestion in the list.
|
|
264
|
+
*/
|
|
265
|
+
const selectNextSuggestion = () => {
|
|
266
|
+
const index = items.indexOf(selected);
|
|
267
|
+
if (items.length < 1) {
|
|
268
|
+
selected = undefined;
|
|
269
|
+
} else if (index === -1) {
|
|
270
|
+
selected = items[0];
|
|
271
|
+
} else {
|
|
272
|
+
selected = items[(index + 1) % items.length];
|
|
273
|
+
}
|
|
274
|
+
updateSelectedSuggestion(index);
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Update the selected suggestion visually.
|
|
279
|
+
* @param {number} index - Index of the previously selected item.
|
|
280
|
+
*/
|
|
281
|
+
const updateSelectedSuggestion = (index) => {
|
|
282
|
+
if (items.length > 0) {
|
|
283
|
+
unselectSuggestion(index);
|
|
284
|
+
selectSuggestion(items.indexOf(selected));
|
|
285
|
+
updateScroll();
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Mark a suggestion as selected.
|
|
291
|
+
* @param {number} index - Index of the suggestion to select.
|
|
292
|
+
*/
|
|
293
|
+
const selectSuggestion = (index) => {
|
|
294
|
+
const element = doc.getElementById(container.id + '_' + index);
|
|
295
|
+
if (element) {
|
|
296
|
+
element.classList.add('selected');
|
|
297
|
+
element.setAttribute('aria-selected', 'true');
|
|
298
|
+
input.setAttribute('aria-activedescendant', element.id);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Unmark a suggestion as selected.
|
|
304
|
+
* @param {number} index - Index of the suggestion to unselect.
|
|
305
|
+
*/
|
|
306
|
+
const unselectSuggestion = (index) => {
|
|
307
|
+
const element = doc.getElementById(container.id + '_' + index);
|
|
308
|
+
if (element) {
|
|
309
|
+
element.classList.remove('selected');
|
|
310
|
+
element.removeAttribute('aria-selected');
|
|
311
|
+
input.removeAttribute('aria-activedescendant');
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Handle ArrowUp, ArrowDown, and Escape keys.
|
|
317
|
+
* @param {KeyboardEvent} ev - Keyboard event.
|
|
318
|
+
* @param {string} key - Key pressed.
|
|
319
|
+
*/
|
|
320
|
+
const handleArrowAndEscapeKeys = (ev, key) => {
|
|
321
|
+
const containerIsDisplayed = containerDisplayed();
|
|
322
|
+
if (key === 'Escape') {
|
|
323
|
+
clear();
|
|
324
|
+
} else {
|
|
325
|
+
if (!containerIsDisplayed || items.length < 1) return;
|
|
326
|
+
key === 'ArrowUp'
|
|
327
|
+
? selectPreviousSuggestion()
|
|
328
|
+
: selectNextSuggestion();
|
|
329
|
+
}
|
|
330
|
+
ev.preventDefault();
|
|
331
|
+
if (containerIsDisplayed) {
|
|
332
|
+
ev.stopPropagation();
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Handle Enter key to select a suggestion.
|
|
338
|
+
* @param {KeyboardEvent} ev - Keyboard event.
|
|
339
|
+
*/
|
|
340
|
+
const handleEnterKey = (ev) => {
|
|
341
|
+
if (selected) {
|
|
342
|
+
if (preventSubmit === 2 /* OnSelect */) {
|
|
343
|
+
ev.preventDefault();
|
|
344
|
+
}
|
|
345
|
+
suppressAutocomplete = true;
|
|
346
|
+
try {
|
|
347
|
+
settings.onSelect(selected, input);
|
|
348
|
+
} finally {
|
|
349
|
+
suppressAutocomplete = false;
|
|
350
|
+
}
|
|
351
|
+
clear();
|
|
352
|
+
}
|
|
353
|
+
if (preventSubmit === 1 /* Always */) {
|
|
354
|
+
ev.preventDefault();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Handle keydown events on the input element.
|
|
360
|
+
* @param {KeyboardEvent} ev - Keyboard event.
|
|
361
|
+
*/
|
|
362
|
+
const keydownEventHandler = (ev) => {
|
|
363
|
+
const key = ev.key;
|
|
364
|
+
if (key === 'ArrowUp' || key === 'ArrowDown' || key === 'Escape') {
|
|
365
|
+
handleArrowAndEscapeKeys(ev, key);
|
|
366
|
+
}
|
|
367
|
+
if (key === 'Enter') {
|
|
368
|
+
handleEnterKey(ev);
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Handle focus event on the input element.
|
|
374
|
+
*/
|
|
375
|
+
const focusEventHandler = () => {
|
|
376
|
+
if (showOnFocus) {
|
|
377
|
+
fetchFn(1 /* Focus */);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Fetch suggestions using the provided fetch function.
|
|
383
|
+
* @param {number} trigger - Trigger type (0: Keyboard, 1: Focus, 2: Mouse, 3: Manual).
|
|
384
|
+
*/
|
|
385
|
+
const fetchFn = (trigger) => {
|
|
386
|
+
if (input.value.length >= minLen || trigger === 1 /* Focus */) {
|
|
387
|
+
clearDebounceTimer();
|
|
388
|
+
debounceTimer = window.setTimeout(() => {
|
|
389
|
+
return startFetch(input.value, trigger, input.selectionStart || 0);
|
|
390
|
+
}, trigger === 0 /* Keyboard */ || trigger === 2 /* Mouse */ ? debounceWaitMs : 0);
|
|
391
|
+
} else {
|
|
392
|
+
clear();
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Start fetching suggestions and update the UI.
|
|
398
|
+
* @param {string} inputText - Current input value.
|
|
399
|
+
* @param {number} trigger - Trigger type.
|
|
400
|
+
* @param {number} cursorPos - Cursor position in the input.
|
|
401
|
+
*/
|
|
402
|
+
const startFetch = (inputText, trigger, cursorPos) => {
|
|
403
|
+
if (destroyed) return;
|
|
404
|
+
const savedFetchCounter = ++fetchCounter;
|
|
405
|
+
try {
|
|
406
|
+
settings.fetch(inputText, (elements) => {
|
|
407
|
+
if (fetchCounter === savedFetchCounter && elements) {
|
|
408
|
+
items = elements;
|
|
409
|
+
inputValue = inputText;
|
|
410
|
+
selected = (items.length < 1 || disableAutoSelect) ? undefined : items[0];
|
|
411
|
+
update();
|
|
412
|
+
}
|
|
413
|
+
}, trigger, cursorPos);
|
|
414
|
+
} catch {
|
|
415
|
+
clear();
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Handle keyup events on the input element.
|
|
421
|
+
* @param {KeyboardEvent} e - Keyboard event.
|
|
422
|
+
*/
|
|
423
|
+
const keyupEventHandler = (e) => {
|
|
424
|
+
if (settings.keyup) {
|
|
425
|
+
settings.keyup({
|
|
426
|
+
event: e,
|
|
427
|
+
fetch: () => fetchFn(0 /* Keyboard */),
|
|
428
|
+
});
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (!containerDisplayed() && e.key === 'ArrowDown') {
|
|
432
|
+
fetchFn(0 /* Keyboard */);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Handle click events on the input element.
|
|
438
|
+
* @param {MouseEvent} e - Mouse event.
|
|
439
|
+
*/
|
|
440
|
+
const clickEventHandler = (e) => {
|
|
441
|
+
if (settings.click) {
|
|
442
|
+
settings.click({
|
|
443
|
+
event: e,
|
|
444
|
+
fetch: () => fetchFn(2 /* Mouse */),
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Handle blur event on the input element.
|
|
451
|
+
*/
|
|
452
|
+
const blurEventHandler = () => {
|
|
453
|
+
setTimeout(() => {
|
|
454
|
+
if (doc.activeElement !== input) {
|
|
455
|
+
clear();
|
|
456
|
+
}
|
|
457
|
+
}, 200);
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Manually trigger fetching of suggestions.
|
|
462
|
+
*/
|
|
463
|
+
const manualFetch = () => {
|
|
464
|
+
startFetch(input.value, 3 /* Manual */, input.selectionStart || 0);
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// on long clicks focus will be lost and onSelect method will not be called
|
|
468
|
+
container.addEventListener('mousedown', (evt) => {
|
|
469
|
+
evt.stopPropagation();
|
|
470
|
+
evt.preventDefault();
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// If the custom autocomplete container is already appended to the DOM during widget initialization, detach it.
|
|
474
|
+
detach();
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Remove DOM elements and clear event handlers.
|
|
478
|
+
*/
|
|
479
|
+
const destroy = () => {
|
|
480
|
+
input.removeEventListener('focus', focusEventHandler);
|
|
481
|
+
input.removeEventListener('keyup', keyupEventHandler);
|
|
482
|
+
input.removeEventListener('click', clickEventHandler);
|
|
483
|
+
input.removeEventListener('keydown', keydownEventHandler);
|
|
484
|
+
input.removeEventListener('input', inputEventHandler);
|
|
485
|
+
input.removeEventListener('blur', blurEventHandler);
|
|
486
|
+
window.removeEventListener('resize', resizeEventHandler);
|
|
487
|
+
doc.removeEventListener('scroll', scrollEventHandler, true);
|
|
488
|
+
input.removeAttribute('role');
|
|
489
|
+
input.removeAttribute('aria-expanded');
|
|
490
|
+
input.removeAttribute('aria-autocomplete');
|
|
491
|
+
input.removeAttribute('aria-controls');
|
|
492
|
+
input.removeAttribute('aria-activedescendant');
|
|
493
|
+
input.removeAttribute('aria-haspopup');
|
|
494
|
+
clearDebounceTimer();
|
|
495
|
+
clear();
|
|
496
|
+
destroyed = true;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
// setup event handlers
|
|
500
|
+
input.addEventListener('keyup', keyupEventHandler);
|
|
501
|
+
input.addEventListener('click', clickEventHandler);
|
|
502
|
+
input.addEventListener('keydown', keydownEventHandler);
|
|
503
|
+
input.addEventListener('input', inputEventHandler);
|
|
504
|
+
input.addEventListener('blur', blurEventHandler);
|
|
505
|
+
input.addEventListener('focus', focusEventHandler);
|
|
506
|
+
window.addEventListener('resize', resizeEventHandler);
|
|
507
|
+
doc.addEventListener('scroll', scrollEventHandler, true);
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
destroy,
|
|
511
|
+
fetch: manualFetch,
|
|
512
|
+
};
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
export default autocomplete;
|