demarches-normaliennes/app/javascript/old_design/carto.js

178 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-10-03 10:39:35 +02:00
import L from 'leaflet';
import area from '@turf/area';
import FreeDraw, { NONE, EDIT, CREATE, DELETE } from 'leaflet-freedraw';
import $ from 'jquery';
import { getData } from '../shared/data';
import { DEFAULT_POSITION, LAT, LON } from '../shared/carto';
import { qpActive, displayQP, getQP } from './carto/qp';
import { cadastreActive, displayCadastre, getCadastre } from './carto/cadastre';
function initialize() {
if ($('#map').length > 0) {
getPosition(getData('carto').dossierId).then(
position => initializeWithPosition(position),
() => initializeWithPosition(DEFAULT_POSITION)
);
}
}
2018-10-03 10:39:35 +02:00
addEventListener('turbolinks:load', initialize);
2018-10-03 10:39:35 +02:00
function initializeWithPosition(position) {
const OSM = L.tileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{
attribution:
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}
);
2018-10-03 10:39:35 +02:00
const map = L.map('map', {
2017-04-04 16:15:33 +02:00
center: new L.LatLng(position.lat, position.lon),
zoom: position.zoom,
layers: [OSM],
scrollWheelZoom: false
});
2018-10-03 10:39:35 +02:00
if (qpActive()) {
displayQP(map, getJsonValue('#quartier_prioritaires'));
}
2018-10-03 10:39:35 +02:00
if (cadastreActive()) {
displayCadastre(map, getJsonValue('#cadastres'));
}
2018-10-03 10:39:35 +02:00
const freeDraw = new FreeDraw({
mode: NONE,
smoothFactor: 4,
mergePolygons: false
});
2017-04-04 16:15:33 +02:00
map.addLayer(freeDraw);
2018-10-03 10:39:35 +02:00
const latLngs = getJsonValue('#json_latlngs');
if (latLngs.length) {
2017-04-04 16:15:33 +02:00
map.setZoom(18);
2018-10-03 10:39:35 +02:00
for (let polygon of latLngs) {
freeDraw.createPolygon(polygon);
}
2015-11-19 17:20:22 +01:00
2017-04-04 16:15:33 +02:00
map.fitBounds(freeDraw.polygons[0].getBounds());
2018-10-03 10:39:35 +02:00
} else if (position.lat == LAT && position.lon == LON) {
2017-04-04 16:15:33 +02:00
map.setView(new L.LatLng(position.lat, position.lon), position.zoom);
2018-10-03 10:39:35 +02:00
}
2018-10-03 10:39:35 +02:00
addEventFreeDraw(map, freeDraw);
addEventSearchAddress(map);
}
2018-10-03 10:39:35 +02:00
function getExternalData(map, latLngs) {
const { dossierId } = getData('carto');
2018-10-03 10:39:35 +02:00
if (qpActive()) {
getQP(dossierId, latLngs).then(qps => displayQP(map, qps));
}
2018-10-03 10:39:35 +02:00
if (cadastreActive()) {
const polygons = { type: 'FeatureCollection', features: [] };
2018-10-03 10:39:35 +02:00
for (let i = 0; i < latLngs.length; i++) {
polygons.features.push(featurePolygonLatLngs(latLngs[i]));
}
2018-10-03 10:39:35 +02:00
if (area(polygons) < 300000) {
getCadastre(dossierId, latLngs).then(cadastres =>
displayCadastre(map, cadastres)
);
} else {
displayCadastre(map, [{ zoom_error: true }]);
}
2017-04-04 16:15:33 +02:00
}
}
2018-10-03 10:39:35 +02:00
function featurePolygonLatLngs(coordinates) {
return {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [JSON.parse(getJsonPolygons([coordinates]))['latLngs']]
2017-04-04 16:15:33 +02:00
}
2018-10-03 10:39:35 +02:00
};
}
2018-10-03 10:39:35 +02:00
function addEventFreeDraw(map, freeDraw) {
freeDraw.on('markers', ({ latLngs }) => {
$('#json_latlngs').val(JSON.stringify(latLngs));
2018-10-03 10:39:35 +02:00
addEventEdit(freeDraw);
2018-10-03 10:39:35 +02:00
getExternalData(map, latLngs);
2017-04-04 16:15:33 +02:00
});
2018-10-03 10:39:35 +02:00
$('#map').on('click', () => {
freeDraw.mode(NONE);
2017-04-04 16:15:33 +02:00
});
2018-10-03 10:39:35 +02:00
$('#new').on('click', () => {
freeDraw.mode(CREATE);
2017-04-04 16:15:33 +02:00
});
}
2018-10-03 10:39:35 +02:00
function addEventEdit(freeDraw) {
$('.leaflet-container svg').removeAttr('pointer-events');
$('.leaflet-container g path').on('click', () => {
setTimeout(function() {
freeDraw.mode(EDIT | DELETE);
}, 50);
2017-04-04 16:15:33 +02:00
});
2016-06-14 11:00:57 +02:00
}
2018-10-03 10:39:35 +02:00
function getPosition(dossierId) {
return $.getJSON(`/users/dossiers/${dossierId}/carte/position`);
}
2015-08-10 11:05:06 +02:00
2018-10-03 10:39:35 +02:00
function getAddressPoint(map, request) {
$.get('/ban/address_point', { request }).then(data => {
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) {
$("#search-by-address input[type='address']").on(
'autocomplete:select',
(_, seggestion) => {
getAddressPoint(map, seggestion['label']);
}
);
2016-06-09 16:28:44 +02:00
}
2018-10-03 10:39:35 +02:00
function getJsonValue(selector) {
let data = document.querySelector(selector).value;
if (data && data !== '[]') {
return JSON.parse(data);
}
return [];
2015-11-25 10:26:55 +01:00
}
2016-06-09 16:28:44 +02:00
2018-10-03 10:39:35 +02:00
function getJsonPolygons(latLngGroups) {
var groups = [];
2017-04-04 16:15:33 +02:00
2018-10-03 10:39:35 +02:00
latLngGroups.forEach(function forEach(latLngs) {
var group = [];
latLngs.forEach(function forEach(latLng) {
group.push('[' + latLng.lng + ', ' + latLng.lat + ']');
});
groups.push('{ "latLngs": [' + group.join(', ') + '] }');
2017-04-04 16:15:33 +02:00
});
2018-10-03 10:39:35 +02:00
return groups;
2017-02-01 10:31:32 +01:00
}