Skip to content

Commit 0da5752

Browse files
committed
Added script for Tagify
1 parent 29c6669 commit 0da5752

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

resources/js/field.js

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
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 () {
437
const elements = document.querySelectorAll('[data-choices="1"]');
538
elements.forEach(element => {
639
const removeItemButton = element.getAttribute('data-choices-removeItemButton') != 0;
740
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, {
943
removeItemButton: removeItemButton,
10-
maxItemCount: maxItemCount
44+
maxItemCount: maxItemCount,
45+
addChoices: addChoices
1146
});
1247
const dataUrl = element.getAttribute('data-choices-url');
1348
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+
});
2866
}
2967
});
3068
});

0 commit comments

Comments
 (0)