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

250 lines
7.7 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';
2024-05-07 17:19:53 +02:00
import { Button } from '$lib/components/ui/button';
import Help from '$lib/components/Help.svelte';
2024-09-30 12:56:58 +02:00
import ButtonWithTooltip from '$lib/components/ButtonWithTooltip.svelte';
2024-05-07 17:19:53 +02:00
import Tooltip from '$lib/components/Tooltip.svelte';
2024-07-16 15:48:01 +02:00
import Shortcut from '$lib/components/Shortcut.svelte';
2024-05-07 17:19:53 +02:00
import {
Bike,
Footprints,
Waves,
TrainFront,
Route,
TriangleAlert,
ArrowRightLeft,
2024-05-08 12:20:01 +02:00
Home,
2024-06-10 12:06:32 +02:00
RouteOff,
Repeat,
2024-06-10 16:56:50 +02:00
SquareArrowUpLeft,
SquareArrowOutDownRight
2024-05-07 17:19:53 +02:00
} from 'lucide-svelte';
2024-04-22 19:36:31 +02:00
import { map, newGPXFile, routingControls, selectFileWhenLoaded } from '$lib/stores';
import { dbUtils, getFile, getFileIds, settings } from '$lib/db';
2024-05-04 15:10:30 +02:00
import { brouterProfiles, routingProfileSelectItem } from './Routing';
2024-09-20 10:15:28 +02:00
import { _, locale } from 'svelte-i18n';
2024-04-25 16:41:06 +02:00
import { RoutingControls } from './RoutingControls';
2024-04-26 13:33:17 +02:00
import mapboxgl from 'mapbox-gl';
2024-05-02 19:51:08 +02:00
import { fileObservers } from '$lib/db';
2024-05-07 17:19:53 +02:00
import { slide } from 'svelte/transition';
import { getOrderedSelection, selection } from '$lib/components/file-list/Selection';
import {
ListFileItem,
ListRootItem,
ListTrackItem,
ListTrackSegmentItem,
type ListItem
} from '$lib/components/file-list/FileList';
2024-09-20 10:15:28 +02:00
import { flyAndScale, getURLForLanguage, resetCursor, setCrosshairCursor } from '$lib/utils';
import { onDestroy, onMount } from 'svelte';
import { TrackPoint } from 'gpx';
2024-04-25 16:41:06 +02:00
2024-07-05 16:08:16 +02:00
export let minimized = false;
export let minimizable = true;
export let popup: mapboxgl.Popup | undefined = undefined;
export let popupElement: HTMLElement | undefined = undefined;
2024-05-22 16:05:31 +02:00
let selectedItem: ListItem | null = null;
2024-04-25 16:41:06 +02:00
2024-07-05 16:08:16 +02:00
const { privateRoads, routing } = settings;
2024-05-04 15:10:30 +02:00
2024-07-05 16:08:16 +02:00
$: if ($map && popup && popupElement) {
2024-04-25 16:41:06 +02:00
// remove controls for deleted files
2024-04-30 20:55:47 +02:00
routingControls.forEach((controls, fileId) => {
2024-05-02 19:51:08 +02:00
if (!$fileObservers.has(fileId)) {
2024-05-24 16:37:26 +02:00
controls.destroy();
2024-04-30 20:55:47 +02:00
routingControls.delete(fileId);
2024-04-30 22:35:54 +02:00
2024-05-22 16:05:31 +02:00
if (selectedItem && selectedItem.getFileId() === fileId) {
selectedItem = null;
2024-04-30 22:35:54 +02:00
}
2024-07-13 11:42:21 +02:00
} else if ($map !== controls.map) {
controls.updateMap($map);
2024-04-25 16:41:06 +02:00
}
});
2024-05-24 16:37:26 +02:00
// add controls for new files
$fileObservers.forEach((file, fileId) => {
if (!routingControls.has(fileId)) {
2024-07-13 11:42:21 +02:00
routingControls.set(fileId, new RoutingControls($map, fileId, file, popup, popupElement));
2024-04-30 20:55:47 +02:00
}
2024-05-24 16:37:26 +02:00
});
2024-04-22 19:36:31 +02:00
}
2024-04-26 13:33:17 +02:00
2024-05-24 16:37:26 +02:00
$: validSelection = $selection.hasAnyChildren(new ListRootItem(), true, ['waypoints']);
function createFileWithPoint(e: any) {
if ($selection.size === 0) {
let file = newGPXFile();
2024-07-04 02:17:50 +02:00
file.replaceTrackPoints(0, 0, 0, 0, [
new TrackPoint({
attributes: {
lat: e.lngLat.lat,
lon: e.lngLat.lng
}
})
]);
2024-07-04 02:17:50 +02:00
file._data.id = getFileIds(1)[0];
dbUtils.add(file);
selectFileWhenLoaded(file._data.id);
}
}
onMount(() => {
setCrosshairCursor();
$map?.on('click', createFileWithPoint);
});
onDestroy(() => {
resetCursor();
$map?.off('click', createFileWithPoint);
2024-07-13 11:42:21 +02:00
routingControls.forEach((controls) => controls.destroy());
routingControls.clear();
});
2024-04-22 19:36:31 +02:00
</script>
2024-09-30 12:56:58 +02:00
{#if minimizable && minimized}
2024-06-10 12:06:32 +02:00
<div class="-m-1.5 -mb-2">
2024-07-05 16:08:16 +02:00
<Button variant="ghost" class="px-1 h-[26px]" on:click={() => (minimized = false)}>
2024-06-10 16:56:50 +02:00
<SquareArrowOutDownRight size="18" />
2024-06-10 12:06:32 +02:00
</Button>
</div>
{:else}
2024-07-09 22:15:05 +02:00
<div
class="flex flex-col gap-3 w-full max-w-80 {$$props.class ?? ''}"
in:flyAndScale={{ x: -2, y: 0, duration: 50 }}
>
2024-09-30 12:56:58 +02:00
<div class="flex flex-col gap-3">
<Label class="flex flex-row justify-between items-center gap-2">
<span class="flex flex-row items-center gap-1">
{#if $routing}
<Route size="16" />
{:else}
<RouteOff size="16" />
{/if}
{$_('toolbar.routing.use_routing')}
</span>
<Tooltip label={$_('toolbar.routing.use_routing_tooltip')}>
2024-06-10 16:56:50 +02:00
<Switch class="scale-90" bind:checked={$routing} />
2024-09-30 12:56:58 +02:00
<Shortcut slot="extra" key="F5" />
</Tooltip>
</Label>
2024-06-10 16:56:50 +02:00
{#if $routing}
<div class="flex flex-col gap-3" in:slide>
2024-09-30 12:56:58 +02:00
<Label class="flex flex-row justify-between items-center gap-2">
2024-06-10 16:56:50 +02:00
<span class="shrink-0 flex flex-row items-center gap-1">
{#if $routingProfileSelectItem.value.includes('bike') || $routingProfileSelectItem.value.includes('motorcycle')}
<Bike size="16" />
{:else if $routingProfileSelectItem.value.includes('foot')}
<Footprints size="16" />
{:else if $routingProfileSelectItem.value.includes('water')}
<Waves size="16" />
{:else if $routingProfileSelectItem.value.includes('railway')}
<TrainFront size="16" />
2024-06-10 12:06:32 +02:00
{/if}
2024-06-10 16:56:50 +02:00
{$_('toolbar.routing.activity')}
2024-06-10 12:06:32 +02:00
</span>
2024-06-10 16:56:50 +02:00
<Select.Root bind:selected={$routingProfileSelectItem}>
<Select.Trigger class="h-8 grow">
<Select.Value />
</Select.Trigger>
<Select.Content>
{#each Object.keys(brouterProfiles) as profile}
<Select.Item value={profile}
>{$_(`toolbar.routing.activities.${profile}`)}</Select.Item
>
{/each}
</Select.Content>
</Select.Root>
2024-06-10 12:06:32 +02:00
</Label>
2024-09-30 12:56:58 +02:00
<Label class="flex flex-row justify-between items-center gap-2">
<span class="flex flex-row gap-1">
<TriangleAlert size="16" />
{$_('toolbar.routing.allow_private')}
</span>
2024-06-10 16:56:50 +02:00
<Switch class="scale-90" bind:checked={$privateRoads} />
</Label>
</div>
{/if}
2024-06-10 12:06:32 +02:00
</div>
<div class="flex flex-row flex-wrap justify-center gap-1">
2024-09-30 12:56:58 +02:00
<ButtonWithTooltip
label={$_('toolbar.routing.reverse.tooltip')}
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={!validSelection}
on:click={dbUtils.reverseSelection}
>
<ArrowRightLeft size="12" />{$_('toolbar.routing.reverse.button')}
</ButtonWithTooltip>
<ButtonWithTooltip
label={$_('toolbar.routing.route_back_to_start.tooltip')}
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={!validSelection}
on:click={() => {
const selected = getOrderedSelection();
if (selected.length > 0) {
const firstFileId = selected[0].getFileId();
const firstFile = 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();
routingControls
.get(lastFileId)
?.appendAnchorWithCoordinates(start.getCoordinates());
}
}
2024-09-30 12:56:58 +02:00
}
}}
>
<Home size="12" />{$_('toolbar.routing.route_back_to_start.button')}
</ButtonWithTooltip>
<ButtonWithTooltip
label={$_('toolbar.routing.round_trip.tooltip')}
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={!validSelection}
on:click={dbUtils.createRoundTripForSelection}
>
<Repeat size="12" />{$_('toolbar.routing.round_trip.button')}
</ButtonWithTooltip>
2024-05-07 17:19:53 +02:00
</div>
2024-06-10 16:56:50 +02:00
<div class="w-full flex flex-row gap-2 items-end justify-between">
2024-09-20 10:15:28 +02:00
<Help link={getURLForLanguage($locale, '/help/toolbar/routing')}>
{#if !validSelection}
2024-08-07 16:54:48 +02:00
{$_('toolbar.routing.help_no_file')}
2024-06-10 16:56:50 +02:00
{:else}
2024-08-07 16:54:48 +02:00
{$_('toolbar.routing.help')}
2024-06-10 16:56:50 +02:00
{/if}
</Help>
2024-07-10 00:11:47 +02:00
<Button
variant="ghost"
class="px-1 h-6"
on:click={() => {
if (minimizable) {
minimized = true;
}
}}
>
<SquareArrowUpLeft size="18" />
</Button>
2024-06-10 16:56:50 +02:00
</div>
</div>
2024-06-10 12:06:32 +02:00
{/if}