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

177 lines
4.4 KiB
Svelte
Raw Normal View History

2024-05-17 15:02:45 +02:00
<script lang="ts">
import { GPXFile, Track, Waypoint, type AnyGPXTreeElement, type GPXTreeElement } from 'gpx';
import { Button } from '$lib/components/ui/button';
2024-05-21 13:22:14 +02:00
import { getContext, onDestroy, onMount } from 'svelte';
2024-05-17 15:02:45 +02:00
import Sortable from 'sortablejs/Sortable';
import type { GPXFileWithStatistics } from '$lib/db';
2024-05-21 13:22:14 +02:00
import type { Readable, Writable } from 'svelte/store';
2024-05-17 15:02:45 +02:00
import FileListNodeStore from './FileListNodeStore.svelte';
import FileListNode from './FileListNode.svelte';
2024-05-21 13:22:14 +02:00
import FileListNodeLabel from './FileListNodeLabel.svelte';
2024-05-17 15:02:45 +02:00
export let node:
| Map<string, Readable<GPXFileWithStatistics | undefined>>
| GPXTreeElement<AnyGPXTreeElement>
| ReadonlyArray<Readonly<Waypoint>>;
2024-05-21 13:22:14 +02:00
export let waypointRoot: boolean = false;
2024-05-17 15:02:45 +02:00
export let id: string;
let container: HTMLElement;
2024-05-21 13:22:14 +02:00
let items: { [id: string | number]: HTMLElement } = {};
2024-05-17 15:02:45 +02:00
let sortableLevel =
node instanceof Map
? 'file'
: node instanceof GPXFile
2024-05-21 13:22:14 +02:00
? waypointRoot
? 'waypoints'
: 'track'
2024-05-17 15:02:45 +02:00
: node instanceof Track
? 'segment'
: 'waypoint';
2024-05-20 14:32:52 +02:00
let pull: Record<string, string[]> = {
file: ['file', 'track'],
track: ['file', 'track'],
segment: ['file', 'track', 'segment'],
waypoint: ['waypoint']
};
2024-05-17 15:02:45 +02:00
let sortable: Sortable;
2024-05-21 13:22:14 +02:00
let selected = getContext<Writable<Set<string>>>('selected');
function onSelectChange() {
selected.update(($selected) => {
$selected.clear();
Object.entries(items).forEach(([id, item]) => {
if (item.classList.contains('sortable-selected')) {
$selected.add(id);
}
});
return $selected;
});
}
2024-05-17 15:02:45 +02:00
onMount(() => {
sortable = Sortable.create(container, {
2024-05-20 14:32:52 +02:00
group: {
name: sortableLevel
},
2024-05-17 15:02:45 +02:00
forceAutoScrollFallback: true,
multiDrag: true,
multiDragKey: 'Meta',
2024-05-21 13:22:14 +02:00
avoidImplicitDeselect: true,
onSelect: onSelectChange,
onDeselect: onSelectChange,
sort: sortableLevel !== 'waypoint'
2024-05-17 15:02:45 +02:00
});
});
function handleClick(id: string) {
2024-05-21 13:22:14 +02:00
//console.log('handle click for', id);
}
const unsubscribe = selected.subscribe(($selected) => {
Object.entries(items).forEach(([id, item]) => {
if ($selected.has(id) && !item.classList.contains('sortable-selected')) {
Sortable.utils.select(item);
} else if (!$selected.has(id) && item.classList.contains('sortable-selected')) {
Sortable.utils.deselect(item);
}
});
});
onDestroy(() => {
unsubscribe();
});
function getChildId(i: number): string {
switch (sortableLevel) {
case 'track':
return `${id}-track-${i}`;
case 'segment':
return `${id}-seg-${i}`;
case 'waypoint':
return `${id}-${i}`;
}
return '';
2024-05-17 15:02:45 +02:00
}
</script>
2024-05-21 13:22:14 +02:00
<div bind:this={container} class="sortable flex flex-col">
2024-05-17 15:02:45 +02:00
{#if node instanceof Map}
2024-05-21 13:22:14 +02:00
{#each node as [fileId, file]}
<div bind:this={items[fileId]}>
2024-05-17 15:02:45 +02:00
<FileListNodeStore {file} on:click={(e) => handleClick(e.detail.id)} />
</div>
{/each}
{:else if node instanceof GPXFile}
2024-05-21 13:22:14 +02:00
{#if waypointRoot}
{#if node.wpt.length > 0}
<div bind:this={items[`${id}-wpt`]}>
<FileListNode
node={node.wpt}
id={`${id}-wpt`}
on:click={(e) => handleClick(e.detail.id)}
/>
</div>
{/if}
{:else}
{#each node.children as child, i}
<div bind:this={items[getChildId(i)]}>
<FileListNode
node={child}
id={getChildId(i)}
index={i}
on:click={(e) => handleClick(e.detail.id)}
/>
</div>
{/each}
2024-05-17 15:02:45 +02:00
{/if}
{:else if node instanceof Track}
{#each node.children as child, i}
2024-05-21 13:22:14 +02:00
<div bind:this={items[getChildId(i)]}>
2024-05-20 14:32:52 +02:00
<div>
<Button
variant="ghost"
2024-05-21 13:22:14 +02:00
class="p-0 px-1 h-fit w-full"
on:click={() => handleClick(getChildId(i))}
2024-05-20 14:32:52 +02:00
>
2024-05-21 13:22:14 +02:00
<FileListNodeLabel id={getChildId(i)} label={`Segment ${i + 1}`} />
</Button>
2024-05-20 14:32:52 +02:00
</div>
2024-05-17 15:02:45 +02:00
</div>
{/each}
{:else if Array.isArray(node) && node.length > 0 && node[0] instanceof Waypoint}
{#each node as wpt, i}
2024-05-21 13:22:14 +02:00
<div bind:this={items[getChildId(i)]}>
2024-05-20 14:32:52 +02:00
<div>
<Button
variant="ghost"
2024-05-21 13:22:14 +02:00
class="p-0 px-1 h-fit w-full"
on:click={() => handleClick(getChildId(i))}
2024-05-20 14:32:52 +02:00
>
2024-05-21 13:22:14 +02:00
<FileListNodeLabel id={getChildId(i)} label={wpt.name ?? `Waypoint ${i + 1}`} />
</Button>
2024-05-20 14:32:52 +02:00
</div>
2024-05-17 15:02:45 +02:00
</div>
{/each}
{/if}
</div>
2024-05-21 13:22:14 +02:00
{#if node instanceof GPXFile}
{#if !waypointRoot}
<svelte:self {node} {id} waypointRoot={true} />
{/if}
{/if}
2024-05-17 15:02:45 +02:00
<style lang="postcss">
2024-05-21 13:22:14 +02:00
.sortable > div {
@apply rounded-md;
@apply h-fit;
@apply leading-none;
}
div :global(.sortable-selected) {
2024-05-17 15:02:45 +02:00
@apply bg-accent;
}
</style>