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

57 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-10-03 10:39:35 +02:00
import L from 'leaflet';
import $ from 'jquery';
2017-04-04 16:15:33 +02:00
2018-10-03 10:39:35 +02:00
export function qpActive() {
return $('#map.qp').length > 0;
}
2017-04-04 16:15:33 +02:00
2018-10-03 10:39:35 +02:00
export function getQP(dossierId, coordinates) {
return $.ajax({
2017-04-04 16:15:33 +02:00
method: 'post',
2018-10-03 10:39:35 +02:00
url: `/users/dossiers/${dossierId}/carte/qp`,
data: { coordinates: JSON.stringify(coordinates) },
dataType: 'json'
}).done(({ quartier_prioritaires }) => values(quartier_prioritaires));
}
2018-10-03 10:39:35 +02:00
let qpItems;
2018-10-03 10:39:35 +02:00
export function displayQP(map, qps) {
if (!qpActive()) return;
2018-10-03 10:39:35 +02:00
$('#qp.list ul').html('');
newQPLayer(map);
2018-10-03 10:39:35 +02:00
if (qps.length > 0) {
qps.forEach(function(qp) {
$('#qp.list ul').append('<li>' + qp.commune + ' : ' + qp.nom + '</li>');
2017-04-04 16:15:33 +02:00
qpItems.addData(qp.geometry);
});
2017-04-04 16:15:33 +02:00
qpItems.setStyle({
fillColor: '#31708f',
weight: 2,
opacity: 0.3,
color: 'white',
dashArray: '3',
fillOpacity: 0.7
2018-10-03 10:39:35 +02:00
});
} else {
$('#qp.list ul').html('<li>AUCUN</li>');
2017-04-04 16:15:33 +02:00
}
}
2018-10-03 10:39:35 +02:00
function newQPLayer(map) {
if (qpItems) {
2017-04-04 16:15:33 +02:00
map.removeLayer(qpItems);
2018-10-03 10:39:35 +02:00
}
2017-04-04 16:15:33 +02:00
qpItems = new L.GeoJSON();
qpItems.addTo(map);
2017-04-04 15:27:04 +02:00
}
2018-10-03 10:39:35 +02:00
function values(obj) {
return Object.keys(obj).map(v => obj[v]);
}