Files
gpx.studio/website/src/lib/components/layer-control/LayerTreeNode.svelte

118 lines
2.9 KiB
Svelte
Raw Normal View History

2024-04-12 13:43:51 +02:00
<script lang="ts">
2024-04-12 13:58:35 +02:00
import { Label } from '$lib/components/ui/label';
import { Checkbox } from '$lib/components/ui/checkbox';
2024-04-12 15:51:57 +02:00
import { Button } from '$lib/components/ui/button';
import * as Collapsible from '$lib/components/ui/collapsible';
2024-04-15 10:33:47 +02:00
import { ChevronDown, ChevronUp } from 'lucide-svelte';
2024-04-12 13:58:35 +02:00
2024-05-04 23:50:27 +02:00
import {
type CheckedInfoTreeType,
type CollapsedInfoTreeType,
type LayerTreeType
} from '$lib/assets/layers';
2024-04-12 13:43:51 +02:00
2024-04-24 17:39:56 +02:00
import { _ } from 'svelte-i18n';
2024-04-12 13:43:51 +02:00
export let name: string;
export let node: LayerTreeType;
2024-05-04 23:50:27 +02:00
export let selected: string | undefined = undefined;
2024-04-12 13:43:51 +02:00
export let multiple: boolean = false;
2024-05-04 23:50:27 +02:00
export let open: CollapsedInfoTreeType<boolean>;
2024-04-12 15:51:57 +02:00
if (!Array.isArray(node)) {
Object.keys(node).forEach((id) => {
if (!open.children.hasOwnProperty(id)) {
open.children[id] = {
self: true,
children: {}
};
}
2024-04-12 15:51:57 +02:00
});
}
2024-05-04 23:50:27 +02:00
export let checked: CheckedInfoTreeType;
if (Array.isArray(node)) {
if (multiple) {
node.forEach((id) => {
if (!checked.hasOwnProperty(id)) {
checked[id] = false;
}
});
}
} else {
Object.keys(node).forEach((id) => {
if (!checked.hasOwnProperty(id)) {
checked[id] = {};
}
});
}
2024-04-12 13:43:51 +02:00
</script>
2024-04-12 17:22:04 +02:00
{#if Array.isArray(node)}
<div class="flex flex-col gap-1 mt-1">
2024-04-12 13:43:51 +02:00
{#each node as id}
2024-04-12 15:12:27 +02:00
<div class="flex flex-row items-center gap-2">
2024-04-12 13:43:51 +02:00
{#if multiple}
2024-04-12 13:58:35 +02:00
<Checkbox
2024-05-04 23:50:27 +02:00
id="{name}-{id}"
2024-04-12 13:43:51 +02:00
{name}
value={id}
2024-04-12 13:58:35 +02:00
bind:checked={checked[id]}
2024-04-12 15:12:27 +02:00
class="scale-90"
2024-04-12 13:43:51 +02:00
/>
{:else}
2024-05-04 23:50:27 +02:00
<input id="{name}-{id}" type="radio" {name} value={id} bind:group={selected} />
2024-04-12 13:43:51 +02:00
{/if}
2024-05-04 23:50:27 +02:00
<Label for="{name}-{id}" class="flex flex-row items-center gap-1"
>{$_(`layers.label.${id}`)}</Label
>
2024-04-12 13:43:51 +02:00
</div>
{/each}
2024-04-12 17:22:04 +02:00
</div>
{:else}
<div class="flex flex-col gap-1">
2024-04-12 13:43:51 +02:00
{#each Object.keys(node) as id}
<Collapsible.Root bind:open={open.children[id].self} class="ml-1">
2024-04-12 15:51:57 +02:00
<Collapsible.Trigger class="w-full"
><Button
variant="ghost"
class="w-full flex flex-row justify-between py-0 px-1 h-fit hover:bg-background"
>
2024-04-24 17:39:56 +02:00
<span class="mr-2">{$_(`layers.label.${id}`)}</span>
{#if open.children[id].self}
2024-04-15 10:33:47 +02:00
<ChevronUp size="16" />
2024-04-12 15:51:57 +02:00
{:else}
2024-04-15 10:33:47 +02:00
<ChevronDown size="16" />
2024-04-12 15:51:57 +02:00
{/if}
</Button></Collapsible.Trigger
>
2024-04-12 17:22:04 +02:00
<Collapsible.Content class="ml-1">
<svelte:self
node={node[id]}
{name}
2024-05-04 23:50:27 +02:00
bind:selected
{multiple}
bind:open={open.children[id]}
2024-05-04 23:50:27 +02:00
bind:checked={checked[id]}
/>
2024-04-12 15:51:57 +02:00
</Collapsible.Content>
</Collapsible.Root>
2024-04-12 13:43:51 +02:00
{/each}
2024-04-12 17:22:04 +02:00
</div>
{/if}
2024-04-12 15:12:27 +02:00
<style lang="postcss">
div :global(input[type='radio']) {
@apply appearance-none;
@apply w-4 h-4;
@apply border-[1.5px] border-primary;
@apply rounded-full;
@apply ring-offset-background focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2;
@apply cursor-pointer;
@apply checked:bg-primary;
@apply checked:bg-clip-content;
@apply checked:p-0.5;
}
</style>