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

210 lines
8.5 KiB
Svelte
Raw Normal View History

2024-04-22 19:36:31 +02:00
<script lang="ts">
2025-02-02 11:17:22 +01:00
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,
2025-10-24 20:07:15 +02:00
House,
2025-02-02 11:17:22 +01:00
RouteOff,
Repeat,
SquareArrowUpLeft,
SquareArrowOutDownRight,
2025-06-21 21:07:36 +02:00
} from '@lucide/svelte';
2026-02-01 18:06:16 +01:00
import { routingProfiles } from '$lib/components/toolbar/tools/routing/routing';
2025-06-21 21:07:36 +02:00
import { i18n } from '$lib/i18n.svelte';
2025-02-02 11:17:22 +01:00
import { slide } from 'svelte/transition';
2026-10-07 17:58:17 +02:00
import { get } from 'svelte/store';
2025-10-18 16:10:08 +02:00
import { getURLForLanguage } from '$lib/utils';
2025-02-02 11:17:22 +01:00
import { onDestroy, onMount } from 'svelte';
2025-10-17 23:54:45 +02:00
import { settings } from '$lib/logic/settings';
import { map } from '$lib/components/map/map';
2026-10-07 17:58:17 +02:00
import { engine } from '$lib/engine';
import { fileActions, newFileName } from '$lib/logic/file-actions';
2025-10-18 16:10:08 +02:00
import { mapCursor, MapCursorState } from '$lib/logic/map-cursor';
2026-10-07 17:58:17 +02:00
import { 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;
2026-01-30 21:01:24 +01:00
popup?: maplibregl.Popup;
2025-10-05 19:34:05 +02:00
popupElement?: HTMLDivElement;
class?: string;
} = $props();
2024-04-24 18:13:01 +02:00
2025-06-21 21:07:36 +02:00
const { privateRoads, routing, routingProfile } = settings;
2024-05-04 15:10:30 +02:00
2026-10-07 17:58:17 +02:00
const selection = engine.selection;
let routingControls: RoutingControls | undefined = undefined;
2024-04-26 13:33:17 +02:00
2026-10-07 17:58:17 +02:00
// the tool works on files, tracks and segments, not on waypoints
2025-10-05 19:34:05 +02:00
let validSelection = $derived(
2026-10-07 17:58:17 +02:00
$selection.type === 'file' || $selection.type === 'track' || $selection.type === 'segment'
2025-10-05 19:34:05 +02:00
);
2024-06-15 19:17:27 +02:00
2025-02-02 11:17:22 +01:00
function createFileWithPoint(e: any) {
2026-10-07 17:58:17 +02:00
if ($selection.type === 'empty') {
// the engine selects the new file, which starts with the trackpoint: one undo step
engine.newFile(newFileName(), { lng: e.lngLat.lng, lat: e.lngLat.lat });
2025-02-02 11:17:22 +01:00
}
}
2024-06-15 19:17:27 +02:00
2025-02-02 11:17:22 +01:00
onMount(() => {
2025-10-18 16:10:08 +02:00
if ($map && popup && popupElement) {
2026-10-07 17:58:17 +02:00
routingControls = new RoutingControls(popup, popupElement);
2025-10-18 16:10:08 +02:00
mapCursor.notify(MapCursorState.TOOL_WITH_CROSSHAIR, true);
$map.on('click', createFileWithPoint);
}
2025-02-02 11:17:22 +01:00
});
2024-06-15 19:17:27 +02:00
2025-02-02 11:17:22 +01:00
onDestroy(() => {
2025-10-18 16:10:08 +02:00
if ($map) {
2026-10-07 17:58:17 +02:00
routingControls?.destroy();
routingControls = undefined;
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);
}
2025-02-02 11:17:22 +01:00
});
2024-04-22 19:36:31 +02:00
</script>
2024-09-30 12:56:58 +02:00
{#if minimizable && minimized}
2025-02-02 11:17:22 +01:00
<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" />
2025-02-02 11:17:22 +01:00
</Button>
</div>
2024-06-10 12:06:32 +02:00
{:else}
2025-11-10 11:11:37 +01:00
<div class="flex flex-col gap-3 w-full max-w-80 {className ?? ''}">
2025-02-02 11:17:22 +01:00
<div class="flex flex-col gap-3">
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
2025-02-02 11:17:22 +01:00
<span class="flex flex-row items-center gap-1">
2025-10-18 16:10:08 +02:00
{#if $routing}
2025-02-02 11:17:22 +01:00
<Route size="16" />
{:else}
<RouteOff size="16" />
{/if}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.use_routing')}
2025-02-02 11:17:22 +01:00
</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}
2025-02-02 11:17:22 +01:00
</Tooltip>
</Label>
2025-10-18 16:10:08 +02:00
{#if $routing}
2025-02-02 11:17:22 +01:00
<div class="flex flex-col gap-3" in:slide>
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
2025-02-02 11:17:22 +01:00
<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')}
2025-02-02 11:17:22 +01:00
<Bike size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('foot')}
2025-02-02 11:17:22 +01:00
<Footprints size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('water')}
2025-02-02 11:17:22 +01:00
<Waves size="16" />
2025-10-18 16:10:08 +02:00
{:else if $routingProfile.includes('railway')}
2025-02-02 11:17:22 +01:00
<TrainFront size="16" />
{/if}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.activity')}
2025-02-02 11:17:22 +01:00
</span>
2025-10-18 16:10:08 +02:00
<Select.Root type="single" bind:value={$routingProfile}>
2025-12-07 12:15:40 +01:00
<Select.Trigger class="grow" size="sm">
2025-10-18 16:10:08 +02:00
{i18n._(`toolbar.routing.activities.${$routingProfile}`)}
2025-02-02 11:17:22 +01:00
</Select.Trigger>
<Select.Content>
2026-02-01 18:06:16 +01:00
{#each Object.keys(routingProfiles) as profile}
2025-02-02 11:17:22 +01:00
<Select.Item value={profile}
2025-06-21 21:07:36 +02:00
>{i18n._(
`toolbar.routing.activities.${profile}`
)}</Select.Item
2025-02-02 11:17:22 +01:00
>
{/each}
</Select.Content>
</Select.Root>
</Label>
2025-10-18 18:51:11 +02:00
<Label class="justify-between">
2025-02-02 11:17:22 +01:00
<span class="flex flex-row gap-1">
<TriangleAlert size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.allow_private')}
2025-02-02 11:17:22 +01:00
</span>
2025-10-18 18:51:11 +02:00
<Switch bind:checked={$privateRoads} />
2025-02-02 11:17:22 +01:00
</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')}
2025-02-02 11:17:22 +01:00
variant="outline"
2026-04-06 18:22:01 +02:00
class="gap-1 text-xs px-1.5 py-1.5 h-fit"
2025-02-02 11:17:22 +01:00
disabled={!validSelection}
2026-10-07 18:36:00 +02:00
onclick={() => engine.reverse()}
2025-02-02 11:17:22 +01:00
>
2025-12-02 21:49:15 +01:00
<ArrowRightLeft class="size-3" />{i18n._('toolbar.routing.reverse.button')}
2025-02-02 11:17:22 +01:00
</ButtonWithTooltip>
<ButtonWithTooltip
2025-06-21 21:07:36 +02:00
label={i18n._('toolbar.routing.route_back_to_start.tooltip')}
2025-02-02 11:17:22 +01:00
variant="outline"
2026-04-06 18:22:01 +02:00
class="gap-1 text-xs px-1.5 py-1.5 h-fit"
2025-02-02 11:17:22 +01:00
disabled={!validSelection}
2025-06-21 21:07:36 +02:00
onclick={() => {
2026-10-07 17:58:17 +02:00
// the first trackpoint of the selection
const { length, lng, lat } = get(engine.statistics);
if (length > 0) {
routingControls?.appendAnchorWithCoordinates({ lng: lng[0], lat: lat[0] });
2025-02-02 11:17:22 +01:00
}
}}
>
2025-12-02 21:49:15 +01:00
<House class="size-3" />{i18n._('toolbar.routing.route_back_to_start.button')}
2025-02-02 11:17:22 +01:00
</ButtonWithTooltip>
<ButtonWithTooltip
2025-06-21 21:07:36 +02:00
label={i18n._('toolbar.routing.round_trip.tooltip')}
2025-02-02 11:17:22 +01:00
variant="outline"
2026-04-06 18:22:01 +02:00
class="gap-1 text-xs px-1.5 py-1.5 h-fit"
2025-02-02 11:17:22 +01:00
disabled={!validSelection}
2025-10-17 23:54:45 +02:00
onclick={fileActions.createRoundTripForSelection}
2025-02-02 11:17:22 +01:00
>
2025-12-02 21:49:15 +01:00
<Repeat class="size-3" />{i18n._('toolbar.routing.round_trip.button')}
2025-02-02 11:17:22 +01:00
</ButtonWithTooltip>
</div>
2026-04-06 18:22:01 +02:00
<div class="w-full flex flex-row gap-1 items-end justify-between">
2025-06-21 21:07:36 +02:00
<Help link={getURLForLanguage(i18n.lang, '/help/toolbar/routing')}>
2025-02-02 11:17:22 +01:00
{#if !validSelection}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.help_no_file')}
2025-02-02 11:17:22 +01:00
{:else}
2025-06-21 21:07:36 +02:00
{i18n._('toolbar.routing.help')}
2025-02-02 11:17:22 +01:00
{/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={() => {
2025-02-02 11:17:22 +01:00
if (minimizable) {
minimized = true;
}
}}
>
<SquareArrowUpLeft size="18" />
</Button>
</div>
</div>
2024-06-10 12:06:32 +02:00
{/if}