xref: /webtrees/resources/views/modules/places/tab.phtml (revision aeea31bd8bfebe3bb14b66dc16d5c7d1393c0bb2)
1d70512abSGreg Roach<?php
2d70512abSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
5d70512abSGreg Roachuse Fisharebest\Webtrees\I18N;
6d70512abSGreg Roach
77c2c99faSGreg Roach/**
87c2c99faSGreg Roach * @var array<mixed> $data
9c9c6f2ecSGreg Roach * @var object       $leaflet_config
107c2c99faSGreg Roach */
117c2c99faSGreg Roach
12d70512abSGreg Roach?>
13dd6b2bfcSGreg Roach
14dd6b2bfcSGreg Roach<div class="py-4">
1555ea0389SGreg Roach    <div class="row gchart wt-places-tab-wrapper wt-fullscreen-container">
1684df6051SGreg Roach        <div id="wt-map" class="col-sm-9 wt-ajax-load wt-map wt-places-tab-map" dir="ltr"></div>
1784df6051SGreg Roach        <ul class="col-sm-3 wt-places-tab-sidebar wt-page-options-value list-unstyled px-md-1"></ul>
18dd6b2bfcSGreg Roach    </div>
19dd6b2bfcSGreg Roach</div>
20dd6b2bfcSGreg Roach
2174b9ba3fSGreg Roach<script>
22ffb44950SGreg Roach  'use strict';
23dd6b2bfcSGreg Roach
2489b00848SDavid Drury  (function () {
25728c8c27SGreg Roach    const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>;
26dd6b2bfcSGreg Roach
27dd6b2bfcSGreg Roach    let map = null;
2884df6051SGreg Roach    const sidebar = document.querySelector('.wt-places-tab-sidebar');
2989b00848SDavid Drury
3089b00848SDavid Drury    const scrollOptions = {
3189b00848SDavid Drury      behavior: "smooth",
32*aeea31bdSGreg Roach      block: "nearest",
3389b00848SDavid Drury      inline: "start"
3489b00848SDavid Drury    };
35597fb44bSDavid Drury
36597fb44bSDavid Drury    // Map components
37dd6b2bfcSGreg Roach    let markers = L.markerClusterGroup({
38dd6b2bfcSGreg Roach        showCoverageOnHover: false,
39dd6b2bfcSGreg Roach    });
40dd6b2bfcSGreg Roach
41f352d954SDavid Drury    /**
42f352d954SDavid Drury     * Passed to resetControl to
43f352d954SDavid Drury     * perform necessary reset actions on map
44b2e5f20eSGreg Roach     *
45b2e5f20eSGreg Roach     * @param {Event} event
46f352d954SDavid Drury     */
47b2e5f20eSGreg Roach    let resetCallback = function (event) {
48b2e5f20eSGreg Roach      event.preventDefault();
4957ef7ac6Smiqrogroove      map.flyToBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15 });
50f352d954SDavid Drury    }
51dd6b2bfcSGreg Roach
52dd6b2bfcSGreg Roach    /**
53dd6b2bfcSGreg Roach     *
54dd6b2bfcSGreg Roach     * @private
55dd6b2bfcSGreg Roach     */
56dd6b2bfcSGreg Roach    let _drawMap = function() {
57b7b71725SGreg Roach      map = webtrees.buildLeafletJsMap('wt-map', config, resetCallback);
58dd6b2bfcSGreg Roach    };
59dd6b2bfcSGreg Roach
60597fb44bSDavid Drury    /**
61597fb44bSDavid Drury     *
62597fb44bSDavid Drury     * @private
63597fb44bSDavid Drury     */
64597fb44bSDavid Drury    let _buildMapData = function() {
65728c8c27SGreg Roach      let data = <?= json_encode($data, JSON_THROW_ON_ERROR) ?>;
66dd6b2bfcSGreg Roach
67597fb44bSDavid Drury      if (data.features.length === 0) {
68597fb44bSDavid Drury        map.fitWorld();
6989b00848SDavid Drury        sidebar.innerHTML = '<div class="bg-info text-white text-center">' + <?= json_encode(I18N::translate('Nothing to show'), JSON_THROW_ON_ERROR) ?> + '</div>';
70597fb44bSDavid Drury      } else {
7189b00848SDavid Drury        sidebar.innerHTML = '';
72597fb44bSDavid Drury        let geoJsonLayer = L.geoJson(data, {
73dd6b2bfcSGreg Roach          pointToLayer: function(feature, latlng) {
74dd6b2bfcSGreg Roach            return new L.Marker(latlng, {
75dd6b2bfcSGreg Roach              icon: L.BeautifyIcon.icon({
76dd6b2bfcSGreg Roach                icon           : feature.properties.icon["name"],
77dd6b2bfcSGreg Roach                borderColor    : "transparent",
7880993423SGreg Roach                backgroundColor: feature.properties.icon["color"],
79dd6b2bfcSGreg Roach                iconShape      : "marker",
8080993423SGreg Roach                textColor      : "white",
81dd6b2bfcSGreg Roach              }),
82dd6b2bfcSGreg Roach              title: feature.properties.tooltip,
83dd6b2bfcSGreg Roach              id   : feature.id,
84dd6b2bfcSGreg Roach            })
85dd6b2bfcSGreg Roach            .on("popupopen", function(e) {
8689b00848SDavid Drury              let item = document.querySelector('.gchart[data-wt-feature-id="' + e.target.feature.id + '"]');
8789b00848SDavid Drury              item.classList.add('messagebox');
8889b00848SDavid Drury              item.scrollIntoView(scrollOptions);
89dd6b2bfcSGreg Roach            })
90dd6b2bfcSGreg Roach            .on("popupclose", function() {
9189b00848SDavid Drury              sidebar.childNodes.forEach(e => e.classList.remove('messagebox'));
92dd6b2bfcSGreg Roach            });
93dd6b2bfcSGreg Roach          },
94dd6b2bfcSGreg Roach          onEachFeature: function(feature, layer) {
95dd6b2bfcSGreg Roach              layer.bindPopup(feature.properties.summary);
9689b00848SDavid Drury              sidebar.innerHTML += `<li class="gchart px-md-2" data-wt-feature-id=${feature.id}>${feature.properties.summary}</li>`;
97dd6b2bfcSGreg Roach          },
98dd6b2bfcSGreg Roach        });
99dd6b2bfcSGreg Roach        markers.addLayer(geoJsonLayer);
100597fb44bSDavid Drury        map.addLayer(markers);
10157ef7ac6Smiqrogroove        map.fitBounds(markers.getBounds(), {padding: [50, 30], maxZoom: 15});
102dd6b2bfcSGreg Roach      }
103dd6b2bfcSGreg Roach    };
104dd6b2bfcSGreg Roach
10589b00848SDavid Drury    // Can't use window.onload here. seems to be because of AJAX loading
10689b00848SDavid Drury    const _loadListeners = function() {
107dd6b2bfcSGreg Roach      // Activate marker popup when sidebar entry clicked
10889b00848SDavid Drury      sidebar.querySelectorAll('.gchart').forEach((element) => {
109815856a7SGreg Roach        const eventId = parseInt(element.dataset.wtFeatureId);
11089b00848SDavid Drury
11189b00848SDavid Drury        element.addEventListener('click', () => {
112dd6b2bfcSGreg Roach          // first close any existing
113dd6b2bfcSGreg Roach          map.closePopup();
114dd6b2bfcSGreg Roach          //find the marker corresponding to the clicked event
115815856a7SGreg Roach          const mkrLayer = markers.getLayers().filter(function (v) {
11689b00848SDavid Drury            return v.feature !== undefined && v.feature.id === eventId;
117dd6b2bfcSGreg Roach          });
11889b00848SDavid Drury
119dd6b2bfcSGreg Roach          let mkr = mkrLayer.pop();
12089b00848SDavid Drury
121dd6b2bfcSGreg Roach          markers.zoomToShowLayer(mkr, function (e) {
122dd6b2bfcSGreg Roach            mkr.openPopup();
123dd6b2bfcSGreg Roach          });
12489b00848SDavid Drury        });
12589b00848SDavid Drury
12689b00848SDavid Drury        // stop click on a person also opening the popup
12789b00848SDavid Drury        element.querySelectorAll('a').forEach((el) => {
12889b00848SDavid Drury          el.addEventListener('click', (e) => {
129dd6b2bfcSGreg Roach            e.stopPropagation();
130dd6b2bfcSGreg Roach          });
131597fb44bSDavid Drury        });
13289b00848SDavid Drury      });
133815856a7SGreg Roach    };
134dd6b2bfcSGreg Roach
135597fb44bSDavid Drury    _drawMap();
136597fb44bSDavid Drury    _buildMapData();
13789b00848SDavid Drury    _loadListeners();
138597fb44bSDavid Drury  })();
139dd6b2bfcSGreg Roach</script>
140