demarches-normaliennes/app/javascript/new_design/champs/carte.js

39 lines
935 B
JavaScript
Raw Normal View History

2019-04-25 16:30:51 +02:00
async function initialize() {
const elements = document.querySelectorAll('.carte');
if (elements.length) {
for (let element of elements) {
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
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) {
const { drawEditableMap } = await import('../../shared/carte-editor');
drawEditableMap(element, data);
} else {
const { drawMap } = await import('../../shared/carte');
drawMap(element, data);
2019-04-25 16:30:51 +02:00
}
2018-10-17 12:07:12 +02:00
}
async function loadAndRedrawMap(element, data) {
const { redrawMap } = await import('../../shared/carte-editor');
2018-10-17 12:07:12 +02: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);
loadAndRedrawMap(element, data);
2018-10-17 12:07:12 +02:00
});