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

168 lines
5.0 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,
RouteOff
2024-05-07 17:19:53 +02:00
} from 'lucide-svelte';
2024-04-22 19:36:31 +02:00
2024-05-24 20:23:49 +02:00
import { map, routingControls } from '$lib/stores';
2024-05-07 17:19:53 +02:00
import { dbUtils, 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-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-05-04 15:10:30 +02:00
const { privateRoads, routing } = settings;
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']);
2024-04-22 19:36:31 +02:00
</script>
2024-05-24 20:23:49 +02:00
<div class=" flex flex-col gap-3">
2024-05-07 17:19:53 +02:00
<Tooltip>
<div slot="data" class="w-full flex flex-row justify-between items-center gap-2">
2024-05-08 12:20:01 +02:00
<Label for="routing" class="flex flex-row gap-1">
{#if $routing}
<Route size="16" />
{:else}
<RouteOff size="16" />
{/if}
{$_('toolbar.routing.use_routing')}</Label
2024-05-07 17:19:53 +02:00
>
<Switch id="routing" class="scale-90" bind:checked={$routing} />
</div>
<span slot="tooltip">{$_('toolbar.routing.use_routing_tooltip')}</span>
</Tooltip>
2024-05-24 20:23:49 +02:00
2024-05-07 17:19:53 +02:00
{#if $routing}
<div class="flex flex-col gap-3" in:slide>
<div class="w-full flex flex-row justify-between items-center gap-2">
<Label class="flex flex-row 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" />
{/if}
{$_('toolbar.routing.activity')}
</Label>
<Select.Root bind:selected={$routingProfileSelectItem}>
<Select.Trigger class="h-8 w-full">
<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>
</div>
<div class="w-full flex flex-row justify-between items-center gap-2">
<Label for="private" class="flex flex-row gap-1"
><TriangleAlert size="16" />{$_('toolbar.routing.allow_private')}</Label
>
<Switch id="private" class="scale-90" bind:checked={$privateRoads} />
</div>
</div>
{/if}
<div class="flex flex-row justify-center gap-2">
<Tooltip>
<Button
slot="data"
variant="outline"
2024-05-24 16:37:26 +02:00
disabled={!validSelection}
on:click={dbUtils.reverseSelection}
2024-05-07 17:19:53 +02:00
>
<ArrowRightLeft size="16" />
</Button>
<span slot="tooltip">{$_('toolbar.routing.reverse_tooltip')}</span>
</Tooltip>
<Tooltip>
<Button
slot="data"
variant="outline"
2024-05-24 16:37:26 +02:00
disabled={$selection.size != 1 || !validSelection}
2024-05-07 17:19:53 +02:00
on:click={() => {
2024-05-22 16:05:31 +02:00
const fileId = get(selection).getSelected()[0].getFileId();
2024-05-07 17:19:53 +02:00
routingControls.get(fileId)?.routeToStart();
}}
>
<Home size="16" />
</Button>
<span slot="tooltip">{$_('toolbar.routing.route_back_to_start_tooltip')}</span>
</Tooltip>
<Tooltip>
<Button
slot="data"
variant="outline"
2024-05-24 16:37:26 +02:00
disabled={$selection.size != 1 || !validSelection}
2024-05-07 17:19:53 +02:00
on:click={() => {
2024-05-22 16:05:31 +02:00
const fileId = get(selection).getSelected()[0].getFileId();
2024-05-07 17:19:53 +02:00
routingControls.get(fileId)?.createRoundTrip();
}}
>
<Home size="16" />
</Button>
<span slot="tooltip">{$_('toolbar.routing.round_trip_tooltip')}</span>
</Tooltip>
</div>
2024-05-07 17:19:53 +02:00
<Help class="max-w-60">
2024-05-22 16:05:31 +02:00
{#if $selection.size > 1}
2024-05-07 17:19:53 +02:00
<div>{$_('toolbar.routing.help_multiple_files')}</div>
2024-05-24 16:37:26 +02:00
{:else if $selection.size == 0 || !validSelection}
2024-05-07 17:19:53 +02:00
<div>{$_('toolbar.routing.help_no_file')}</div>
{:else}
<div>{$_('toolbar.routing.help')}</div>
{/if}
</Help>
2024-05-24 20:23:49 +02:00
</div>