2018-08-22 15:06:19 +02:00
|
|
|
import autocomplete from 'autocomplete.js';
|
2018-10-09 11:35:22 +02:00
|
|
|
import { getJSON, fire } from '@utils';
|
2018-08-22 15:06:19 +02:00
|
|
|
|
|
|
|
const sources = [
|
|
|
|
{
|
|
|
|
type: 'address',
|
2018-10-15 23:39:42 +02:00
|
|
|
url: '/address/suggestions'
|
2018-08-22 15:06:19 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'path',
|
|
|
|
url: '/admin/procedures/path_list'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
autoselect: true,
|
|
|
|
minLength: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
function selector(type) {
|
|
|
|
return `[data-autocomplete=${type}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function source(url) {
|
|
|
|
return {
|
|
|
|
source(query, callback) {
|
2018-10-09 11:35:22 +02:00
|
|
|
getJSON(url, { request: query }).then(callback);
|
2018-08-22 15:06:19 +02:00
|
|
|
},
|
|
|
|
templates: {
|
|
|
|
suggestion({ label, mine }) {
|
|
|
|
const mineClass = `path-mine-${mine ? 'true' : 'false'}`;
|
|
|
|
const openTag = `<div class="aa-suggestion ${mineClass}">`;
|
|
|
|
return autocomplete.escapeHighlightedString(label, openTag, '</div>');
|
|
|
|
}
|
2018-10-15 21:41:42 +02:00
|
|
|
},
|
|
|
|
debounce: 300
|
2018-08-22 15:06:19 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-01 15:22:37 +02:00
|
|
|
addEventListener('ds:page:update', function() {
|
2018-08-22 15:06:19 +02:00
|
|
|
for (let { type, url } of sources) {
|
2019-02-07 11:01:21 +01:00
|
|
|
for (let element of document.querySelectorAll(selector(type))) {
|
2019-02-07 13:16:33 +01:00
|
|
|
element.removeAttribute('data-autocomplete');
|
|
|
|
autocompleteInitializeElement(element, url);
|
2018-08-28 12:04:44 +02:00
|
|
|
}
|
2018-08-22 15:06:19 +02:00
|
|
|
}
|
2019-08-01 15:22:37 +02:00
|
|
|
});
|
2019-02-07 11:01:21 +01:00
|
|
|
|
|
|
|
function autocompleteInitializeElement(element, url) {
|
|
|
|
const select = autocomplete(element, options, [source(url)]);
|
|
|
|
select.on('autocomplete:selected', ({ target }, suggestion) => {
|
|
|
|
fire(target, 'autocomplete:select', suggestion);
|
|
|
|
select.autocomplete.setVal(suggestion.label);
|
|
|
|
});
|
|
|
|
}
|