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