1<?php 2use Fisharebest\Webtrees\I18N; 3use Fisharebest\Webtrees\Individual; 4use Fisharebest\Webtrees\View; 5 6/** 7 * @var Individual $individual 8 */ 9?> 10 11 12<div class="py-4"> 13 <div class="row gchart osm-wrapper"> 14 <div id="osm-map" class="col-sm-9 wt-ajax-load osm-user-map" dir="ltr"></div> 15 <ul class="col-sm-3 osm-sidebar wt-page-options-value list-unstyled p-0"></ul> 16 </div> 17</div> 18 19<?php View::push('styles') ?> 20<style> 21 .osm-wrapper, .osm-user-map { 22 height: 75vh 23 } 24 25 .osm-sidebar { 26 height: 100%; 27 overflow-y: auto; 28 padding: 0; 29 margin: 0; 30 border: 0; 31 display: none; 32 font-size: small; 33 } 34 35 .osm-sidebar .gchart { 36 margin: 1px; 37 padding: 2px 38 } 39 40 .osm-sidebar .gchart img { 41 height: 15px; 42 width: 25px 43 } 44 45 .osm-sidebar .border-danger:hover { 46 cursor: not-allowed 47 } 48</style> 49<?php View::endpush() ?> 50 51<?php View::push('javascript') ?> 52<script type="application/javascript"> 53 "use strict"; 54 55 window.WT_OSM = (function() { 56 let baseData = { 57 minZoom: 2, 58 providerName: "OpenStreetMap.Mapnik", 59 providerOptions: [], 60 }; 61 62 let map = null; 63 let zoom = null; 64 let markers = L.markerClusterGroup({ 65 showCoverageOnHover: false 66 }); 67 68 let resetControl = L.Control.extend({ 69 options: { 70 position: 'topleft' 71 }, 72 73 onAdd: function (map) { 74 let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); 75 container.onclick = function () { 76 if (zoom) { 77 map.flyTo(markers.getBounds().getCenter(), zoom); 78 } else { 79 map.flyToBounds(markers.getBounds().pad(0.2)); 80 } 81 return false; 82 }; 83 let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container); 84 let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>; 85 anchor.href = '#'; 86 anchor.title = reset; 87 anchor.role = 'button'; 88 $(anchor).attr('aria-label', 'reset'); 89 let image = L.DomUtil.create('i', 'fas fa-redo', anchor); 90 image.alt = reset; 91 92 return container; 93 }, 94 }); 95 96 /** 97 * 98 * @private 99 */ 100 let _drawMap = function () { 101 map = L.map('osm-map', { 102 center : [0, 0], 103 minZoom : baseData.minZoom, // maxZoom set by leaflet-providers.js 104 zoomControl: false, // remove default 105 }); 106 L.tileLayer.provider(baseData.providerName, baseData.providerOptions).addTo(map); 107 L.control.zoom({ // Add zoom with localised text 108 zoomInTitle : <?= json_encode(I18N::translate('Zoom in')) ?>, 109 zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>, 110 }).addTo(map); 111 }; 112 113 /** 114 * @param Generations 115 * @private 116 */ 117 let _addLayer = function (Generations) { 118 let geoJsonLayer; 119 let domObj = '.osm-sidebar'; 120 let sidebar = ''; 121 122 $.getJSON(<?= json_encode(route('module', ['module' => 'pedigree-map', 'action' => 'MapData', 'tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>, { 123 generations: Generations 124 }) 125 .done(function (data, textStatus, jqXHR) { 126 if (jqXHR.status === 200 && data.features.length === 1) { 127 zoom = data.features[0].properties.zoom; 128 } 129 geoJsonLayer = L.geoJson(data, { 130 pointToLayer : function (feature, latlng) { 131 return new L.Marker(latlng, { 132 icon : L.BeautifyIcon.icon({ 133 icon : feature.properties.icon['name'], 134 borderColor : 'transparent', 135 backgroundColor: feature.valid ? feature.properties.icon['color'] : 'transparent', 136 iconShape : 'marker', 137 textColor : feature.valid ? 'white' : 'transparent', 138 }), 139 title: feature.properties.tooltip, 140 alt : feature.properties.tooltip, 141 id : feature.id 142 }) 143 .on('popupopen', function (e) { 144 let sidebar = $('.osm-sidebar'); 145 let item = sidebar.children(".gchart[data-id=" + e.target.feature.id + "]"); 146 item.addClass('messagebox'); 147 sidebar.scrollTo(item); 148 }) 149 .on('popupclose', function () { 150 $('.osm-sidebar').children(".gchart") 151 .removeClass('messagebox'); 152 }); 153 }, 154 onEachFeature: function (feature, layer) { 155 if (feature.properties.polyline) { 156 let pline = L.polyline(feature.properties.polyline.points, feature.properties.polyline.options); 157 markers.addLayer(pline); 158 } 159 layer.bindPopup(feature.properties.summary); 160 let myclass = feature.valid ? 'gchart' : 'border border-danger'; 161 sidebar += `<li class="${myclass}" data-id=${feature.id}>${feature.properties.summary}</li>`; 162 }, 163 }); 164 }) 165 .fail(function (jqXHR, textStatus, errorThrown) { 166 console.log(jqXHR, textStatus, errorThrown); 167 }) 168 .always(function (data_jqXHR, textStatus, jqXHR_errorThrown) { 169 switch (jqXHR_errorThrown.status) { 170 case 200: // Success 171 $(domObj).append(sidebar); 172 markers.addLayer(geoJsonLayer); 173 map 174 .addControl(new resetControl()) 175 .addLayer(markers) 176 .fitBounds(markers.getBounds().pad(0.2)); 177 if (zoom) { 178 map.setView(markers.getBounds().getCenter(), zoom); 179 } 180 break; 181 case 204: // No data 182 map.fitWorld(); 183 $(domObj).append('<div class="bg-info text-white">' + <?= json_encode(I18N::translate('No mappable items')) ?> + '</div>'); 184 break; 185 default: // Anything else 186 map.fitWorld(); 187 $(domObj).append('<div class="bg-danger text-white">' + <?= json_encode(I18N::translate('An unknown error occurred')) ?> + '</div>'); 188 } 189 $(domObj).slideDown(300); 190 }); 191 }; 192 193 /** 194 * 195 * @param elem 196 * @returns {$} 197 */ 198 199 $.fn.scrollTo = function (elem) { 200 let _this = $(this); 201 _this.animate({ 202 scrollTop: elem.offset().top - _this.offset().top + _this.scrollTop() 203 }); 204 return this; 205 }; 206 207 /** 208 * @param generations integer 209 * @private 210 */ 211 let _initialize = function (generations) { 212 // Activate marker popup when sidebar entry clicked 213 $(function () { 214 $('.osm-sidebar') 215 // open marker popup if sidebar event is clicked 216 .on('click', '.gchart', function (e) { 217 // first close any existing 218 map.closePopup(); 219 let eventId = $(this).data('id'); 220 //find the marker corresponding to the clicked event 221 let mkrLayer = markers.getLayers().filter(function (v) { 222 return typeof(v.feature) !== 'undefined' && v.feature.id === eventId; 223 }); 224 let mkr = mkrLayer.pop(); 225 // Unfortunately zoomToShowLayer zooms to maxZoom 226 // when all marker in a cluster have exactly the 227 // same co-ordinates 228 markers.zoomToShowLayer(mkr, function (e) { 229 mkr.openPopup(); 230 }); 231 return false; 232 }) 233 .on('click', 'a', function (e) { // stop click on a person also opening the popup 234 e.stopPropagation(); 235 }); 236 }); 237 238 _drawMap(); 239 _addLayer(generations); 240 }; 241 242 return { 243 /** 244 * @param generations integer 245 */ 246 drawMap: function (generations) { 247 _initialize(generations); 248 } 249 }; 250 })(); 251 252 WT_OSM.drawMap(<?= json_encode($generations) ?>); 253</script> 254<?php View::endpush() ?> 255