2019-04-25 16:30:51 +02:00
|
|
|
async function initialize() {
|
|
|
|
const elements = document.querySelectorAll('.carte');
|
|
|
|
|
|
|
|
if (elements.length) {
|
|
|
|
for (let element of elements) {
|
2020-01-30 11:26:50 +01:00
|
|
|
loadAndDrawMap(element);
|
2019-04-25 16:30:51 +02:00
|
|
|
}
|
2018-10-17 12:07:12 +02:00
|
|
|
}
|
2019-04-25 16:30:51 +02:00
|
|
|
}
|
2018-10-17 12:07:12 +02:00
|
|
|
|
2020-01-30 11:26:50 +01:00
|
|
|
async function loadAndDrawMap(element) {
|
|
|
|
const data = JSON.parse(element.dataset.geo);
|
|
|
|
const editable = element.classList.contains('edit');
|
2019-04-25 16:30:51 +02:00
|
|
|
|
|
|
|
if (editable) {
|
2020-01-30 11:26:50 +01:00
|
|
|
const { drawEditableMap } = await import('../../shared/carte-editor');
|
|
|
|
|
|
|
|
drawEditableMap(element, data);
|
2019-04-25 16:30:51 +02:00
|
|
|
}
|
2018-10-17 12:07:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 11:26:50 +01:00
|
|
|
async function loadAndRedrawMap(element, data) {
|
|
|
|
const { redrawMap } = await import('../../shared/carte-editor');
|
2018-10-17 12:07:12 +02:00
|
|
|
|
2020-01-30 11:26:50 +01:00
|
|
|
redrawMap(element, data);
|
2018-10-17 12:07:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 10:48:49 +02:00
|
|
|
addEventListener('turbolinks:load', initialize);
|
2018-10-17 12:07:12 +02:00
|
|
|
|
2019-04-25 16:30:51 +02:00
|
|
|
addEventListener('carte:update', ({ detail: { selector, data } }) => {
|
|
|
|
const element = document.querySelector(selector);
|
2020-01-30 11:26:50 +01:00
|
|
|
|
|
|
|
loadAndRedrawMap(element, data);
|
2018-10-17 12:07:12 +02:00
|
|
|
});
|