Files
gpx.studio/website/src/lib/components/toolbar/tools/routing/Routing.svelte

269 lines
11 KiB
Svelte
Raw Normal View History

2024-04-22 19:36:31 +02:00
<script lang="ts">
import * as Select from '$lib/components/ui/select';
import { Switch } from '$lib/components/ui/switch';
import { Label } from '$lib/components/ui/label/index.js';
import { Button } from '$lib/components/ui/button';
import Help from '$lib/components/Help.svelte';
import ButtonWithTooltip from '$lib/components/ButtonWithTooltip.svelte';
import Tooltip from '$lib/components/Tooltip.svelte';
import Shortcut from '$lib/components/Shortcut.svelte';
import {
Bike,
Footprints,
Waves,
TrainFront,
Route,
TriangleAlert,
ArrowRightLeft,
Home,
RouteOff,
Repeat,
SquareArrowUpLeft,
SquareArrowOutDownRight,
2025-06-21 21:07:36 +02:00
} from '@lucide/svelte';
2025-10-18 16:10:08 +02:00
import { brouterProfiles } from '$lib/components/toolbar/tools/routing/routing';
2025-06-21 21:07:36 +02:00
import { i18n } from '$lib/i18n.svelte';
import { slide } from 'svelte/transition';
import {
ListFileItem,
ListRootItem,
ListTrackItem,
ListTrackSegmentItem,
type ListItem,
2025-10-05 19:34:05 +02:00
} from '$lib/components/file-list/file-list';
2025-10-18 16:10:08 +02:00
import { getURLForLanguage } from '$lib/utils';
import { onDestroy, onMount } from 'svelte';
import { TrackPoint } from 'gpx';
2025-10-17 23:54:45 +02:00
import { settings } from '$lib/logic/settings';
import { map } from '$lib/components/map/map';
2025-10-18 16:10:08 +02:00
import { fileStateCollection, GPXFileStateCollectionObserver } from '$lib/logic/file-state';
2025-10-17 23:54:45 +02:00
import { selection } from '$lib/logic/selection';
import { fileActions, getFileIds, newGPXFile } from '$lib/logic/file-actions';
2025-10-18 16:10:08 +02:00
import { mapCursor, MapCursorState } from '$lib/logic/map-cursor';
2025-10-23 18:56:04 +02:00
import { RoutingControls, routingControls } from './routing-controls';
2025-10-05 19:34:05 +02:00
let {
minimized = $bindable(false),
minimizable = true,
popup = undefined,
popupElement = undefined,
class: className = '',
}: {
minimized?: boolean;
minimizable?: boolean;
popup?: mapboxgl.Popup;
popupElement?: HTMLDivElement;
class?: string;
} = $props();
2024-04-25 16:41:06 +02:00
2025-06-21 21:07:36 +02:00
const { privateRoads, routing, routingProfile } = settings;
2024-05-04 15:10:30 +02:00
2025-10-18 16:10:08 +02:00
let fileStateCollectionObserver: GPXFileStateCollectionObserver;
2024-04-26 13:33:17 +02:00
2025-10-05 19:34:05 +02:00
let validSelection = $derived(
2025-10-17 23:54:45 +02:00
$selection.hasAnyChildren(new ListRootItem(), true, ['waypoints'])
2025-10-05 19:34:05 +02:00
);
function createFileWithPoint(e: any) {
2025-10-17 23:54:45 +02:00
if ($selection.size === 0) {
let file = newGPXFile();
file.replaceTrackPoints(0, 0, 0, 0, [
new TrackPoint({
attributes: {
lat: e.lngLat.lat,
lon: e.lngLat.lng,
},
}),
]);
file._data.id = getFileIds(1)[0];
2025-10-05 19:34:05 +02:00
fileActions.add(file);
2025-10-18 16:10:08 +02:00
selection.selectFileWhenLoaded(file._data.id);
}
}
onMount(() => {
2025-10-18 16:10:08 +02:00
if ($map && popup && popupElement) {
fileStateCollectionObserver = new GPXFileStateCollectionObserver(
2025-10-19 16:14:05 +02:00
(newFiles) => {
newFiles.forEach((fileState, fileId) => {
routingControls.set(
fileId,
new RoutingControls(fileId, fileState, popup, popupElement)
);
});
2025-10-18 16:10:08 +02:00
},
(fileId) => {
const controls = routingControls.get(fileId);
if (controls) {
controls.destroy();
routingControls.delete(fileId);
}
},
() => {
routingControls.forEach((controls) => controls.destroy());
routingControls.clear();
}
);
mapCursor.notify(MapCursorState.TOOL_WITH_CROSSHAIR, true);
$map.on('click', createFileWithPoint);
}
});
onDestroy(() => {
2025-10-18 16:10:08 +02:00
if ($map) {
2025-10-18 18:51:11 +02:00
if (fileStateCollectionObserver) {
fileStateCollectionObserver.destroy();
}
2024-07-13 11:42:21 +02:00
2025-10-18 16:10:08 +02:00
mapCursor.notify(MapCursorState.TOOL_WITH_CROSSHAIR, false);
$map.off('click', createFileWithPoint);
}
});
2024-04-22 19:36:31 +02:00
</script>
2024-09-30 12:56:58 +02:00
{#if minimizable && minimized}
<div class="-m-1.5 -mb-2">
2025-10-18 18:51:11 +02:00
<Button variant="ghost" size="icon-sm" class="size-6" onclick={() => (minimized = false)}>
<SquareArrowOutDownRight size="18" class="size-4.5" />
</Button>
</div>
2024-06-10 12:06:32 +02:00
{:else}
2025-10-05 19:34:05 +02:00
<div class="flex flex-col gap-3 w-full max-w-80 animate-in animate-out {className ?? ''}">
<div class="flex flex-col gap-3">
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
<span class="flex flex-row items-center gap-1">
2025-10-18 16:10:08 +02:00
{#if $routing}
<Route size="16" />
{:else}
<RouteOff size="16" />
{/if}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.use_routing')}
</span>
2025-06-21 21:07:36 +02:00
<Tooltip label={i18n._('toolbar.routing.use_routing_tooltip')}>
2025-10-18 18:51:11 +02:00
<Switch bind:checked={$routing} />
{#snippet extra()}
<Shortcut key="F5" />
{/snippet}
</Tooltip>
</Label>
2025-10-18 16:10:08 +02:00
{#if $routing}
<div class="flex flex-col gap-3" in:slide>
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
<span class="shrink-0 flex flex-row items-center gap-1">
2025-10-18 16:10:08 +02:00
{#if $routingProfile.includes('bike') || $routingProfile.includes('motorcycle')}
<Bike size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('foot')}
<Footprints size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('water')}
<Waves size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('railway')}
<TrainFront size="16" />
{/if}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.activity')}
</span>
2025-10-18 16:10:08 +02:00
<Select.Root type="single" bind:value={$routingProfile}>
<Select.Trigger class="h-8 grow">
2025-10-18 16:10:08 +02:00
{i18n._(`toolbar.routing.activities.${$routingProfile}`)}
</Select.Trigger>
<Select.Content>
{#each Object.keys(brouterProfiles) as profile}
<Select.Item value={profile}
2025-06-21 21:07:36 +02:00
>{i18n._(
`toolbar.routing.activities.${profile}`
)}</Select.Item
>
{/each}
</Select.Content>
</Select.Root>
</Label>
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
<span class="flex flex-row gap-1">
<TriangleAlert size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.allow_private')}
</span>
2025-10-18 18:51:11 +02:00
<Switch bind:checked={$privateRoads} />
</Label>
</div>
{/if}
</div>
<div class="flex flex-row flex-wrap justify-center gap-1">
<ButtonWithTooltip
2025-06-21 21:07:36 +02:00
label={i18n._('toolbar.routing.reverse.tooltip')}
variant="outline"
2025-10-18 18:51:11 +02:00
class="gap-1 text-xs"
disabled={!validSelection}
2025-10-05 19:34:05 +02:00
onclick={fileActions.reverseSelection}
>
2025-06-21 21:07:36 +02:00
<ArrowRightLeft size="12" />{i18n._('toolbar.routing.reverse.button')}
</ButtonWithTooltip>
<ButtonWithTooltip
2025-06-21 21:07:36 +02:00
label={i18n._('toolbar.routing.route_back_to_start.tooltip')}
variant="outline"
2025-10-18 18:51:11 +02:00
class="gap-1 text-xs"
disabled={!validSelection}
2025-06-21 21:07:36 +02:00
onclick={() => {
2025-10-05 19:34:05 +02:00
const selected = selection.getOrderedSelection();
if (selected.length > 0) {
const firstFileId = selected[0].getFileId();
2025-10-05 19:34:05 +02:00
const firstFile = fileStateCollection.getFile(firstFileId);
if (firstFile) {
let start = (() => {
if (selected[0] instanceof ListFileItem) {
return firstFile.trk[0]?.trkseg[0]?.trkpt[0];
} else if (selected[0] instanceof ListTrackItem) {
return firstFile.trk[selected[0].getTrackIndex()]?.trkseg[0]
?.trkpt[0];
} else if (selected[0] instanceof ListTrackSegmentItem) {
return firstFile.trk[selected[0].getTrackIndex()]?.trkseg[
selected[0].getSegmentIndex()
]?.trkpt[0];
}
})();
2024-09-30 12:56:58 +02:00
if (start !== undefined) {
const lastFileId = selected[selected.length - 1].getFileId();
2025-10-18 16:10:08 +02:00
routingControls
.get(lastFileId)
?.appendAnchorWithCoordinates(start.getCoordinates());
}
}
}
}}
>
2025-06-21 21:07:36 +02:00
<Home size="12" />{i18n._('toolbar.routing.route_back_to_start.button')}
</ButtonWithTooltip>
<ButtonWithTooltip
2025-06-21 21:07:36 +02:00
label={i18n._('toolbar.routing.round_trip.tooltip')}
variant="outline"
2025-10-18 18:51:11 +02:00
class="gap-1 text-xs"
disabled={!validSelection}
2025-10-17 23:54:45 +02:00
onclick={fileActions.createRoundTripForSelection}
>
2025-06-21 21:07:36 +02:00
<Repeat size="12" />{i18n._('toolbar.routing.round_trip.button')}
</ButtonWithTooltip>
</div>
<div class="w-full flex flex-row gap-2 items-end justify-between">
2025-06-21 21:07:36 +02:00
<Help link={getURLForLanguage(i18n.lang, '/help/toolbar/routing')}>
{#if !validSelection}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.help_no_file')}
{:else}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.help')}
{/if}
</Help>
<Button
variant="ghost"
2025-10-18 18:51:11 +02:00
size="icon-sm"
class="size-6"
2025-06-21 21:07:36 +02:00
onclick={() => {
if (minimizable) {
minimized = true;
}
}}
>
<SquareArrowUpLeft size="18" />
</Button>
</div>
</div>
2024-06-10 12:06:32 +02:00
{/if}