|
1 | | -if(window.Choices) |
2 | | -{ |
3 | | - document.addEventListener('DOMContentLoaded', function() { |
| 1 | +if (window.Tagify) { |
| 2 | + document.addEventListener('DOMContentLoaded', function () { |
| 3 | + const tagifyElements = document.querySelectorAll('[data-tagify="1"]'); |
| 4 | + tagifyElements.forEach(element => { |
| 5 | + const maxTags = element.getAttribute('data-tagify-maxTags') || 'Infinity'; |
| 6 | + const dataUrl = element.getAttribute('data-tagify-url'); |
| 7 | + const tagify = new Tagify(element, { |
| 8 | + maxTags: maxTags |
| 9 | + }); |
| 10 | + |
| 11 | + if (dataUrl) { |
| 12 | + tagify.on('input', function (e) { |
| 13 | + var controller; |
| 14 | + const value = e.detail.value; |
| 15 | + if (value.length > 0) { |
| 16 | + controller && controller.abort(); |
| 17 | + controller = new AbortController(); |
| 18 | + tagify.loading(true); |
| 19 | + fetch(`${element.getAttribute('data-tagify-url')}?search=${value}&autocomplete=1`, { signal: controller.signal }) |
| 20 | + .then(response => response.json()) |
| 21 | + .then(data => { |
| 22 | + tagify.whitelist = data; |
| 23 | + tagify.loading(false).dropdown.show(value); |
| 24 | + }) |
| 25 | + .catch(error => { |
| 26 | + console.error(error); |
| 27 | + }); |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | + }); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +if (window.Choices) { |
| 36 | + document.addEventListener('DOMContentLoaded', function () { |
4 | 37 | const elements = document.querySelectorAll('[data-choices="1"]'); |
5 | 38 | elements.forEach(element => { |
6 | 39 | const removeItemButton = element.getAttribute('data-choices-removeItemButton') != 0; |
7 | 40 | const maxItemCount = element.getAttribute('data-choices-maxItemCount') || -1; |
8 | | - const choicesElement = new Choices(element, { |
| 41 | + const addChoices = element.getAttribute('data-choices-addChoices') != 0; |
| 42 | + const choices = new Choices(element, { |
9 | 43 | removeItemButton: removeItemButton, |
10 | | - maxItemCount: maxItemCount |
| 44 | + maxItemCount: maxItemCount, |
| 45 | + addChoices: addChoices |
11 | 46 | }); |
12 | 47 | const dataUrl = element.getAttribute('data-choices-url'); |
13 | 48 | if (dataUrl) { |
14 | | - choicesElement.passedElement.element.addEventListener( |
15 | | - 'search', async (event) => { |
16 | | - try { |
17 | | - const items = await fetch(`${dataUrl}?search=${event.detail.value}&autocomplete=1`); |
18 | | - const data = await items.json(); |
19 | | - // Clear existing choices |
20 | | - choicesElement.clearChoices(); |
21 | | - |
22 | | - // Set new choices |
23 | | - choicesElement.setChoices(data, 'value', 'label', true); |
24 | | - } catch (err) { |
25 | | - console.error(err); |
26 | | - } |
27 | | - }); |
| 49 | + choices.passedElement.element.addEventListener( |
| 50 | + 'search', async (event) => { |
| 51 | + try { |
| 52 | + var controller; |
| 53 | + controller && controller.abort(); |
| 54 | + controller = new AbortController(); |
| 55 | + const items = await fetch(`${dataUrl}?search=${event.detail.value}&autocomplete=1`, { signal: controller.signal }); |
| 56 | + const data = await items.json(); |
| 57 | + // Clear existing choices |
| 58 | + choices.clearChoices(); |
| 59 | + |
| 60 | + // Set new choices |
| 61 | + choices.setChoices(data, 'value', 'label', true); |
| 62 | + } catch (err) { |
| 63 | + console.error(err); |
| 64 | + } |
| 65 | + }); |
28 | 66 | } |
29 | 67 | }); |
30 | 68 | }); |
|
0 commit comments