'use strict'; /** * @namespace UI */ const UI = {}; /** * Add a Leaflet control to center the map * @param {Map} map The Leaflet map object * @param {LatLngExpression} centerCoords The coordinates to center the map * @param {number} zoom Zoom level */ UI.addCenterMapControl = function (map, centerCoords, zoom) { L.Control.CenterControl = L.Control.extend({ options: { position: 'topleft' }, onAdd: function (map) { let controlDiv = L.DomUtil.create('div', 'leaflet-draw-toolbar leaflet-bar'); L.DomEvent .addListener(controlDiv, 'click', L.DomEvent.stopPropagation) .addListener(controlDiv, 'click', L.DomEvent.preventDefault) .addListener(controlDiv, 'click', function () { map.setView(centerCoords, zoom, {animate: true}); } ); let controlUI = L.DomUtil.create('a', 'leaflet-draw-edit-remove', controlDiv); controlUI.title = 'Centra la mappa'; controlUI.href = '#'; controlUI.innerHTML = ` `; return controlDiv; } }); let centerCtr = new L.Control.CenterControl(); map.addControl(centerCtr); } /** * * @param {string} triggerId The ID of the trigger element */ UI.toggleMenu = function (triggerId) { const trigger = document.querySelector(`#${triggerId}`); trigger.addEventListener('click', () => { const menu = document.querySelector('#menu'); menu.classList.toggle('is-hidden'); menu.classList.toggle('is-2'); document.querySelector('#map').parentElement.classList.toggle('is-full'); }); } /** * Open a modal with DB data * @param {object} data The data retrieved from the DB to display as modal content */ UI.openModal = async function (data) { const modal = document.querySelector('.modal'); // DEBUG let table = ` `; if (data.images.length) { table += ``; for (const img of data.images) { table += ` `; } } table += '
Denominazione${data.denominazione}
Località${data.localita}
Indirizzo${data.indirizzo}
Comune${data.comune}
Localizzazione${data.localizzazione}
Definizione${data.definizione}
Periodo${data.periodo}
Fase${data.fase}
Cronologia${data.cronologia}
Motivazione cronologia${data.motivazione_cron}
Ritrovamento${data.ritrovamento}
Materiali rinvenuti${data.materiali_rinv}
Luogo custodia materiali${data.luogo_custodia_mat}
Tutela vigente${data.tutela_vigente}
Stato di conservazione${data.stato_conserv}
Documenti${data.documenti}
Descrizione${data.descrizione}
Immagini
${img.didascalia}
'; modal.querySelector('.modal-content').innerHTML = table; modal.classList.add('is-active'); const closeBtn = modal.querySelector('.modal-close'); const modalBg = modal.querySelector('.modal-background'); const closeModal = () => modal.classList.remove('is-active'); // CLose modal when clicking either on the X button or on the background closeBtn.addEventListener('click', () => { closeModal(); }); modalBg.addEventListener('click', () => { closeModal(); }); } /** * @param {string} menuListSel Menu list selector * @param {L.Map} map * @param {L.LayerGroup} sites */ UI.sitesMenu = function (menuListSel, map, sites) { // Close menu if arrow button is clicked... this.toggleMenu('close-menu'); const menu = document.querySelector(menuListSel); menu.addEventListener('click', event => { if (event.target.nodeName === 'A') { // zoom to layer... const layer = sites.customGetLayer(event.target.id); map.setView( layer.getBounds().getCenter(), 19, {animate: true, duration: 1, easeLinearity: 0.25} ); } }); } export default UI;