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