xref: /webtrees/resources/views/modules/place-hierarchy/map.phtml (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
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">
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: "start",
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     let resetCallback = function () {
45      map.flyToBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15 });
46      sidebar.firstElementChild.scrollIntoView(scrollOptions);
47    }
48
49    /**
50     * @private
51     */
52    let _drawMap = function () {
53      map = webtrees.buildLeafletJsMap('wt-map', config, resetCallback);
54    };
55
56    /**
57     * @private
58     */
59    let _buildMapData = function () {
60      let data = <?= json_encode($data['markers'], JSON_THROW_ON_ERROR) ?>;
61
62      let geoJsonLayer = L.geoJson(data, {
63        pointToLayer: function (feature, latlng) {
64          return new L.Marker(latlng, {
65            icon: L.BeautifyIcon.icon({
66              icon: 'bullseye fas',
67              borderColor: 'transparent',
68              backgroundColor: '#1e90ff',
69              iconShape: 'marker',
70              textColor: 'white',
71            }),
72            title: feature.properties.tooltip,
73            id: feature.id,
74          })
75            .on('popupopen', function (e) {
76              let item = document.querySelector('.mapped[data-wt-feature-id="' + e.target.feature.id + '"]');
77              item.classList.add('messagebox');
78              item.scrollIntoView(scrollOptions);
79            })
80            .on('popupclose', function () {
81              sidebar.querySelectorAll('.mapped').forEach(e => e.classList.remove('messagebox'));
82              sidebar.firstElementChild.scrollIntoView(scrollOptions);
83            });
84        },
85        onEachFeature: function (feature, layer) {
86          layer.bindPopup(feature.properties.popup);
87        },
88      });
89
90      if (data.features.length > 0) {
91        markers.addLayer(geoJsonLayer);
92        map.addLayer(markers);
93      }
94
95      map.fitBounds(<?= json_encode($data['bounds'], JSON_THROW_ON_ERROR) ?>, { padding: [50, 30] });
96      sidebar.innerHTML = <?= json_encode($data['sidebar'], JSON_THROW_ON_ERROR) ?>;
97   };
98
99    window.onload = function() {
100      // Activate marker popup when sidebar entry clicked
101      sidebar.querySelectorAll('.mapped').forEach((element) => {
102        var eventId = parseInt(element.dataset.wtFeatureId);
103
104        element.addEventListener('click', () => {
105          // first close any existing
106          map.closePopup();
107          //find the marker corresponding to the clicked event
108          let mkrLayer = markers.getLayers().filter(function (v) {
109            return v.feature !== undefined && v.feature.id === eventId;
110          });
111
112          let mkr = mkrLayer.pop();
113
114          markers.zoomToShowLayer(mkr, function (e) {
115            mkr.openPopup();
116          });
117
118          return false;
119        });
120
121        // stop clicking on a person also opening the popup
122        element.querySelectorAll('a').forEach((el) => {
123          el.addEventListener('click', (e) => {
124            e.stopPropagation();
125          });
126        });
127      });
128    }
129
130    _drawMap();
131    _buildMapData();
132  })();
133</script>
134<?php View::endpush() ?>
135