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