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