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