2018-10-03 10:39:35 +02:00
|
|
|
import L from 'leaflet';
|
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
import FreeDraw, { NONE, CREATE } from 'leaflet-freedraw';
|
|
|
|
import { fire, on, getJSON } from '@utils';
|
2015-11-10 18:08:47 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
import { getData } from '../shared/data';
|
|
|
|
import { initMap } from '../shared/carto';
|
2015-11-10 18:08:47 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
import polygonArea from './carto/polygon_area';
|
|
|
|
import drawFactory from './carto/draw';
|
2016-01-18 14:45:08 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
function initialize() {
|
|
|
|
if (document.getElementById('map')) {
|
|
|
|
const data = getData('carto');
|
|
|
|
const position = data.position;
|
|
|
|
|
|
|
|
const map = initMap(position);
|
|
|
|
const freeDraw = new FreeDraw({
|
|
|
|
mode: NONE,
|
|
|
|
smoothFactor: 4,
|
|
|
|
mergePolygons: false
|
|
|
|
});
|
2016-01-18 14:45:08 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
map.addLayer(freeDraw);
|
2016-01-18 14:45:08 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
addEventFreeDraw(freeDraw);
|
|
|
|
addEventSearchAddress(map);
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
const cartoDrawZones = drawFactory(map, freeDraw);
|
|
|
|
window.DS = { cartoDrawZones };
|
2018-10-03 10:39:35 +02:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
cartoDrawZones(data);
|
2016-01-18 14:45:08 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
if (freeDraw.polygons[0]) {
|
|
|
|
map.setZoom(18);
|
|
|
|
map.fitBounds(freeDraw.polygons[0].getBounds());
|
2018-10-03 10:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 14:33:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
addEventListener('turbolinks:load', initialize);
|
2016-01-27 15:48:27 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
function addEventFreeDraw(freeDraw) {
|
|
|
|
freeDraw.on('markers', ({ latLngs }) => {
|
|
|
|
const input = document.querySelector('input[name=selection]');
|
2016-01-22 14:01:02 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
if (polygonArea(latLngs) < 300000) {
|
|
|
|
input.value = JSON.stringify(latLngs);
|
2018-10-03 10:39:35 +02:00
|
|
|
} else {
|
2018-10-13 10:59:07 +02:00
|
|
|
input.value = '{ "error": "TooManyPolygons" }';
|
2018-10-03 10:39:35 +02:00
|
|
|
}
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
fire(input, 'change');
|
2017-04-04 16:15:33 +02:00
|
|
|
});
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
on('#map', 'click', () => {
|
2018-10-03 10:39:35 +02:00
|
|
|
freeDraw.mode(NONE);
|
2017-04-04 16:15:33 +02:00
|
|
|
});
|
2015-11-12 17:34:12 +01:00
|
|
|
|
2018-10-13 10:59:07 +02:00
|
|
|
on('#new', 'click', () => {
|
2018-10-03 10:39:35 +02:00
|
|
|
freeDraw.mode(CREATE);
|
2017-04-04 16:15:33 +02:00
|
|
|
});
|
2015-11-10 18:08:47 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 10:39:35 +02:00
|
|
|
function getAddressPoint(map, request) {
|
2018-10-13 10:59:07 +02:00
|
|
|
getJSON('/ban/address_point', { request }).then(data => {
|
2018-10-03 10:39:35 +02:00
|
|
|
if (data.lat !== null) {
|
|
|
|
map.setView(new L.LatLng(data.lat, data.lon), data.zoom);
|
|
|
|
}
|
2017-04-04 16:15:33 +02:00
|
|
|
});
|
2015-11-25 10:26:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 10:39:35 +02:00
|
|
|
function addEventSearchAddress(map) {
|
2018-10-13 10:59:07 +02:00
|
|
|
on(
|
|
|
|
'#search-by-address input[type=address]',
|
2018-10-03 10:39:35 +02:00
|
|
|
'autocomplete:select',
|
|
|
|
(_, seggestion) => {
|
|
|
|
getAddressPoint(map, seggestion['label']);
|
|
|
|
}
|
|
|
|
);
|
2016-06-09 16:28:44 +02:00
|
|
|
}
|