xref: /webtrees/resources/views/modules/pedigree-map/chart.phtml (revision 36409e74031e7511ee5eb54dedf4f1503b5937b5)
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
453dcc812bSGreg Roach    .osm-sidebar .border-danger:hover {
463dcc812bSGreg Roach        cursor: not-allowed
473dcc812bSGreg Roach    }
483dcc812bSGreg Roach</style>
493dcc812bSGreg Roach<?php View::endpush() ?>
503dcc812bSGreg Roach
513dcc812bSGreg Roach<?php View::push('javascript') ?>
523dcc812bSGreg Roach<script type="application/javascript">
533dcc812bSGreg Roach  "use strict";
543dcc812bSGreg Roach
553dcc812bSGreg Roach  window.WT_OSM = (function() {
563dcc812bSGreg Roach    let baseData = {
573dcc812bSGreg Roach      minZoom: 2,
583dcc812bSGreg Roach      providerName: "OpenStreetMap.Mapnik",
593dcc812bSGreg Roach      providerOptions: [],
603dcc812bSGreg Roach    };
613dcc812bSGreg Roach
623dcc812bSGreg Roach    let map          = null;
633dcc812bSGreg Roach    let zoom         = null;
643dcc812bSGreg Roach    let markers      = L.markerClusterGroup({
653dcc812bSGreg Roach      showCoverageOnHover: false
663dcc812bSGreg Roach    });
673dcc812bSGreg Roach
683dcc812bSGreg Roach    let resetControl = L.Control.extend({
693dcc812bSGreg Roach      options: {
703dcc812bSGreg Roach        position: 'topleft'
713dcc812bSGreg Roach      },
723dcc812bSGreg Roach
733dcc812bSGreg Roach      onAdd: function (map) {
743dcc812bSGreg Roach        let container     = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
753dcc812bSGreg Roach        container.onclick = function () {
763dcc812bSGreg Roach          if (zoom) {
773dcc812bSGreg Roach            map.flyTo(markers.getBounds().getCenter(), zoom);
783dcc812bSGreg Roach          } else {
793dcc812bSGreg Roach            map.flyToBounds(markers.getBounds().pad(0.2));
803dcc812bSGreg Roach          }
81*36409e74SGreg Roach          let sidebar = $(".osm-sidebar");
82*36409e74SGreg Roach          sidebar.scrollTo(sidebar.children(":first"));
83*36409e74SGreg Roach
843dcc812bSGreg Roach          return false;
853dcc812bSGreg Roach        };
863dcc812bSGreg Roach        let anchor   = L.DomUtil.create('a', 'leaflet-control-reset', container);
87a4321966SGreg Roach        let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
883dcc812bSGreg Roach        anchor.href  = '#';
89a4321966SGreg Roach        anchor.title = reset;
903dcc812bSGreg Roach        anchor.role  = 'button';
913dcc812bSGreg Roach        $(anchor).attr('aria-label', 'reset');
923dcc812bSGreg Roach        let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
93a4321966SGreg Roach        image.alt = reset;
943dcc812bSGreg Roach
953dcc812bSGreg Roach        return container;
963dcc812bSGreg Roach      },
973dcc812bSGreg Roach    });
983dcc812bSGreg Roach
993dcc812bSGreg Roach    /**
1003dcc812bSGreg Roach     *
1013dcc812bSGreg Roach     * @private
1023dcc812bSGreg Roach     */
1033dcc812bSGreg Roach    let _drawMap = function () {
1043dcc812bSGreg Roach      map = L.map('osm-map', {
1053dcc812bSGreg Roach        center     : [0, 0],
1063dcc812bSGreg Roach        minZoom    : baseData.minZoom, // maxZoom set by leaflet-providers.js
1073dcc812bSGreg Roach        zoomControl: false, // remove default
1083dcc812bSGreg Roach      });
1093dcc812bSGreg Roach      L.tileLayer.provider(baseData.providerName, baseData.providerOptions).addTo(map);
1103dcc812bSGreg Roach      L.control.zoom({ // Add zoom with localised text
111a4321966SGreg Roach        zoomInTitle : <?= json_encode(I18N::translate('Zoom in')) ?>,
112a4321966SGreg Roach        zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
1133dcc812bSGreg Roach      }).addTo(map);
1143dcc812bSGreg Roach    };
1153dcc812bSGreg Roach
1163dcc812bSGreg Roach    /**
1173dcc812bSGreg Roach     * @param Generations
1183dcc812bSGreg Roach     * @private
1193dcc812bSGreg Roach     */
1209ec21bfdSGreg Roach    let _addLayer = function (Generations) {
1213dcc812bSGreg Roach      let geoJsonLayer;
1223dcc812bSGreg Roach      let domObj  = '.osm-sidebar';
1233dcc812bSGreg Roach      let sidebar = '';
1243dcc812bSGreg Roach
125353080f8SGreg Roach      $.getJSON(<?= json_encode(route('module', ['module' => 'pedigree-map', 'action' => 'MapData', 'tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>, {
126353080f8SGreg Roach        generations: Generations
1273dcc812bSGreg Roach      })
1283dcc812bSGreg Roach        .done(function (data, textStatus, jqXHR) {
1293dcc812bSGreg Roach          if (jqXHR.status === 200 && data.features.length === 1) {
1303dcc812bSGreg Roach            zoom = data.features[0].properties.zoom;
1313dcc812bSGreg Roach          }
1323dcc812bSGreg Roach          geoJsonLayer = L.geoJson(data, {
1333dcc812bSGreg Roach            pointToLayer : function (feature, latlng) {
1343dcc812bSGreg Roach              return new L.Marker(latlng, {
1353dcc812bSGreg Roach                icon : L.BeautifyIcon.icon({
1363dcc812bSGreg Roach                  icon           : feature.properties.icon['name'],
1373dcc812bSGreg Roach                  borderColor    : 'transparent',
1383dcc812bSGreg Roach                  backgroundColor: feature.valid ? feature.properties.icon['color'] : 'transparent',
1393dcc812bSGreg Roach                  iconShape      : 'marker',
1403dcc812bSGreg Roach                  textColor      : feature.valid ? 'white' : 'transparent',
1413dcc812bSGreg Roach                }),
1423dcc812bSGreg Roach                title: feature.properties.tooltip,
1433dcc812bSGreg Roach                alt  : feature.properties.tooltip,
1443dcc812bSGreg Roach                id   : feature.id
1453dcc812bSGreg Roach              })
1463dcc812bSGreg Roach                .on('popupopen', function (e) {
1473dcc812bSGreg Roach                  let sidebar = $('.osm-sidebar');
1483dcc812bSGreg Roach                  let item  = sidebar.children(".gchart[data-id=" + e.target.feature.id + "]");
1493dcc812bSGreg Roach                  item.addClass('messagebox');
1503dcc812bSGreg Roach                  sidebar.scrollTo(item);
1513dcc812bSGreg Roach                })
1523dcc812bSGreg Roach                .on('popupclose', function () {
1533dcc812bSGreg Roach                  $('.osm-sidebar').children(".gchart")
1543dcc812bSGreg Roach                    .removeClass('messagebox');
1553dcc812bSGreg Roach                });
1563dcc812bSGreg Roach            },
1573dcc812bSGreg Roach            onEachFeature: function (feature, layer) {
1583dcc812bSGreg Roach              if (feature.properties.polyline) {
1593dcc812bSGreg Roach                let pline = L.polyline(feature.properties.polyline.points, feature.properties.polyline.options);
1603dcc812bSGreg Roach                markers.addLayer(pline);
1613dcc812bSGreg Roach              }
1623dcc812bSGreg Roach              layer.bindPopup(feature.properties.summary);
1633dcc812bSGreg Roach              let myclass = feature.valid ? 'gchart' : 'border border-danger';
1643dcc812bSGreg Roach              sidebar += `<li class="${myclass}" data-id=${feature.id}>${feature.properties.summary}</li>`;
1653dcc812bSGreg Roach            },
1663dcc812bSGreg Roach          });
1673dcc812bSGreg Roach        })
1683dcc812bSGreg Roach        .fail(function (jqXHR, textStatus, errorThrown) {
1693dcc812bSGreg Roach          console.log(jqXHR, textStatus, errorThrown);
1703dcc812bSGreg Roach        })
1713dcc812bSGreg Roach        .always(function (data_jqXHR, textStatus, jqXHR_errorThrown) {
1723dcc812bSGreg Roach          switch (jqXHR_errorThrown.status) {
1733dcc812bSGreg Roach            case 200: // Success
1743dcc812bSGreg Roach              $(domObj).append(sidebar);
1753dcc812bSGreg Roach              markers.addLayer(geoJsonLayer);
1763dcc812bSGreg Roach              map
1773dcc812bSGreg Roach                .addControl(new resetControl())
1783dcc812bSGreg Roach                .addLayer(markers)
1793dcc812bSGreg Roach                .fitBounds(markers.getBounds().pad(0.2));
1803dcc812bSGreg Roach              if (zoom) {
1813dcc812bSGreg Roach                map.setView(markers.getBounds().getCenter(), zoom);
1823dcc812bSGreg Roach              }
1833dcc812bSGreg Roach              break;
1843dcc812bSGreg Roach            case 204: // No data
1853dcc812bSGreg Roach              map.fitWorld();
186a4321966SGreg Roach              $(domObj).append('<div class="bg-info text-white">' + <?= json_encode(I18N::translate('No mappable items')) ?> + '</div>');
1873dcc812bSGreg Roach              break;
1883dcc812bSGreg Roach            default: // Anything else
1893dcc812bSGreg Roach              map.fitWorld();
190a4321966SGreg Roach              $(domObj).append('<div class="bg-danger text-white">' + <?= json_encode(I18N::translate('An unknown error occurred')) ?> + '</div>');
1913dcc812bSGreg Roach          }
1923dcc812bSGreg Roach          $(domObj).slideDown(300);
1933dcc812bSGreg Roach        });
1943dcc812bSGreg Roach    };
1953dcc812bSGreg Roach
1963dcc812bSGreg Roach    /**
1973dcc812bSGreg Roach     *
1983dcc812bSGreg Roach     * @param elem
1993dcc812bSGreg Roach     * @returns {$}
2003dcc812bSGreg Roach     */
2013dcc812bSGreg Roach
2023dcc812bSGreg Roach    $.fn.scrollTo = function (elem) {
2033dcc812bSGreg Roach      let _this = $(this);
2043dcc812bSGreg Roach      _this.animate({
2053dcc812bSGreg Roach        scrollTop: elem.offset().top - _this.offset().top + _this.scrollTop()
2063dcc812bSGreg Roach      });
2073dcc812bSGreg Roach      return this;
2083dcc812bSGreg Roach    };
2093dcc812bSGreg Roach
2103dcc812bSGreg Roach    /**
2113dcc812bSGreg Roach     * @param generations integer
2123dcc812bSGreg Roach     * @private
2133dcc812bSGreg Roach     */
2149ec21bfdSGreg Roach    let _initialize = function (generations) {
2153dcc812bSGreg Roach      // Activate marker popup when sidebar entry clicked
2163dcc812bSGreg Roach      $(function () {
2173dcc812bSGreg Roach        $('.osm-sidebar')
2183dcc812bSGreg Roach        // open marker popup if sidebar event is clicked
2193dcc812bSGreg Roach          .on('click', '.gchart', function (e) {
2203dcc812bSGreg Roach            // first close any existing
2213dcc812bSGreg Roach            map.closePopup();
2223dcc812bSGreg Roach            let eventId = $(this).data('id');
2233dcc812bSGreg Roach            //find the marker corresponding to the clicked event
2243dcc812bSGreg Roach            let mkrLayer = markers.getLayers().filter(function (v) {
2253dcc812bSGreg Roach              return typeof(v.feature) !== 'undefined' && v.feature.id === eventId;
2263dcc812bSGreg Roach            });
2273dcc812bSGreg Roach            let mkr = mkrLayer.pop();
2283dcc812bSGreg Roach            // Unfortunately zoomToShowLayer zooms to maxZoom
2293dcc812bSGreg Roach            // when all marker in a cluster have exactly the
2303dcc812bSGreg Roach            // same co-ordinates
2313dcc812bSGreg Roach            markers.zoomToShowLayer(mkr, function (e) {
2323dcc812bSGreg Roach              mkr.openPopup();
2333dcc812bSGreg Roach            });
2343dcc812bSGreg Roach            return false;
2353dcc812bSGreg Roach          })
2363dcc812bSGreg Roach          .on('click', 'a', function (e) { // stop click on a person also opening the popup
2373dcc812bSGreg Roach            e.stopPropagation();
2383dcc812bSGreg Roach          });
2393dcc812bSGreg Roach      });
2403dcc812bSGreg Roach
2413dcc812bSGreg Roach      _drawMap();
2429ec21bfdSGreg Roach      _addLayer(generations);
2433dcc812bSGreg Roach    };
2443dcc812bSGreg Roach
2453dcc812bSGreg Roach    return {
2463dcc812bSGreg Roach      /**
2473dcc812bSGreg Roach       * @param generations integer
2483dcc812bSGreg Roach       */
2499ec21bfdSGreg Roach      drawMap: function (generations) {
2509ec21bfdSGreg Roach        _initialize(generations);
2513dcc812bSGreg Roach      }
2523dcc812bSGreg Roach    };
2533dcc812bSGreg Roach  })();
2543dcc812bSGreg Roach
2559ec21bfdSGreg Roach    WT_OSM.drawMap(<?= json_encode($generations) ?>);
2563dcc812bSGreg Roach</script>
2573dcc812bSGreg Roach<?php View::endpush() ?>
258