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