1<?php 2 3use Fisharebest\Webtrees\I18N; 4 5/** 6 * @var array<mixed> $data 7 * @var object $leaflet_config 8 */ 9 10?> 11 12<div class="py-4"> 13 <div class="row gchart wt-places-tab-wrapper"> 14 <div id="wt-map" class="col-sm-9 wt-ajax-load wt-map wt-places-tab-map" dir="ltr"></div> 15 <ul class="col-sm-3 wt-places-tab-sidebar wt-page-options-value list-unstyled px-md-1"></ul> 16 </div> 17</div> 18 19<script> 20 'use strict'; 21 22 (function () { 23 const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>; 24 25 let map = null; 26 const sidebar = document.querySelector('.wt-places-tab-sidebar'); 27 28 const scrollOptions = { 29 behavior: "smooth", 30 block: "start", 31 inline: "start" 32 }; 33 34 // Map components 35 let markers = L.markerClusterGroup({ 36 showCoverageOnHover: false, 37 }); 38 39 /** 40 * Passed to resetControl to 41 * perform necessary reset actions on map 42 */ 43 let resetActions = function () { 44 map.flyToBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15 }); 45 sidebar.firstElementChild.scrollIntoView(scrollOptions); 46 47 return false; 48 } 49 50 /** 51 * 52 * @private 53 */ 54 let _drawMap = function() { 55 map = webtrees.buildLeafletJsMap('wt-map', config, resetActions); 56 }; 57 58 /** 59 * 60 * @private 61 */ 62 let _buildMapData = function() { 63 let data = <?= json_encode($data, JSON_THROW_ON_ERROR) ?>; 64 65 if (data.features.length === 0) { 66 map.fitWorld(); 67 sidebar.innerHTML = '<div class="bg-info text-white text-center">' + <?= json_encode(I18N::translate('Nothing to show'), JSON_THROW_ON_ERROR) ?> + '</div>'; 68 } else { 69 sidebar.innerHTML = ''; 70 let geoJsonLayer = L.geoJson(data, { 71 pointToLayer: function(feature, latlng) { 72 return new L.Marker(latlng, { 73 icon: L.BeautifyIcon.icon({ 74 icon : feature.properties.icon["name"], 75 borderColor : "transparent", 76 backgroundColor: feature.properties.icon["color"], 77 iconShape : "marker", 78 textColor : "white", 79 }), 80 title: feature.properties.tooltip, 81 alt : feature.properties.tooltip, 82 id : feature.id, 83 }) 84 .on("popupopen", function(e) { 85 let item = document.querySelector('.gchart[data-wt-feature-id="' + e.target.feature.id + '"]'); 86 item.classList.add('messagebox'); 87 item.scrollIntoView(scrollOptions); 88 }) 89 .on("popupclose", function() { 90 sidebar.childNodes.forEach(e => e.classList.remove('messagebox')); 91 sidebar.firstElementChild.scrollIntoView(scrollOptions); 92 }); 93 }, 94 onEachFeature: function(feature, layer) { 95 layer.bindPopup(feature.properties.summary); 96 sidebar.innerHTML += `<li class="gchart px-md-2" data-wt-feature-id=${feature.id}>${feature.properties.summary}</li>`; 97 }, 98 }); 99 markers.addLayer(geoJsonLayer); 100 map.addLayer(markers); 101 map.fitBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15}); 102 } 103 }; 104 105 // Can't use window.onload here. seems to be because of AJAX loading 106 const _loadListeners = function() { 107 // Activate marker popup when sidebar entry clicked 108 sidebar.querySelectorAll('.gchart').forEach((element) => { 109 var eventId = parseInt(element.dataset.wtFeatureId); 110 111 element.addEventListener('click', () => { 112 // first close any existing 113 map.closePopup(); 114 //find the marker corresponding to the clicked event 115 let mkrLayer = markers.getLayers().filter(function (v) { 116 return v.feature !== undefined && v.feature.id === eventId; 117 }); 118 119 let mkr = mkrLayer.pop(); 120 121 markers.zoomToShowLayer(mkr, function (e) { 122 mkr.openPopup(); 123 }); 124 125 return false; 126 }); 127 128 // stop click on a person also opening the popup 129 element.querySelectorAll('a').forEach((el) => { 130 el.addEventListener('click', (e) => { 131 e.stopPropagation(); 132 }); 133 }); 134 }); 135 } 136 137 _drawMap(); 138 _buildMapData(); 139 _loadListeners(); 140 })(); 141</script> 142