| 1 |
<script lang="ts"> |
| 2 |
import { leaf } from "../../lib/account"; |
| 3 |
import type { TreeNode } from "../../lib/tree"; |
| 4 |
import Accounts from "./Accounts.svelte"; |
| 5 |
import { selectedAccount } from "./stores"; |
| 6 |
|
| 7 |
interface Props { |
| 8 |
node: TreeNode<{ name: string; count: number }>; |
| 9 |
move: (m: { account: string; filename: string }) => void; |
| 10 |
} |
| 11 |
|
| 12 |
let { node, move }: Props = $props(); |
| 13 |
|
| 14 |
let expanded = $state(true); |
| 15 |
let drag = $state(false); |
| 16 |
|
| 17 |
let hasChildren = $derived(node.children.length > 0); |
| 18 |
let selected = $derived($selectedAccount === node.name); |
| 19 |
|
| 20 |
/** |
| 21 |
* Start drag if a document filename is dragged onto an account. |
| 22 |
* @param event - The drag event that is passed to the event handler. |
| 23 |
*/ |
| 24 |
function dragenter(event: DragEvent) { |
| 25 |
const types = event.dataTransfer?.types ?? []; |
| 26 |
if (types.includes("fava/filename")) { |
| 27 |
event.preventDefault(); |
| 28 |
drag = true; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Handle a drop and bubble the event. |
| 34 |
* @param event - The drag event that is passed to the event handler. |
| 35 |
*/ |
| 36 |
function drop(event: DragEvent) { |
| 37 |
event.preventDefault(); |
| 38 |
const filename = event.dataTransfer?.getData("fava/filename"); |
| 39 |
if (filename != null) { |
| 40 |
move({ account: node.name, filename }); |
| 41 |
drag = false; |
| 42 |
} |
| 43 |
} |
| 44 |
</script> |
| 45 |
|
| 46 |
{#if node.name} |
| 47 |
<p |
| 48 |
ondragenter={dragenter} |
| 49 |
ondragover={dragenter} |
| 50 |
ondragleave={() => { |
| 51 |
drag = false; |
| 52 |
}} |
| 53 |
ondrop={drop} |
| 54 |
title={node.name} |
| 55 |
class="droptarget" |
| 56 |
data-account-name={node.name} |
| 57 |
class:selected |
| 58 |
class:drag |
| 59 |
> |
| 60 |
{#if hasChildren} |
| 61 |
<button |
| 62 |
type="button" |
| 63 |
class="unset toggle" |
| 64 |
onclick={(ev) => { |
| 65 |
expanded = !expanded; |
| 66 |
ev.stopPropagation(); |
| 67 |
}}>{expanded ? "▾" : "▸"}</button |
| 68 |
> |
| 69 |
{/if} |
| 70 |
<button |
| 71 |
type="button" |
| 72 |
class="unset leaf" |
| 73 |
onclick={() => { |
| 74 |
$selectedAccount = selected ? "" : node.name; |
| 75 |
}}>{leaf(node.name)}</button |
| 76 |
> |
| 77 |
{#if node.count > 0} |
| 78 |
<span class="count"> {node.count}</span> |
| 79 |
{/if} |
| 80 |
</p> |
| 81 |
{/if} |
| 82 |
{#if hasChildren} |
| 83 |
<ul hidden={!expanded}> |
| 84 |
{#each node.children as child} |
| 85 |
<li> |
| 86 |
<Accounts node={child} {move} /> |
| 87 |
</li> |
| 88 |
{/each} |
| 89 |
</ul> |
| 90 |
{/if} |
| 91 |
|
| 92 |
<style> |
| 93 |
ul { |
| 94 |
padding: 0 0 0 0.5em; |
| 95 |
margin: 0; |
| 96 |
} |
| 97 |
|
| 98 |
p { |
| 99 |
position: relative; |
| 100 |
display: flex; |
| 101 |
padding-right: 0.5em; |
| 102 |
margin: 0; |
| 103 |
overflow: hidden; |
| 104 |
border-bottom: 1px solid var(--table-border); |
| 105 |
border-left: 1px solid var(--table-border); |
| 106 |
} |
| 107 |
|
| 108 |
p > * { |
| 109 |
padding: 1px; |
| 110 |
} |
| 111 |
|
| 112 |
.count { |
| 113 |
opacity: 0.6; |
| 114 |
} |
| 115 |
|
| 116 |
.selected, |
| 117 |
.drag { |
| 118 |
background-color: var(--table-header-background); |
| 119 |
} |
| 120 |
|
| 121 |
.leaf { |
| 122 |
flex-grow: 1; |
| 123 |
margin-left: 1em; |
| 124 |
} |
| 125 |
|
| 126 |
.toggle { |
| 127 |
position: absolute; |
| 128 |
margin: 0 0.25rem; |
| 129 |
color: var(--treetable-expander); |
| 130 |
} |
| 131 |
</style> |