xref: /webtrees/resources/views/modules/pedigree-map/chart.phtml (revision 0d123f04d29c959ec190af6efde9b5d6b0f27ac0)
19ec21bfdSGreg Roach<?php
2d70512abSGreg Roach
39ec21bfdSGreg Roachuse Fisharebest\Webtrees\I18N;
49ec21bfdSGreg Roachuse Fisharebest\Webtrees\Individual;
59ec21bfdSGreg Roachuse Fisharebest\Webtrees\View;
69ec21bfdSGreg Roach
79ec21bfdSGreg Roach/**
89ec21bfdSGreg Roach * @var Individual $individual
9*0d123f04SDavid Drury * @var int $generations
109ec21bfdSGreg Roach */
119ec21bfdSGreg Roach?>
129ec21bfdSGreg Roach
133dcc812bSGreg Roach<div class="py-4">
143dcc812bSGreg Roach    <div class="row gchart osm-wrapper">
15a2e602dbSGreg Roach        <div id="osm-map" class="col-sm-9 wt-ajax-load osm-user-map" dir="ltr"></div>
16*0d123f04SDavid Drury        <ul class="col-sm-3 osm-sidebar wt-page-options-value list-unstyled px-md-1"></ul>
173dcc812bSGreg Roach    </div>
183dcc812bSGreg Roach</div>
193dcc812bSGreg Roach
203dcc812bSGreg Roach<?php View::push('styles') ?>
213dcc812bSGreg Roach<style>
223dcc812bSGreg Roach    .osm-wrapper, .osm-user-map {
23*0d123f04SDavid Drury        height: 70vh
243dcc812bSGreg Roach    }
253dcc812bSGreg Roach
263dcc812bSGreg Roach    .osm-sidebar {
273dcc812bSGreg Roach        height: 100%;
283dcc812bSGreg Roach        overflow-y: auto;
293dcc812bSGreg Roach        font-size: small;
303dcc812bSGreg Roach    }
313dcc812bSGreg Roach
323dcc812bSGreg Roach</style>
333dcc812bSGreg Roach<?php View::endpush() ?>
343dcc812bSGreg Roach
353dcc812bSGreg Roach<?php View::push('javascript') ?>
363dcc812bSGreg Roach<script type="application/javascript">
373dcc812bSGreg Roach  "use strict";
383dcc812bSGreg Roach
393dcc812bSGreg Roach  window.WT_OSM = (function() {
403dcc812bSGreg Roach    let baseData = {
413dcc812bSGreg Roach      minZoom: 2,
423dcc812bSGreg Roach      providerName: "OpenStreetMap.Mapnik",
433dcc812bSGreg Roach      providerOptions: [],
443dcc812bSGreg Roach    };
453dcc812bSGreg Roach
463dcc812bSGreg Roach    let map          = null;
473dcc812bSGreg Roach    let zoom         = null;
48*0d123f04SDavid Drury    let sidebar      = $('.osm-sidebar');
493dcc812bSGreg Roach    let markers      = L.markerClusterGroup({
503dcc812bSGreg Roach      showCoverageOnHover: false
513dcc812bSGreg Roach    });
523dcc812bSGreg Roach
533dcc812bSGreg Roach    let resetControl = L.Control.extend({
543dcc812bSGreg Roach      options: {
553dcc812bSGreg Roach        position: 'topleft'
563dcc812bSGreg Roach      },
573dcc812bSGreg Roach
583dcc812bSGreg Roach      onAdd: function (map) {
593dcc812bSGreg Roach        let container     = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
603dcc812bSGreg Roach        container.onclick = function () {
613dcc812bSGreg Roach          if (zoom) {
623dcc812bSGreg Roach            map.flyTo(markers.getBounds().getCenter(), zoom);
633dcc812bSGreg Roach          } else {
643dcc812bSGreg Roach            map.flyToBounds(markers.getBounds().pad(0.2));
653dcc812bSGreg Roach          }
6636409e74SGreg Roach          sidebar.scrollTo(sidebar.children(":first"));
6736409e74SGreg Roach
683dcc812bSGreg Roach          return false;
693dcc812bSGreg Roach        };
703dcc812bSGreg Roach        let anchor   = L.DomUtil.create('a', 'leaflet-control-reset', container);
71a4321966SGreg Roach        let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
723dcc812bSGreg Roach        anchor.href  = '#';
73a4321966SGreg Roach        anchor.title = reset;
743dcc812bSGreg Roach        anchor.role  = 'button';
753dcc812bSGreg Roach        $(anchor).attr('aria-label', 'reset');
763dcc812bSGreg Roach        let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
77a4321966SGreg Roach        image.alt = reset;
783dcc812bSGreg Roach
793dcc812bSGreg Roach        return container;
803dcc812bSGreg Roach      },
813dcc812bSGreg Roach    });
823dcc812bSGreg Roach
833dcc812bSGreg Roach    /**
843dcc812bSGreg Roach     *
853dcc812bSGreg Roach     * @private
863dcc812bSGreg Roach     */
873dcc812bSGreg Roach    let _drawMap = function () {
883dcc812bSGreg Roach      map = L.map('osm-map', {
893dcc812bSGreg Roach        center     : [0, 0],
903dcc812bSGreg Roach        minZoom    : baseData.minZoom, // maxZoom set by leaflet-providers.js
913dcc812bSGreg Roach        zoomControl: false, // remove default
923dcc812bSGreg Roach      });
933dcc812bSGreg Roach      L.tileLayer.provider(baseData.providerName, baseData.providerOptions).addTo(map);
943dcc812bSGreg Roach      L.control.zoom({ // Add zoom with localised text
95a4321966SGreg Roach        zoomInTitle : <?= json_encode(I18N::translate('Zoom in')) ?>,
96a4321966SGreg Roach        zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
973dcc812bSGreg Roach      }).addTo(map);
983dcc812bSGreg Roach    };
993dcc812bSGreg Roach
1003dcc812bSGreg Roach    /**
1013dcc812bSGreg Roach     * @param Generations
1023dcc812bSGreg Roach     * @private
1033dcc812bSGreg Roach     */
1049ec21bfdSGreg Roach    let _addLayer = function (Generations) {
1053dcc812bSGreg Roach        let geoJsonLayer;
106*0d123f04SDavid Drury        let sidebar_content = '';
1073dcc812bSGreg Roach
108353080f8SGreg Roach      $.getJSON(<?= json_encode(route('module', ['module' => 'pedigree-map', 'action' => 'MapData', 'tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>, {
109353080f8SGreg Roach        generations: Generations
1103dcc812bSGreg Roach      })
1113dcc812bSGreg Roach        .done(function (data, textStatus, jqXHR) {
1123dcc812bSGreg Roach          if (jqXHR.status === 200 && data.features.length === 1) {
1133dcc812bSGreg Roach            zoom = data.features[0].properties.zoom;
1143dcc812bSGreg Roach          }
1153dcc812bSGreg Roach          geoJsonLayer = L.geoJson(data, {
1163dcc812bSGreg Roach            pointToLayer : function (feature, latlng) {
1173dcc812bSGreg Roach              return new L.Marker(latlng, {
1183dcc812bSGreg Roach                icon : L.BeautifyIcon.icon({
1193130efd4SGreg Roach                  icon           : 'bullseye fas',
1203dcc812bSGreg Roach                  borderColor    : 'transparent',
1213130efd4SGreg Roach                  backgroundColor: feature.properties.iconcolor,
1223dcc812bSGreg Roach                  iconShape      : 'marker',
1233130efd4SGreg Roach                  textColor      : 'white',
1243dcc812bSGreg Roach                }),
1253dcc812bSGreg Roach                title: feature.properties.tooltip,
1263dcc812bSGreg Roach                alt  : feature.properties.tooltip,
1273dcc812bSGreg Roach                id   : feature.id
1283dcc812bSGreg Roach              })
1293dcc812bSGreg Roach                .on('popupopen', function (e) {
1303dcc812bSGreg Roach                  let item  = sidebar.children(".gchart[data-id=" + e.target.feature.id + "]");
1313dcc812bSGreg Roach                  item.addClass('messagebox');
1323dcc812bSGreg Roach                  sidebar.scrollTo(item);
1333dcc812bSGreg Roach                })
1343dcc812bSGreg Roach                .on('popupclose', function () {
135*0d123f04SDavid Drury                  sidebar.children(".gchart")
1363dcc812bSGreg Roach                    .removeClass('messagebox');
1373dcc812bSGreg Roach                });
1383dcc812bSGreg Roach            },
1393dcc812bSGreg Roach            onEachFeature: function (feature, layer) {
1403dcc812bSGreg Roach              if (feature.properties.polyline) {
1413dcc812bSGreg Roach                let pline = L.polyline(feature.properties.polyline.points, feature.properties.polyline.options);
1423dcc812bSGreg Roach                markers.addLayer(pline);
1433dcc812bSGreg Roach              }
1443dcc812bSGreg Roach              layer.bindPopup(feature.properties.summary);
145*0d123f04SDavid Drury              sidebar_content += `<li class="gchart px-md-2" data-id=${feature.id}>${feature.properties.summary}</li>`;
1463dcc812bSGreg Roach            },
1473dcc812bSGreg Roach          });
1483dcc812bSGreg Roach        })
1493dcc812bSGreg Roach        .fail(function (jqXHR, textStatus, errorThrown) {
1503dcc812bSGreg Roach          console.log(jqXHR, textStatus, errorThrown);
1513dcc812bSGreg Roach        })
1523dcc812bSGreg Roach        .always(function (data_jqXHR, textStatus, jqXHR_errorThrown) {
1533dcc812bSGreg Roach          switch (jqXHR_errorThrown.status) {
1543dcc812bSGreg Roach            case 200: // Success
155*0d123f04SDavid Drury              sidebar.append(sidebar_content);
1563dcc812bSGreg Roach              markers.addLayer(geoJsonLayer);
1573dcc812bSGreg Roach              map
1583dcc812bSGreg Roach                .addControl(new resetControl())
1593dcc812bSGreg Roach                .addLayer(markers)
1603dcc812bSGreg Roach                .fitBounds(markers.getBounds().pad(0.2));
1613dcc812bSGreg Roach              if (zoom) {
1623dcc812bSGreg Roach                map.setView(markers.getBounds().getCenter(), zoom);
1633dcc812bSGreg Roach              }
1643dcc812bSGreg Roach              break;
1653dcc812bSGreg Roach            case 204: // No data
1663dcc812bSGreg Roach              map.fitWorld();
167*0d123f04SDavid Drury              sidebar.append('<div class="bg-info text-white">' + <?= json_encode(I18N::translate('No mappable items')) ?> + '</div>');
1683dcc812bSGreg Roach              break;
1693dcc812bSGreg Roach            default: // Anything else
1703dcc812bSGreg Roach              map.fitWorld();
171*0d123f04SDavid Drury              sidebar.append('<div class="bg-danger text-white">' + <?= json_encode(I18N::translate('An unknown error occurred')) ?> + '</div>');
1723dcc812bSGreg Roach          }
1733dcc812bSGreg Roach        });
1743dcc812bSGreg Roach    };
1753dcc812bSGreg Roach
1763dcc812bSGreg Roach    /**
1773dcc812bSGreg Roach     *
1783dcc812bSGreg Roach     * @param elem
1793dcc812bSGreg Roach     * @returns {$}
1803dcc812bSGreg Roach     */
1813dcc812bSGreg Roach
1823dcc812bSGreg Roach    $.fn.scrollTo = function (elem) {
1833dcc812bSGreg Roach      let _this = $(this);
1843dcc812bSGreg Roach      _this.animate({
1853dcc812bSGreg Roach        scrollTop: elem.offset().top - _this.offset().top + _this.scrollTop()
1863dcc812bSGreg Roach      });
1873dcc812bSGreg Roach      return this;
1883dcc812bSGreg Roach    };
1893dcc812bSGreg Roach
1903dcc812bSGreg Roach    /**
1913dcc812bSGreg Roach     * @param generations integer
1923dcc812bSGreg Roach     * @private
1933dcc812bSGreg Roach     */
1949ec21bfdSGreg Roach    let _initialize = function (generations) {
1953dcc812bSGreg Roach      // Activate marker popup when sidebar entry clicked
1963dcc812bSGreg Roach      $(function () {
197*0d123f04SDavid Drury        sidebar
1983dcc812bSGreg Roach        // open marker popup if sidebar event is clicked
1993dcc812bSGreg Roach          .on('click', '.gchart', function (e) {
2003dcc812bSGreg Roach            // first close any existing
2013dcc812bSGreg Roach            map.closePopup();
2023dcc812bSGreg Roach            let eventId = $(this).data('id');
2033dcc812bSGreg Roach            //find the marker corresponding to the clicked event
2043dcc812bSGreg Roach            let mkrLayer = markers.getLayers().filter(function (v) {
2053dcc812bSGreg Roach              return typeof(v.feature) !== 'undefined' && v.feature.id === eventId;
2063dcc812bSGreg Roach            });
2073dcc812bSGreg Roach            let mkr = mkrLayer.pop();
2083dcc812bSGreg Roach            // Unfortunately zoomToShowLayer zooms to maxZoom
2093dcc812bSGreg Roach            // when all marker in a cluster have exactly the
2103dcc812bSGreg Roach            // same co-ordinates
2113dcc812bSGreg Roach            markers.zoomToShowLayer(mkr, function (e) {
2123dcc812bSGreg Roach              mkr.openPopup();
2133dcc812bSGreg Roach            });
2143dcc812bSGreg Roach            return false;
2153dcc812bSGreg Roach          })
2163dcc812bSGreg Roach          .on('click', 'a', function (e) { // stop click on a person also opening the popup
2173dcc812bSGreg Roach            e.stopPropagation();
2183dcc812bSGreg Roach          });
2193dcc812bSGreg Roach      });
2203dcc812bSGreg Roach
2213dcc812bSGreg Roach      _drawMap();
2229ec21bfdSGreg Roach      _addLayer(generations);
2233dcc812bSGreg Roach    };
2243dcc812bSGreg Roach
2253dcc812bSGreg Roach    return {
2263dcc812bSGreg Roach      /**
2273dcc812bSGreg Roach       * @param generations integer
2283dcc812bSGreg Roach       */
2299ec21bfdSGreg Roach      drawMap: function (generations) {
2309ec21bfdSGreg Roach        _initialize(generations);
2313dcc812bSGreg Roach      }
2323dcc812bSGreg Roach    };
2333dcc812bSGreg Roach  })();
2343dcc812bSGreg Roach
2359ec21bfdSGreg Roach    WT_OSM.drawMap(<?= json_encode($generations) ?>);
2363dcc812bSGreg Roach</script>
2373dcc812bSGreg Roach<?php View::endpush() ?>
238