Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ddrury committed Feb 16, 2022
1 parent cbf14da commit 9a1386f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
13 changes: 6 additions & 7 deletions app/Module/PedigreeMapModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ private function getMapData(ServerRequestInterface $request): array
];

$sosa_points = [];
$lines = [];

foreach ($facts as $sosa => $fact) {
$location = new PlaceLocation($fact->place()->gedcomName());
Expand All @@ -257,16 +258,12 @@ private function getMapData(ServerRequestInterface $request): array
}

if ($latitude !== null && $longitude !== null) {
$polyline = null;
$sosa_points[$sosa] = [$latitude, $longitude];
$sosa_child = intdiv($sosa, 2);
$color = 'var(--wt-pedigree-map-gen-' . $sosa_child % self::GENERATION_COLORS . ')';

if (array_key_exists($sosa_child, $sosa_points)) {
// Would like to use a GeometryCollection to hold LineStrings
// rather than generate polylines but the MarkerCluster library
// doesn't seem to like them
$polyline = [
$lines[] = [
'points' => [
$sosa_points[$sosa_child],
[$latitude, $longitude],
Expand All @@ -284,7 +281,6 @@ private function getMapData(ServerRequestInterface $request): array
'coordinates' => [$longitude, $latitude],
],
'properties' => [
'polyline' => $polyline,
'iconcolor' => $color,
'tooltip' => $fact->place()->gedcomName(),
'summary' => view('modules/pedigree-map/events', [
Expand All @@ -297,7 +293,10 @@ private function getMapData(ServerRequestInterface $request): array
}
}

return $geojson;
return [
'geoJSON' => $geojson,
'polylines' => $lines,
];
}

/**
Expand Down
22 changes: 11 additions & 11 deletions resources/views/modules/pedigree-map/chart.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use Fisharebest\Webtrees\View;

(function () {
const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>;
const geoJson_data = <?= json_encode($data, JSON_THROW_ON_ERROR) ?>;
const geoJson_data = <?= json_encode($data['geoJSON'], JSON_THROW_ON_ERROR) ?>;
const polylines = <?= json_encode($data['polylines'], JSON_THROW_ON_ERROR) ?>;
const sidebar = document.querySelector('.wt-pedigree-map-sidebar');

const scrollOptions = {
Expand All @@ -42,18 +43,17 @@ use Fisharebest\Webtrees\View;
tree: null
}

geoJson_data.features.forEach((feature) => {
if (feature.properties.polyline) {
features.layer.addLayer(L.polyline(feature.properties.polyline.points, feature.properties.polyline.options));
if (!features.tree) {
features.tree = {
label: '<?= I18N::translate("Show/Hide parent to child lines") ?>',
layer: features.layer
}
}
}
polylines.forEach((line) => {
features.layer.addLayer(L.polyline(line.points, line.options));
});

if (polylines.length) {
features.tree = {
label: '<?= I18N::translate("Show/Hide parent to child lines") ?>',
layer: features.layer
}
}

/**
* Passed to resetControl to
* perform necessary reset actions on map
Expand Down

0 comments on commit 9a1386f

Please sign in to comment.