Skip to content

Commit

Permalink
Merge pull request #119 from OneBusAway/refactor/clear-markers-on-tri…
Browse files Browse the repository at this point in the history
…p-planner-mode-with-state-enhancement

Refactor/clear-markers-on-trip-planner-mode-with-state-enhancement
  • Loading branch information
aaronbrethorst authored Nov 14, 2024
2 parents c889eff + 1742745 commit 255db5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 46 deletions.
64 changes: 19 additions & 45 deletions src/components/map/MapView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
export let selectedRoute = null;
export let showRoute = false;
export let showRouteMap = false;
export let stop = null;
export let mapProvider = null;
let isTripPlanMoodActive = false;
let isTripPlanModeActive = false;
let selectedStopID = null;
let mapInstance = null;
let mapElement;
let markers = [];
Expand Down Expand Up @@ -99,8 +97,8 @@
const center = mapInstance.getCenter();
const zoomLevel = mapInstance.map.getZoom();
// Prevent fetching stops when a route is selected, we only fetch stops when we are see other stops
if (selectedRoute || showRoute) {
// Prevent fetching stops in the background when a route is selected or trip plan mode is active, we only fetch stops when we are see other stops
if (selectedRoute || showRoute || isTripPlanModeActive) {
return;
}
await loadStopsAndAddMarkers(center.lat, center.lng, false, zoomLevel);
Expand Down Expand Up @@ -129,15 +127,6 @@
});
allStops = [...new Map([...allStops, ...newStops].map((stop) => [stop.id, stop])).values()];
if (selectedRoute && !showRoute) {
allStops = [];
} else if (showRoute && selectedRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else {
newStops.forEach((s) => addMarker(s));
}
}
function clearAllMarkers() {
Expand All @@ -147,42 +136,28 @@
markers = [];
}
$: if (selectedRoute && !showRoute) {
clearAllMarkers();
allStops = [];
}
$: if (stop && mapInstance) {
// TODO: make sure that these markers are deduped. i.e. we shouldn't
// show the same stop twice on the map
if (stop.id !== selectedStopID) {
addMarker(stop);
function updateMarkers() {
if (showRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else if (!selectedRoute && !isTripPlanModeActive) {
allStops.forEach((s) => addMarker(s));
}
}
$: if (selectedRoute && showRoute) {
clearAllMarkers();
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
}
// If no route is selected and not showing a route, add all markers
// This ensures markers are re-added after a route is deselected
$: if (!selectedRoute && !showRoute) {
allStops.forEach((s) => addMarker(s));
}
// TODO: prevent fetch stops-for-location if the trip planner mode is on - we should do this after merge.
$: {
if (isTripPlanMoodActive) {
if (selectedRoute) {
clearAllMarkers();
} else {
if (!selectedRoute || !showRoute) {
allStops.forEach((s) => addMarker(s));
}
updateMarkers();
} else if (!isTripPlanModeActive) {
allStops.forEach((s) => addMarker(s));
}
}
$: if (isTripPlanModeActive) {
clearAllMarkers();
}
function addMarker(s) {
if (!mapInstance) {
console.error('Map not initialized yet');
Expand Down Expand Up @@ -213,7 +188,6 @@
icon: icon,
stop: s,
onClick: () => {
selectedStopID = s.id;
dispatch('stopSelected', { stop: s });
}
});
Expand All @@ -239,10 +213,10 @@
if (browser) {
const darkMode = document.documentElement.classList.contains('dark');
window.addEventListener('planTripTabClicked', () => {
isTripPlanMoodActive = true;
isTripPlanModeActive = true;
});
window.addEventListener('tabSwitched', () => {
isTripPlanMoodActive = false;
isTripPlanModeActive = false;
});
const event = new CustomEvent('themeChange', { detail: { darkMode } });
window.dispatchEvent(event);
Expand Down
1 change: 0 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@
on:stopSelected={stopSelected}
{showRoute}
{showRouteMap}
{stop}
bind:mapProvider
/>
{/if}

0 comments on commit 255db5d

Please sign in to comment.