| 1 |
<script lang="ts" module> |
| 2 |
import { _, format } from "../i18n"; |
| 3 |
import type { KeySpec } from "../keyboard-shortcuts"; |
| 4 |
import { keyboardShortcut } from "../keyboard-shortcuts"; |
| 5 |
import { journalShow } from "../stores/journal"; |
| 6 |
|
| 7 |
const toggleText = _("Toggle %(type)s entries"); |
| 8 |
|
| 9 |
/** |
| 10 |
* This is the list of all toggle buttons to show. |
| 11 |
* For some entry types we have subtypes (like 'cleared' for 'transaction'), |
| 12 |
* which get special-cased in the toggle logic below. |
| 13 |
*/ |
| 14 |
const buttons: [ |
| 15 |
type: string, |
| 16 |
button_text: string, |
| 17 |
title: string | null, |
| 18 |
shortcut: KeySpec, |
| 19 |
supertype?: string, |
| 20 |
][] = [ |
| 21 |
["open", "Open", null, "s o"], |
| 22 |
["close", "Close", null, "s c"], |
| 23 |
["transaction", "Transaction", null, "s t"], |
| 24 |
["cleared", "*", _("Cleared transactions"), "t c", "transaction"], |
| 25 |
["pending", "!", _("Pending transactions"), "t p", "transaction"], |
| 26 |
["other", "x", _("Other transactions"), "t o", "transaction"], |
| 27 |
["balance", "Balance", null, "s b"], |
| 28 |
["note", "Note", null, "s n"], |
| 29 |
["document", "Document", null, "s d"], |
| 30 |
[ |
| 31 |
"discovered", |
| 32 |
"D", |
| 33 |
_("Documents with a #discovered tag"), |
| 34 |
"d d", |
| 35 |
"document", |
| 36 |
], |
| 37 |
["linked", "L", _("Documents with a #linked tag"), "d l", "document"], |
| 38 |
["pad", "Pad", null, "s p"], |
| 39 |
["query", "Query", null, "s q"], |
| 40 |
["custom", "Custom", null, "s C"], |
| 41 |
["budget", "B", _("Budget entries"), "s B", "custom"], |
| 42 |
["metadata", _("Metadata"), _("Toggle metadata"), "m"], |
| 43 |
["postings", _("Postings"), _("Toggle postings"), "p"], |
| 44 |
]; |
| 45 |
</script> |
| 46 |
|
| 47 |
<script lang="ts"> |
| 48 |
let shownSet = $derived(new Set($journalShow)); |
| 49 |
|
| 50 |
function toggle(type: string) { |
| 51 |
journalShow.update((show) => { |
| 52 |
const set = new Set(show); |
| 53 |
const toggle_func = set.has(type) |
| 54 |
? set.delete.bind(set) |
| 55 |
: set.add.bind(set); |
| 56 |
toggle_func(type); |
| 57 |
// Also toggle all entries that have `type` as their supertype. |
| 58 |
buttons.filter((b) => b[4] === type).forEach((b) => toggle_func(b[0])); |
| 59 |
return [...set].sort(); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
let active = $derived((type: string, supertype?: string): boolean => |
| 64 |
supertype != null |
| 65 |
? shownSet.has(type) && shownSet.has(supertype) |
| 66 |
: shownSet.has(type), |
| 67 |
); |
| 68 |
</script> |
| 69 |
|
| 70 |
<form class="flex-row"> |
| 71 |
{#each buttons as [type, button_text, title, shortcut, supertype]} |
| 72 |
<button |
| 73 |
type="button" |
| 74 |
title={title ?? format(toggleText, { type: button_text })} |
| 75 |
use:keyboardShortcut={shortcut} |
| 76 |
class:inactive={!active(type, supertype)} |
| 77 |
onclick={() => { |
| 78 |
toggle(type); |
| 79 |
}} |
| 80 |
> |
| 81 |
{button_text} |
| 82 |
</button> |
| 83 |
{/each} |
| 84 |
</form> |
| 85 |
|
| 86 |
<style> |
| 87 |
form { |
| 88 |
justify-content: flex-end; |
| 89 |
} |
| 90 |
</style> |