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',
|
|
|
|
url: '/ban/search'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener('turbolinks:load', function() {
|
|
|
|
for (let { type, url } of sources) {
|
2018-08-28 12:04:44 +02:00
|
|
|
for (let target of document.querySelectorAll(selector(type))) {
|
|
|
|
let select = autocomplete(target, options, [source(url)]);
|
|
|
|
select.on('autocomplete:selected', ({ target }, suggestion) => {
|
2018-10-09 11:35:22 +02:00
|
|
|
fire(target, 'autocomplete:select', suggestion);
|
2018-08-28 12:04:44 +02:00
|
|
|
select.autocomplete.setVal(suggestion.label);
|
|
|
|
});
|
|
|
|
}
|
2018-08-22 15:06:19 +02:00
|
|
|
}
|
|
|
|
});
|