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

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-10-17 12:07:12 +02:00
import { CREATE } from 'leaflet-freedraw';
import { delegate } from '@utils';
import {
initMap,
getCurrentMap,
geocodeAddress,
drawCadastre,
drawQuartiersPrioritaires,
2018-10-23 15:38:20 +02:00
drawParcellesAgricoles,
2018-10-17 12:07:12 +02:00
drawUserSelection,
addFreeDrawEvents
} from '../../shared/carte';
function initialize() {
for (let element of document.querySelectorAll('.carte')) {
diplayMap(element, null, true);
}
window.DS.drawMapData = (selector, data) => {
let element = document.querySelector(selector);
diplayMap(element, data);
};
}
function diplayMap(element, data, initial = false) {
data = data || JSON.parse(element.dataset.geo);
let editable = element.classList.contains('edit');
let map = initMap(element, data.position, editable);
// draw external polygons
drawCadastre(map, data, editable);
drawQuartiersPrioritaires(map, data, editable);
2018-10-23 15:38:20 +02:00
drawParcellesAgricoles(map, data, editable);
2018-10-17 12:07:12 +02:00
// draw user polygon
if (initial) {
drawUserSelection(map, data, editable);
if (editable) {
let input = element.parentElement.querySelector('input[data-remote]');
addFreeDrawEvents(map, input);
}
}
}
addEventListener('turbolinks:load', initialize);
delegate('click', '.toolbar .new-area', event => {
event.preventDefault();
let map = getCurrentMap(event.target);
if (map) {
map.freeDraw.mode(CREATE);
}
});
delegate('autocomplete:select', '.toolbar [data-address]', event => {
let map = getCurrentMap(event.target);
if (map) {
geocodeAddress(map, event.detail.label);
}
});