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

219 lines
6.8 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';
import Tooltip from '$lib/components/Tooltip.svelte';
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, getFileIds, settings } from '$lib/db';
2024-05-04 15:10:30 +02:00
import { brouterProfiles, routingProfileSelectItem } from './Routing';
2024-04-24 18:13:01 +02:00
import { _ } from 'svelte-i18n';
2024-04-30 20:55:47 +02:00
import { get } from 'svelte/store';
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';
2024-05-22 16:05:31 +02:00
import { selection } from '$lib/components/file-list/Selection';
2024-05-24 16:37:26 +02:00
import { ListRootItem, type ListItem } from '$lib/components/file-list/FileList';
2024-06-10 12:06:32 +02:00
import { flyAndScale } from '$lib/utils';
import { onDestroy, onMount } from 'svelte';
import { TrackPoint } from 'gpx';
import { produce } from 'immer';
2024-04-25 16:41:06 +02:00
2024-05-24 20:23:49 +02:00
export let popup: mapboxgl.Popup;
export let popupElement: HTMLElement;
2024-05-22 16:05:31 +02:00
let selectedItem: ListItem | null = null;
2024-04-25 16:41:06 +02:00
2024-06-10 12:06:32 +02:00
const { minimizeRoutingMenu, privateRoads, routing } = settings;
2024-05-04 15:10:30 +02:00
2024-05-02 19:51:08 +02:00
$: if ($map) {
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-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-04-30 20:55:47 +02:00
routingControls.set(
2024-05-22 16:05:31 +02:00
fileId,
2024-05-24 16:37:26 +02:00
new RoutingControls(get(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();
file = file.replaceTrackPoints(0, 0, 0, 0, [
new TrackPoint({
attributes: {
lat: e.lngLat.lat,
lon: e.lngLat.lng
}
})
]);
file = produce(file, (draft) => {
draft._data.id = getFileIds(1)[0];
});
dbUtils.add(file);
selectFileWhenLoaded(file._data.id);
}
}
onMount(() => {
$map?.on('click', createFileWithPoint);
});
onDestroy(() => {
$map?.off('click', createFileWithPoint);
});
2024-04-22 19:36:31 +02:00
</script>
2024-06-10 12:06:32 +02:00
{#if $minimizeRoutingMenu}
<div class="-m-1.5 -mb-2">
<Button variant="ghost" class="px-1 h-[26px]" on:click={() => ($minimizeRoutingMenu = false)}>
2024-06-10 16:56:50 +02:00
<SquareArrowOutDownRight size="18" />
2024-06-10 12:06:32 +02:00
</Button>
</div>
{:else}
2024-06-12 14:53:26 +02:00
<div class="flex flex-col gap-3 w-80" in:flyAndScale={{ x: -2, y: 0, duration: 50 }}>
2024-06-10 16:56:50 +02:00
<div class="grow flex flex-col gap-3">
<Tooltip>
<Label slot="data" class="w-full flex flex-row justify-between items-center gap-2">
<span class="flex flex-row gap-1">
{#if $routing}
<Route size="16" />
{:else}
<RouteOff size="16" />
{/if}
{$_('toolbar.routing.use_routing')}
</span>
<Switch class="scale-90" bind:checked={$routing} />
</Label>
<span slot="tooltip">{$_('toolbar.routing.use_routing_tooltip')}</span>
</Tooltip>
{#if $routing}
<div class="flex flex-col gap-3" in:slide>
<Label class="w-full flex flex-row justify-between items-center gap-2">
<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-06-10 16:56:50 +02:00
<Label class="w-full flex flex-row justify-between items-center gap-2">
<span class="flex flex-row gap-1"
><TriangleAlert size="16" />{$_('toolbar.routing.allow_private')}</span
>
<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">
<Tooltip>
<Button
slot="data"
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={!validSelection}
on:click={dbUtils.reverseSelection}
2024-05-07 17:19:53 +02:00
>
2024-06-10 12:06:32 +02:00
<ArrowRightLeft size="12" />{$_('toolbar.routing.reverse.button')}
</Button>
<span slot="tooltip">{$_('toolbar.routing.reverse.tooltip')}</span>
</Tooltip>
<Tooltip>
<Button
slot="data"
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={$selection.size != 1 || !validSelection}
on:click={() => {
const fileId = get(selection).getSelected()[0].getFileId();
routingControls.get(fileId)?.routeToStart();
}}
>
<Home size="12" />{$_('toolbar.routing.route_back_to_start.button')}
</Button>
<span slot="tooltip">{$_('toolbar.routing.route_back_to_start.tooltip')}</span>
</Tooltip>
<Tooltip>
<Button
slot="data"
variant="outline"
class="flex flex-row gap-1 text-xs px-2"
disabled={$selection.size != 1 || !validSelection}
on:click={() => {
const fileId = get(selection).getSelected()[0].getFileId();
routingControls.get(fileId)?.createRoundTrip();
}}
>
<Repeat size="12" />{$_('toolbar.routing.round_trip.button')}
</Button>
<span slot="tooltip">{$_('toolbar.routing.round_trip.tooltip')}</span>
</Tooltip>
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">
<Help>
{#if $selection.size > 1}
<div>{$_('toolbar.routing.help_multiple_files')}</div>
{:else if $selection.size == 0 || !validSelection}
<div>{$_('toolbar.routing.help_no_file')}</div>
{:else}
<div>{$_('toolbar.routing.help')}</div>
{/if}
</Help>
<Button variant="ghost" class="px-1 h-6" on:click={() => ($minimizeRoutingMenu = true)}>
<SquareArrowUpLeft size="18" />
</Button>
</div>
</div>
2024-06-10 12:06:32 +02:00
{/if}