2018-02-24 18:30:30 +01:00
|
|
|
import Awesomplete from 'awesomplete';
|
|
|
|
import 'awesomplete/awesomplete.css';
|
2017-04-03 19:23:30 +02:00
|
|
|
|
2018-02-24 18:30:30 +01:00
|
|
|
(() => {
|
|
|
|
const autocompleteFields = document.querySelectorAll('input[data-multiple]');
|
2020-11-05 18:16:52 +01:00
|
|
|
const tagsSeparatorElement = document.querySelector('input[name="tags_separator"]');
|
|
|
|
const tagsSeparator = tagsSeparatorElement ? tagsSeparatorElement.value || " " : " ";
|
|
|
|
|
2018-02-24 18:30:30 +01:00
|
|
|
[...autocompleteFields].forEach((autocompleteField) => {
|
2020-11-05 18:16:52 +01:00
|
|
|
const awesome = new Awesomplete(Awesomplete.$(autocompleteField));
|
2018-02-24 18:30:30 +01:00
|
|
|
|
2020-11-05 18:16:52 +01:00
|
|
|
// Tags are separated by separator
|
|
|
|
awesome.filter = (text, input) => Awesomplete.FILTER_CONTAINS(
|
|
|
|
text,
|
|
|
|
input.match(new RegExp(`[^${tagsSeparator}]*$`))[0])
|
|
|
|
;
|
|
|
|
// Insert new selected tag in the input
|
|
|
|
awesome.replace = (text) => {
|
|
|
|
const before = awesome.input.value.match(new RegExp(`^.+${tagsSeparator}+|`))[0];
|
|
|
|
awesome.input.value = `${before}${text}${tagsSeparator}`;
|
|
|
|
};
|
|
|
|
// Highlight found items
|
|
|
|
awesome.item = (text, input) => Awesomplete.ITEM(text, input.match(new RegExp(`[^${tagsSeparator}]*$`))[0]);
|
|
|
|
// Don't display already selected items
|
|
|
|
const reg = new RegExp(`/(\w+)${tagsSeparator}/g`);
|
|
|
|
let match;
|
|
|
|
awesome.data = (item, input) => {
|
|
|
|
while ((match = reg.exec(input))) {
|
|
|
|
if (item === match[1]) {
|
|
|
|
return '';
|
2017-04-03 19:23:30 +02:00
|
|
|
}
|
2018-02-24 18:30:30 +01:00
|
|
|
}
|
2020-11-05 18:16:52 +01:00
|
|
|
return item;
|
|
|
|
};
|
|
|
|
awesome.minChars = 1;
|
2018-02-24 18:30:30 +01:00
|
|
|
});
|
|
|
|
})();
|