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

201 lines
5.8 KiB
Svelte
Raw Normal View History

2024-04-22 19:36:31 +02:00
<script lang="ts">
2024-04-28 17:50:39 +02:00
import ToolbarItemMenu from '$lib/components/toolbar/ToolbarItemMenu.svelte';
2024-04-22 19:36:31 +02: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';
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-02 19:51:08 +02:00
import { map, selectedFiles, Tool } 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 RoutingControlPopup from './RoutingControlPopup.svelte';
import { onMount } from 'svelte';
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-04-25 16:41:06 +02:00
2024-04-30 20:55:47 +02:00
let routingControls: Map<string, RoutingControls> = new Map();
2024-04-26 13:33:17 +02:00
let popupElement: HTMLElement;
let popup: mapboxgl.Popup | null = null;
2024-04-30 20:55:47 +02:00
let selectedId: string | null = null;
2024-04-25 16:41:06 +02:00
let active = false;
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-04-25 16:41:06 +02:00
controls.remove();
2024-04-30 20:55:47 +02:00
routingControls.delete(fileId);
2024-04-30 22:35:54 +02:00
if (selectedId === fileId) {
selectedId = null;
}
2024-04-25 16:41:06 +02:00
}
});
}
2024-04-25 16:41:06 +02:00
$: if ($map && $selectedFiles) {
// update selected file
if ($selectedFiles.size == 0 || $selectedFiles.size > 1 || !active) {
2024-04-30 20:55:47 +02:00
if (selectedId) {
routingControls.get(selectedId)?.remove();
2024-04-25 16:41:06 +02:00
}
2024-04-30 20:55:47 +02:00
selectedId = null;
2024-04-25 11:13:15 +02:00
} else {
2024-04-30 20:55:47 +02:00
let newSelectedId = get(selectedFiles).values().next().value;
if (selectedId !== newSelectedId) {
if (selectedId) {
routingControls.get(selectedId)?.remove();
2024-04-25 16:41:06 +02:00
}
2024-04-30 20:55:47 +02:00
selectedId = newSelectedId;
2024-04-25 16:41:06 +02:00
}
2024-04-25 11:13:15 +02:00
}
}
2024-04-24 20:13:42 +02:00
2024-04-30 20:55:47 +02:00
$: if ($map && selectedId) {
if (!routingControls.has(selectedId)) {
2024-05-02 19:51:08 +02:00
let selectedFileObserver = get(fileObservers).get(selectedId);
if (selectedFileObserver) {
2024-04-30 20:55:47 +02:00
routingControls.set(
selectedId,
2024-05-03 15:59:34 +02:00
new RoutingControls(get(map), selectedId, selectedFileObserver, popup, popupElement)
2024-04-30 20:55:47 +02:00
);
}
2024-04-25 16:41:06 +02:00
} else {
2024-04-30 20:55:47 +02:00
routingControls.get(selectedId)?.add();
2024-04-25 16:41:06 +02:00
}
2024-04-22 19:36:31 +02:00
}
2024-04-26 13:33:17 +02:00
onMount(() => {
popup = new mapboxgl.Popup({
closeButton: false,
maxWidth: undefined
});
popup.setDOMContent(popupElement);
popupElement.classList.remove('hidden');
});
2024-04-22 19:36:31 +02:00
</script>
<ToolbarItemMenu tool={Tool.ROUTING} bind:active>
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>
{#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"
on:click={() => dbUtils.applyToSelectedFiles((file) => file.reverse())}
>
<ArrowRightLeft size="16" />
</Button>
<span slot="tooltip">{$_('toolbar.routing.reverse_tooltip')}</span>
</Tooltip>
<Tooltip>
<Button
slot="data"
variant="outline"
disabled={$selectedFiles.size != 1}
on:click={() => {
const fileId = get(selectedFiles).values().next().value;
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"
disabled={$selectedFiles.size != 1}
on:click={() => {
const fileId = get(selectedFiles).values().next().value;
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">
{#if $selectedFiles.size > 1}
<div>{$_('toolbar.routing.help_multiple_files')}</div>
{:else if $selectedFiles.size == 0}
<div>{$_('toolbar.routing.help_no_file')}</div>
{:else}
<div>{$_('toolbar.routing.help')}</div>
{/if}
</Help>
</ToolbarItemMenu>
2024-04-26 13:33:17 +02:00
<RoutingControlPopup bind:element={popupElement} />