Debian Codegraph @

frontend/src/tree-table/AccountCell.svelte

raw
1 <!--
2 @component
3 Account name cell.
4 -->
5 <script lang="ts">
6 import type { AccountTreeNode } from "../charts/hierarchy";
7 import { urlForAccount } from "../helpers";
8 import { leaf } from "../lib/account";
9 import AccountIndicator from "../sidebar/AccountIndicator.svelte";
10 import { getTreeTableContext } from "./helpers";
11
12 interface Props {
13 /** The account node to render the name cell for. */
14 node: AccountTreeNode;
15 }
16
17 let { node }: Props = $props();
18
19 const { toggled } = getTreeTableContext();
20
21 let { account, children } = $derived(node);
22
23 /**
24 * Toggle the account and (depending on the mouse event) its children in the set.
25 */
26 let on_click = $derived((event: MouseEvent) => {
27 toggled.update((t) => {
28 const new_t = new Set(t);
29 const is_toggled = new_t.has(account);
30 if (is_toggled) {
31 new_t.delete(account);
32 } else {
33 new_t.add(account);
34 }
35 if (event.shiftKey) {
36 // toggle all children as well.
37 const toggle_all = (n: AccountTreeNode) => {
38 if (is_toggled) {
39 new_t.delete(n.account);
40 } else {
41 new_t.add(n.account);
42 }
43 n.children.filter((c) => c.children.length > 0).forEach(toggle_all);
44 };
45 children.forEach(toggle_all);
46 }
47 if (is_toggled && (event.ctrlKey || event.metaKey)) {
48 // collapse all direct children to only expand one level
49 children
50 .filter((c) => c.children.length > 0)
51 .forEach((n) => {
52 new_t.add(n.account);
53 });
54 }
55 return new_t;
56 });
57 });
58 </script>
59
60 <span class="droptarget" data-account-name={account}>
61 {#if children.length > 0}
62 <button type="button" class="unset" onclick={on_click}>
63 {$toggled.has(account) ? "▸" : "▾"}
64 </button>
65 {/if}
66 <a href={$urlForAccount(account)} class="account">
67 {leaf(account)}
68 </a>
69 <AccountIndicator {account} small />
70 </span>
71
72 <style>
73 button {
74 position: absolute;
75 padding: 0 3px;
76 color: var(--treetable-expander);
77 }
78
79 a {
80 margin-left: 1em;
81 }
82
83 span {
84 display: flex;
85 flex: 1;
86 align-items: center;
87 min-width: calc(14em - var(--account-indent, 0em));
88 max-width: calc(30em - var(--account-indent, 0em));
89 margin-left: var(--account-indent, 0);
90 }
91
92 /* Indent each level of account by one more 1em. */
93 :global(ol ol) span {
94 --account-indent: 1em;
95 }
96
97 :global(ol ol ol) span {
98 --account-indent: 2em;
99 }
100
101 :global(ol ol ol ol) span {
102 --account-indent: 3em;
103 }
104
105 :global(ol ol ol ol ol) span {
106 --account-indent: 4em;
107 }
108
109 :global(ol ol ol ol ol ol) span {
110 --account-indent: 5em;
111 }
112
113 :global(ol ol ol ol ol ol ol) span {
114 --account-indent: 6em;
115 }
116
117 :global(ol ol ol ol ol ol ol ol) span {
118 --account-indent: 7em;
119 }
120
121 :global(ol ol ol ol ol ol ol ol ol) span {
122 --account-indent: 8em;
123 }
124 </style>