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

35 lines
836 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);
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
}
2020-05-07 16:31:36 +02:00
addEventListener('DOMContentLoaded', 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
});