feat(js): use constants for selectors and optional chaining
Parent
cefbeca59f
révision
ae6994ee4e
|
@ -1,3 +1,18 @@
|
|||
// =============================================================================
|
||||
// Constants
|
||||
// =============================================================================
|
||||
|
||||
const ID_CENTER_LATITUDE = 'id_center_latitude';
|
||||
const ID_CENTER_LONGITUDE = 'id_center_longitude';
|
||||
const ID_CENTER_CALCULATE = 'map-center-calculate';
|
||||
|
||||
const SELECTOR_POINTS_FORMS = '#id_points-FORMS';
|
||||
const SELECTOR_POINT_FORM = `${SELECTOR_POINTS_FORMS} > li:not(.deleted)`;
|
||||
|
||||
// =============================================================================
|
||||
// Helpers
|
||||
// =============================================================================
|
||||
|
||||
function isNumber(value) {
|
||||
return value === Number(value).toString();
|
||||
}
|
||||
|
@ -6,28 +21,32 @@ function add(accumulator, a) {
|
|||
return accumulator + a;
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const latInput = document.getElementById('id_center_latitude');
|
||||
const lngInput = document.getElementById('id_center_longitude');
|
||||
const calcButton = document.getElementById('map-center-calculate');
|
||||
const errorMessage = calcButton.nextElementSibling;
|
||||
// =============================================================================
|
||||
// Main functions
|
||||
// =============================================================================
|
||||
|
||||
calcButton.addEventListener('click', () => {
|
||||
// Calculate the center of the map from the centroïd of the points
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const latInput = document.getElementById(ID_CENTER_LATITUDE);
|
||||
const lngInput = document.getElementById(ID_CENTER_LONGITUDE);
|
||||
const calcButton = document.getElementById(ID_CENTER_CALCULATE);
|
||||
const errorMessage = calcButton?.nextElementSibling;
|
||||
|
||||
calcButton?.addEventListener('click', () => {
|
||||
const lat = [];
|
||||
const lng = [];
|
||||
|
||||
errorMessage.setAttribute('hidden', '');
|
||||
|
||||
[].slice.call(document.querySelectorAll('#id_points-FORMS > li:not(.deleted)'))
|
||||
.forEach(function (child) {
|
||||
const latitude = child.querySelector('[name$=-latitude]').value;
|
||||
const longitude = child.querySelector('[name$=-longitude]').value;
|
||||
document.querySelectorAll(SELECTOR_POINT_FORM).forEach((child) => {
|
||||
const latitude = child.querySelector('[name$=-latitude]').value;
|
||||
const longitude = child.querySelector('[name$=-longitude]').value;
|
||||
|
||||
if (isNumber(latitude) && isNumber(longitude)) {
|
||||
lat.push(Number(latitude));
|
||||
lng.push(Number(longitude));
|
||||
}
|
||||
});
|
||||
if (isNumber(latitude) && isNumber(longitude)) {
|
||||
lat.push(Number(latitude));
|
||||
lng.push(Number(longitude));
|
||||
}
|
||||
});
|
||||
|
||||
if (!lat.length) {
|
||||
errorMessage.removeAttribute('hidden');
|
||||
|
|
Référencer dans un nouveau ticket