xref: /webtrees/resources/views/modules/place-hierarchy/map.phtml (revision f352d954172f32309c4bbee129b60895bd22fd92)
1<?php
2
3use Fisharebest\Webtrees\View;
4
5/**
6 * @var array<mixed> $data
7 * @var object       $leaflet_config
8 */
9
10?>
11
12<div class="row gchart wt-place-hierarchy-wrapper">
13    <div id="wt-map" class="col-sm-9 wt-ajax-load wt-map wt-place-hierarchy-map" dir="ltr"></div>
14    <ul class="col-sm-3 wt-place-hierarchy-sidebar wt-page-options-value list-unstyled px-md-1"></ul>
15</div>
16
17<?php View::push('javascript') ?>
18<script>
19  'use strict';
20
21  (function () {
22    const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>;
23
24    let map = null;
25    const sidebar = document.querySelector('.wt-place-hierarchy-sidebar');
26
27    const scrollOptions = {
28      behavior: "smooth",
29      block: "start",
30      inline: "start"
31    };
32
33    // Map components
34    let markers = L.markerClusterGroup({
35      showCoverageOnHover: false,
36    });
37
38    /**
39     * Passed to resetControl to
40     * perform necessary reset actions on map
41     */
42     let resetActions = function () {
43      map.flyToBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15 });
44      sidebar.firstElementChild.scrollIntoView(scrollOptions);
45
46      return false;
47    }
48
49    /**
50     * @private
51     */
52    let _drawMap = function () {
53      map = webtrees.buildLeafletJsMap('wt-map', config, resetActions);
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            alt: feature.properties.tooltip,
74            id: feature.id,
75          })
76            .on('popupopen', function (e) {
77              let item = document.querySelector('.mapped[data-wt-feature-id="' + e.target.feature.id + '"]');
78              item.classList.add('messagebox');
79              item.scrollIntoView(scrollOptions);
80            })
81            .on('popupclose', function () {
82              sidebar.querySelectorAll('.mapped').forEach(e => e.classList.remove('messagebox'));
83              sidebar.firstElementChild.scrollIntoView(scrollOptions);
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