xref: /webtrees/resources/views/modules/pedigree-map/chart.phtml (revision 3130efd4be6e9b2e18f714022643e7ed3f5dd8d1)
19ec21bfdSGreg Roach<?php
29ec21bfdSGreg Roachuse Fisharebest\Webtrees\I18N;
39ec21bfdSGreg Roachuse Fisharebest\Webtrees\Individual;
49ec21bfdSGreg Roachuse Fisharebest\Webtrees\View;
59ec21bfdSGreg Roach
69ec21bfdSGreg Roach/**
79ec21bfdSGreg Roach * @var Individual $individual
89ec21bfdSGreg Roach */
99ec21bfdSGreg Roach?>
109ec21bfdSGreg Roach
113dcc812bSGreg Roach
123dcc812bSGreg Roach<div class="py-4">
133dcc812bSGreg Roach    <div class="row gchart osm-wrapper">
14a2e602dbSGreg Roach        <div id="osm-map" class="col-sm-9 wt-ajax-load osm-user-map" dir="ltr"></div>
158fca99e0SGreg Roach        <ul class="col-sm-3 osm-sidebar wt-page-options-value list-unstyled p-0"></ul>
163dcc812bSGreg Roach    </div>
173dcc812bSGreg Roach</div>
183dcc812bSGreg Roach
193dcc812bSGreg Roach<?php View::push('styles') ?>
203dcc812bSGreg Roach<style>
213dcc812bSGreg Roach    .osm-wrapper, .osm-user-map {
228fca99e0SGreg Roach        height: 75vh
233dcc812bSGreg Roach    }
243dcc812bSGreg Roach
253dcc812bSGreg Roach    .osm-sidebar {
263dcc812bSGreg Roach        height: 100%;
273dcc812bSGreg Roach        overflow-y: auto;
283dcc812bSGreg Roach        padding: 0;
293dcc812bSGreg Roach        margin: 0;
303dcc812bSGreg Roach        border: 0;
313dcc812bSGreg Roach        display: none;
323dcc812bSGreg Roach        font-size: small;
333dcc812bSGreg Roach    }
343dcc812bSGreg Roach
353dcc812bSGreg Roach    .osm-sidebar .gchart {
363dcc812bSGreg Roach        margin: 1px;
373dcc812bSGreg Roach        padding: 2px
383dcc812bSGreg Roach    }
393dcc812bSGreg Roach
403dcc812bSGreg Roach    .osm-sidebar .gchart img {
413dcc812bSGreg Roach        height: 15px;
423dcc812bSGreg Roach        width: 25px
433dcc812bSGreg Roach    }
443dcc812bSGreg Roach</style>
453dcc812bSGreg Roach<?php View::endpush() ?>
463dcc812bSGreg Roach
473dcc812bSGreg Roach<?php View::push('javascript') ?>
483dcc812bSGreg Roach<script type="application/javascript">
493dcc812bSGreg Roach  "use strict";
503dcc812bSGreg Roach
513dcc812bSGreg Roach  window.WT_OSM = (function() {
523dcc812bSGreg Roach    let baseData = {
533dcc812bSGreg Roach      minZoom: 2,
543dcc812bSGreg Roach      providerName: "OpenStreetMap.Mapnik",
553dcc812bSGreg Roach      providerOptions: [],
563dcc812bSGreg Roach    };
573dcc812bSGreg Roach
583dcc812bSGreg Roach    let map          = null;
593dcc812bSGreg Roach    let zoom         = null;
603dcc812bSGreg Roach    let markers      = L.markerClusterGroup({
613dcc812bSGreg Roach      showCoverageOnHover: false
623dcc812bSGreg Roach    });
633dcc812bSGreg Roach
643dcc812bSGreg Roach    let resetControl = L.Control.extend({
653dcc812bSGreg Roach      options: {
663dcc812bSGreg Roach        position: 'topleft'
673dcc812bSGreg Roach      },
683dcc812bSGreg Roach
693dcc812bSGreg Roach      onAdd: function (map) {
703dcc812bSGreg Roach        let container     = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
713dcc812bSGreg Roach        container.onclick = function () {
723dcc812bSGreg Roach          if (zoom) {
733dcc812bSGreg Roach            map.flyTo(markers.getBounds().getCenter(), zoom);
743dcc812bSGreg Roach          } else {
753dcc812bSGreg Roach            map.flyToBounds(markers.getBounds().pad(0.2));
763dcc812bSGreg Roach          }
7736409e74SGreg Roach          let sidebar = $(".osm-sidebar");
7836409e74SGreg Roach          sidebar.scrollTo(sidebar.children(":first"));
7936409e74SGreg Roach
803dcc812bSGreg Roach          return false;
813dcc812bSGreg Roach        };
823dcc812bSGreg Roach        let anchor   = L.DomUtil.create('a', 'leaflet-control-reset', container);
83a4321966SGreg Roach        let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
843dcc812bSGreg Roach        anchor.href  = '#';
85a4321966SGreg Roach        anchor.title = reset;
863dcc812bSGreg Roach        anchor.role  = 'button';
873dcc812bSGreg Roach        $(anchor).attr('aria-label', 'reset');
883dcc812bSGreg Roach        let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
89a4321966SGreg Roach        image.alt = reset;
903dcc812bSGreg Roach
913dcc812bSGreg Roach        return container;
923dcc812bSGreg Roach      },
933dcc812bSGreg Roach    });
943dcc812bSGreg Roach
953dcc812bSGreg Roach    /**
963dcc812bSGreg Roach     *
973dcc812bSGreg Roach     * @private
983dcc812bSGreg Roach     */
993dcc812bSGreg Roach    let _drawMap = function () {
1003dcc812bSGreg Roach      map = L.map('osm-map', {
1013dcc812bSGreg Roach        center     : [0, 0],
1023dcc812bSGreg Roach        minZoom    : baseData.minZoom, // maxZoom set by leaflet-providers.js
1033dcc812bSGreg Roach        zoomControl: false, // remove default
1043dcc812bSGreg Roach      });
1053dcc812bSGreg Roach      L.tileLayer.provider(baseData.providerName, baseData.providerOptions).addTo(map);
1063dcc812bSGreg Roach      L.control.zoom({ // Add zoom with localised text
107a4321966SGreg Roach        zoomInTitle : <?= json_encode(I18N::translate('Zoom in')) ?>,
108a4321966SGreg Roach        zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
1093dcc812bSGreg Roach      }).addTo(map);
1103dcc812bSGreg Roach    };
1113dcc812bSGreg Roach
1123dcc812bSGreg Roach    /**
1133dcc812bSGreg Roach     * @param Generations
1143dcc812bSGreg Roach     * @private
1153dcc812bSGreg Roach     */
1169ec21bfdSGreg Roach    let _addLayer = function (Generations) {
1173dcc812bSGreg Roach      let geoJsonLayer;
1183dcc812bSGreg Roach      let domObj  = '.osm-sidebar';
1193dcc812bSGreg Roach      let sidebar = '';
1203dcc812bSGreg Roach
121353080f8SGreg Roach      $.getJSON(<?= json_encode(route('module', ['module' => 'pedigree-map', 'action' => 'MapData', 'tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>, {
122353080f8SGreg Roach        generations: Generations
1233dcc812bSGreg Roach      })
1243dcc812bSGreg Roach        .done(function (data, textStatus, jqXHR) {
1253dcc812bSGreg Roach          if (jqXHR.status === 200 && data.features.length === 1) {
1263dcc812bSGreg Roach            zoom = data.features[0].properties.zoom;
1273dcc812bSGreg Roach          }
1283dcc812bSGreg Roach          geoJsonLayer = L.geoJson(data, {
1293dcc812bSGreg Roach            pointToLayer : function (feature, latlng) {
1303dcc812bSGreg Roach              return new L.Marker(latlng, {
1313dcc812bSGreg Roach                icon : L.BeautifyIcon.icon({
132*3130efd4SGreg Roach                  icon           : 'bullseye fas',
1333dcc812bSGreg Roach                  borderColor    : 'transparent',
134*3130efd4SGreg Roach                  backgroundColor: feature.properties.iconcolor,
1353dcc812bSGreg Roach                  iconShape      : 'marker',
136*3130efd4SGreg Roach                  textColor      : 'white',
1373dcc812bSGreg Roach                }),
1383dcc812bSGreg Roach                title: feature.properties.tooltip,
1393dcc812bSGreg Roach                alt  : feature.properties.tooltip,
1403dcc812bSGreg Roach                id   : feature.id
1413dcc812bSGreg Roach              })
1423dcc812bSGreg Roach                .on('popupopen', function (e) {
1433dcc812bSGreg Roach                  let sidebar = $('.osm-sidebar');
1443dcc812bSGreg Roach                  let item  = sidebar.children(".gchart[data-id=" + e.target.feature.id + "]");
1453dcc812bSGreg Roach                  item.addClass('messagebox');
1463dcc812bSGreg Roach                  sidebar.scrollTo(item);
1473dcc812bSGreg Roach                })
1483dcc812bSGreg Roach                .on('popupclose', function () {
1493dcc812bSGreg Roach                  $('.osm-sidebar').children(".gchart")
1503dcc812bSGreg Roach                    .removeClass('messagebox');
1513dcc812bSGreg Roach                });
1523dcc812bSGreg Roach            },
1533dcc812bSGreg Roach            onEachFeature: function (feature, layer) {
1543dcc812bSGreg Roach              if (feature.properties.polyline) {
1553dcc812bSGreg Roach                let pline = L.polyline(feature.properties.polyline.points, feature.properties.polyline.options);
1563dcc812bSGreg Roach                markers.addLayer(pline);
1573dcc812bSGreg Roach              }
1583dcc812bSGreg Roach              layer.bindPopup(feature.properties.summary);
1594b082fd8SGreg Roach              sidebar += `<li class="gchart" data-id=${feature.id}>${feature.properties.summary}</li>`;
1603dcc812bSGreg Roach            },
1613dcc812bSGreg Roach          });
1623dcc812bSGreg Roach        })
1633dcc812bSGreg Roach        .fail(function (jqXHR, textStatus, errorThrown) {
1643dcc812bSGreg Roach          console.log(jqXHR, textStatus, errorThrown);
1653dcc812bSGreg Roach        })
1663dcc812bSGreg Roach        .always(function (data_jqXHR, textStatus, jqXHR_errorThrown) {
1673dcc812bSGreg Roach          switch (jqXHR_errorThrown.status) {
1683dcc812bSGreg Roach            case 200: // Success
1693dcc812bSGreg Roach              $(domObj).append(sidebar);
1703dcc812bSGreg Roach              markers.addLayer(geoJsonLayer);
1713dcc812bSGreg Roach              map
1723dcc812bSGreg Roach                .addControl(new resetControl())
1733dcc812bSGreg Roach                .addLayer(markers)
1743dcc812bSGreg Roach                .fitBounds(markers.getBounds().pad(0.2));
1753dcc812bSGreg Roach              if (zoom) {
1763dcc812bSGreg Roach                map.setView(markers.getBounds().getCenter(), zoom);
1773dcc812bSGreg Roach              }
1783dcc812bSGreg Roach              break;
1793dcc812bSGreg Roach            case 204: // No data
1803dcc812bSGreg Roach              map.fitWorld();
181a4321966SGreg Roach              $(domObj).append('<div class="bg-info text-white">' + <?= json_encode(I18N::translate('No mappable items')) ?> + '</div>');
1823dcc812bSGreg Roach              break;
1833dcc812bSGreg Roach            default: // Anything else
1843dcc812bSGreg Roach              map.fitWorld();
185a4321966SGreg Roach              $(domObj).append('<div class="bg-danger text-white">' + <?= json_encode(I18N::translate('An unknown error occurred')) ?> + '</div>');
1863dcc812bSGreg Roach          }
1873dcc812bSGreg Roach          $(domObj).slideDown(300);
1883dcc812bSGreg Roach        });
1893dcc812bSGreg Roach    };
1903dcc812bSGreg Roach
1913dcc812bSGreg Roach    /**
1923dcc812bSGreg Roach     *
1933dcc812bSGreg Roach     * @param elem
1943dcc812bSGreg Roach     * @returns {$}
1953dcc812bSGreg Roach     */
1963dcc812bSGreg Roach
1973dcc812bSGreg Roach    $.fn.scrollTo = function (elem) {
1983dcc812bSGreg Roach      let _this = $(this);
1993dcc812bSGreg Roach      _this.animate({
2003dcc812bSGreg Roach        scrollTop: elem.offset().top - _this.offset().top + _this.scrollTop()
2013dcc812bSGreg Roach      });
2023dcc812bSGreg Roach      return this;
2033dcc812bSGreg Roach    };
2043dcc812bSGreg Roach
2053dcc812bSGreg Roach    /**
2063dcc812bSGreg Roach     * @param generations integer
2073dcc812bSGreg Roach     * @private
2083dcc812bSGreg Roach     */
2099ec21bfdSGreg Roach    let _initialize = function (generations) {
2103dcc812bSGreg Roach      // Activate marker popup when sidebar entry clicked
2113dcc812bSGreg Roach      $(function () {
2123dcc812bSGreg Roach        $('.osm-sidebar')
2133dcc812bSGreg Roach        // open marker popup if sidebar event is clicked
2143dcc812bSGreg Roach          .on('click', '.gchart', function (e) {
2153dcc812bSGreg Roach            // first close any existing
2163dcc812bSGreg Roach            map.closePopup();
2173dcc812bSGreg Roach            let eventId = $(this).data('id');
2183dcc812bSGreg Roach            //find the marker corresponding to the clicked event
2193dcc812bSGreg Roach            let mkrLayer = markers.getLayers().filter(function (v) {
2203dcc812bSGreg Roach              return typeof(v.feature) !== 'undefined' && v.feature.id === eventId;
2213dcc812bSGreg Roach            });
2223dcc812bSGreg Roach            let mkr = mkrLayer.pop();
2233dcc812bSGreg Roach            // Unfortunately zoomToShowLayer zooms to maxZoom
2243dcc812bSGreg Roach            // when all marker in a cluster have exactly the
2253dcc812bSGreg Roach            // same co-ordinates
2263dcc812bSGreg Roach            markers.zoomToShowLayer(mkr, function (e) {
2273dcc812bSGreg Roach              mkr.openPopup();
2283dcc812bSGreg Roach            });
2293dcc812bSGreg Roach            return false;
2303dcc812bSGreg Roach          })
2313dcc812bSGreg Roach          .on('click', 'a', function (e) { // stop click on a person also opening the popup
2323dcc812bSGreg Roach            e.stopPropagation();
2333dcc812bSGreg Roach          });
2343dcc812bSGreg Roach      });
2353dcc812bSGreg Roach
2363dcc812bSGreg Roach      _drawMap();
2379ec21bfdSGreg Roach      _addLayer(generations);
2383dcc812bSGreg Roach    };
2393dcc812bSGreg Roach
2403dcc812bSGreg Roach    return {
2413dcc812bSGreg Roach      /**
2423dcc812bSGreg Roach       * @param generations integer
2433dcc812bSGreg Roach       */
2449ec21bfdSGreg Roach      drawMap: function (generations) {
2459ec21bfdSGreg Roach        _initialize(generations);
2463dcc812bSGreg Roach      }
2473dcc812bSGreg Roach    };
2483dcc812bSGreg Roach  })();
2493dcc812bSGreg Roach
2509ec21bfdSGreg Roach    WT_OSM.drawMap(<?= json_encode($generations) ?>);
2513dcc812bSGreg Roach</script>
2523dcc812bSGreg Roach<?php View::endpush() ?>
253