From d315057ee044643b452fe00fe81912b51a671d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Fri, 22 Mar 2024 11:24:15 +0100 Subject: [PATCH] Rudimentary control for sites --- css/app.css | 6 ++++ js/caprigis.js | 9 ++++-- js/index.js | 10 +++---- js/ui.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 js/ui.js diff --git a/css/app.css b/css/app.css index 3cbf754..6f1c413 100644 --- a/css/app.css +++ b/css/app.css @@ -106,6 +106,12 @@ a:visited { #fullscreen { padding: 2px; } +/* Custom controls */ +.site-control { + padding-top: 3px; + padding-left: 10px; + padding-right: 33px; +} /* Leaflef map container */ @media (max-width: 840px) { .map-sm { diff --git a/js/caprigis.js b/js/caprigis.js index aa38bc3..d2b6ba3 100644 --- a/js/caprigis.js +++ b/js/caprigis.js @@ -29,8 +29,10 @@ const optionsPaesistici = { const BASE_URL = location.href; /** - * + * Capitalize a text string + * @todo Move to utils * @param {?string} text + * @returns {?string} The capitalized string or null */ function capitalize(text) { let capital = text; @@ -50,7 +52,7 @@ function capitalize(text) { /** * @param {string} mapId * @param {number} zoomLevel - * @returns {Map} + * @returns {{map: Map, sites: Layer}} */ GIS.initMap = async function (mapId, zoomLevel = 15) { let layerVincoli = await this.loadLayer('vincoli.geojson', optionsVincoli); @@ -80,6 +82,9 @@ GIS.initMap = async function (mapId, zoomLevel = 15) { }; let layerControl = L.control.layers(baseMap, archeo).addTo(map); + + // TODO Horrible? + return {map: map, sites: layerSiti}; } /** * @todo Distinguere tipo di geojson per contenuto popup diff --git a/js/index.js b/js/index.js index 2024c6d..d0e1329 100644 --- a/js/index.js +++ b/js/index.js @@ -1,9 +1,9 @@ import GIS from './caprigis.js'; +import UI from './ui.js'; -document.addEventListener('DOMContentLoaded', () => { - GIS.initMap('map'); - - // Layer vincoli - //GIS.loadLayer('vincoli.geojson'); +document.addEventListener('DOMContentLoaded', async () => { + const init = await GIS.initMap('map'); + const centerCoords = init.sites.getBounds().getCenter(); + UI.addSitesControl(init.map, centerCoords, 'Grotta di Matermania'); }); \ No newline at end of file diff --git a/js/ui.js b/js/ui.js new file mode 100644 index 0000000..81074e5 --- /dev/null +++ b/js/ui.js @@ -0,0 +1,79 @@ +'use strict'; + +/** + * @namespace UI + */ +const UI = {}; + +const siteIcon = ` + + +`; + +/** + * Zoom and pan the map to a given layer position + * @param {Map} map + * @param {Layer} layer + * @param {number} zoom Zoom level + */ +UI.zoomToPosition = function (map, layer, zoom = 20) { + const center = layer.getCenter(); + + map.setView(center, zoom, {animate: true}); +} +/** + * + * @param {Map} map + * @param {LatLng} coordinates + * @param {string} popupContent + */ +UI.addSitesControl = function (map, coordinates, popupContent) { + const popUpCoords = L.latLng(coordinates.lat + 0.0001, coordinates.lng); + L.Control.SiteControl = L.Control.extend({ + options: { + position: 'topright' + }, + 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( + coordinates, + 19, + {animate: true, duration: 1, easeLinearity: 0.25} + ); + L.popup() + .setLatLng(popUpCoords) + .setContent(popupContent) + .openOn(map); + }); + + let controlUI = L.DomUtil.create('a', 'leaflet-draw-edit-remove site-control', controlDiv); + controlUI.title = 'Vai al sito'; + controlUI.href = '#'; + controlUI.innerHTML = siteIcon; + return controlDiv; + } + }); + + let siteCtr = new L.Control.SiteControl(); + map.addControl(siteCtr); +} + +export default UI; \ No newline at end of file