'use strict'; import Spotlight from './vendor/spotlight.js/src/js/spotlight.js'; /** * @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); } /** * Toggle burger menu for small screens * @param {string} burgerClass The CSS class of the burger element */ UI.toggleBurger = function(burgerClass) { const burger = document.querySelector(`.${burgerClass}`); burger.addEventListener('click', () => { burger.classList.toggle('is-active'); const menuId = burger.getAttribute('data-target'); document.querySelector(`#${menuId}`).classList.toggle('is-active'); }); } /** * Toggle side menu and rescale map container * @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 * @todo Refactor! Web components?? * @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 content = `
Scheda del sito
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
Leggi tutto${data.descrizione}
`; if (data.documents.length) { let publications = data.documents.filter(d => d.tipo === 'pubblicazione'); let docs = data.documents.filter(d => d.tipo === 'documentazione'); content += `
Documentazione di archivio
    `; for (const doc of docs) { content += `
  1. ${doc.titolo}
  2. `; } content += '
'; content += `
Pubblicazioni del progetto Carta Archeologica
    `; for (const doc of publications) { content += `
  1. ${doc.titolo}
  2. `; } content += '
'; } if (data.surveys.length) { content += `

Elaborazioni CNR da rilievi

`; content += `

Cliccare sull'immagine per aprire la gallery

`; } if (data.photos.length) { content += `

Fotografie

`; content += `

Cliccare sull'immagine per aprire la gallery

`; } modal.querySelector('.modal-content').innerHTML = content; 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(); }); this.imageGallery('gallery-1', data.surveys); this.imageGallery('gallery-2', data.photos); } /** * @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} ); } }); } /** * Open Spotlight gallery * @param {string} galleryId The id of the trigger element * @param {Array} images Array of image objects from DB */ UI.imageGallery = function (galleryId, images) { const element = document.querySelector(`#${galleryId}`); if (element) { let gallery = []; for (let img of images) { gallery.push({src: `img/${img.filename}`, description: img.didascalia}); } document.querySelector(`#${galleryId}`).addEventListener('click', () => { Spotlight.show(gallery); }); } } export default UI;