Files
gpx.studio/website/src/lib/components/file-list/FileListNode.svelte

78 lines
2.3 KiB
Svelte
Raw Normal View History

2024-05-16 18:18:42 +02:00
<script lang="ts">
2024-06-05 14:51:32 +02:00
import {
GPXFile,
Track,
TrackSegment,
Waypoint,
type AnyGPXTreeElement,
type GPXTreeElement
} from 'gpx';
2024-05-17 15:02:45 +02:00
import { CollapsibleTreeNode } from '$lib/components/collapsible-tree/index';
2024-05-24 13:16:41 +02:00
import { settings, type GPXFileWithStatistics } from '$lib/db';
import { get, type Readable } from 'svelte/store';
2024-05-17 15:02:45 +02:00
import FileListNodeContent from './FileListNodeContent.svelte';
2024-05-21 13:22:14 +02:00
import FileListNodeLabel from './FileListNodeLabel.svelte';
2024-05-24 13:16:41 +02:00
import { getContext, onDestroy } from 'svelte';
2024-06-05 14:51:32 +02:00
import {
ListTrackSegmentItem,
ListWaypointItem,
type ListItem,
type ListTrackItem
} from './FileList';
2024-05-24 13:16:41 +02:00
import { _ } from 'svelte-i18n';
import { selection } from './Selection';
2024-05-16 18:18:42 +02:00
2024-05-17 15:02:45 +02:00
export let node:
| Map<string, Readable<GPXFileWithStatistics | undefined>>
| GPXTreeElement<AnyGPXTreeElement>
| ReadonlyArray<Readonly<Waypoint>>;
2024-05-22 16:05:31 +02:00
export let item: ListItem;
2024-05-17 15:02:45 +02:00
let recursive = getContext<boolean>('recursive');
2024-05-17 15:02:45 +02:00
2024-05-24 13:16:41 +02:00
let collapsible: CollapsibleTreeNode;
2024-05-23 11:21:57 +02:00
$: label =
node instanceof GPXFile
? node.metadata.name
: node instanceof Track
2024-05-24 13:16:41 +02:00
? node.name ?? `${$_('gpx.track')} ${(item as ListTrackItem).trackIndex + 1}`
2024-06-05 14:51:32 +02:00
: node instanceof TrackSegment
? `${$_('gpx.segment')} ${(item as ListTrackSegmentItem).segmentIndex + 1}`
: node instanceof Waypoint
? node.name ?? `${$_('gpx.waypoint')} ${(item as ListWaypointItem).waypointIndex + 1}`
: Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint
? $_('gpx.waypoints')
: '';
2024-05-24 13:16:41 +02:00
const { verticalFileView } = settings;
const unsubscribe = selection.subscribe(($selection) => {
if (collapsible && get(verticalFileView) && $selection.hasAnyChildren(item, false)) {
collapsible.openNode();
}
});
onDestroy(() => {
unsubscribe();
});
2024-05-16 18:18:42 +02:00
</script>
2024-05-17 15:02:45 +02:00
{#if node instanceof Map}
2024-05-22 16:05:31 +02:00
<FileListNodeContent {node} {item} />
2024-06-05 14:51:32 +02:00
{:else if node instanceof TrackSegment}
<FileListNodeLabel {item} {label} />
{:else if node instanceof Waypoint}
<FileListNodeLabel {item} {label} />
{:else if recursive}
2024-05-24 13:16:41 +02:00
<CollapsibleTreeNode id={item.getId()} bind:this={collapsible}>
2024-05-22 16:05:31 +02:00
<FileListNodeLabel {item} {label} slot="trigger" />
2024-05-17 15:02:45 +02:00
<div slot="content">
2024-06-05 17:19:03 +02:00
{#key node}
<FileListNodeContent {node} {item} />
{/key}
2024-05-16 18:18:42 +02:00
</div>
</CollapsibleTreeNode>
{:else}
2024-05-22 16:05:31 +02:00
<FileListNodeLabel {item} {label} />
2024-05-16 18:18:42 +02:00
{/if}