| 1 |
<script lang="ts"> |
| 2 |
import AutocompleteInput from "../AutocompleteInput.svelte"; |
| 3 |
import { _ } from "../i18n"; |
| 4 |
import { escape } from "../journal"; |
| 5 |
import { accounts, links, payees, tags, years } from "../stores"; |
| 6 |
import { account_filter, fql_filter, time_filter } from "../stores/filters"; |
| 7 |
|
| 8 |
let fql_filter_suggestions = $derived([ |
| 9 |
...$tags.map((tag) => `#${tag}`), |
| 10 |
...$links.map((link) => `^${link}`), |
| 11 |
...$payees.map((payee) => `payee:"${escape(payee)}"`), |
| 12 |
...$payees.map((payee) => `payee:"${escape(payee)}"`), |
| 13 |
]); |
| 14 |
|
| 15 |
function valueExtractor(value: string, input: HTMLInputElement) { |
| 16 |
const match = /\S*$/.exec( |
| 17 |
value.slice(0, input.selectionStart ?? undefined), |
| 18 |
); |
| 19 |
return match?.[0] ?? value; |
| 20 |
} |
| 21 |
function valueSelector(value: string, input: HTMLInputElement) { |
| 22 |
const selectionStart = input.selectionStart ?? 0; |
| 23 |
const match = /\S*$/.exec(input.value.slice(0, selectionStart)); |
| 24 |
const matchLength = match?.[0]?.length; |
| 25 |
return matchLength !== undefined |
| 26 |
? `${input.value.slice( |
| 27 |
0, |
| 28 |
selectionStart - matchLength, |
| 29 |
)}${value}${input.value.slice(selectionStart)}` |
| 30 |
: value; |
| 31 |
} |
| 32 |
|
| 33 |
let account_filter_value = $state(""); |
| 34 |
let fql_filter_value = $state(""); |
| 35 |
let time_filter_value = $state(""); |
| 36 |
account_filter.subscribe((v) => { |
| 37 |
account_filter_value = v; |
| 38 |
}); |
| 39 |
fql_filter.subscribe((v) => { |
| 40 |
fql_filter_value = v; |
| 41 |
}); |
| 42 |
time_filter.subscribe((v) => { |
| 43 |
time_filter_value = v; |
| 44 |
}); |
| 45 |
|
| 46 |
/** |
| 47 |
* Submit the filter form. |
| 48 |
* |
| 49 |
* This is called on all the three possible events (blur, select, enter) |
| 50 |
* and also on the form submit. Having the listener on:enter would |
| 51 |
* theoretically be unnecessary (as the form would also be submitted) but |
| 52 |
* it seems to work around a Safari bug, see #809 and #1528. |
| 53 |
*/ |
| 54 |
function submit() { |
| 55 |
account_filter.set(account_filter_value); |
| 56 |
fql_filter.set(fql_filter_value); |
| 57 |
time_filter.set(time_filter_value); |
| 58 |
} |
| 59 |
</script> |
| 60 |
|
| 61 |
<form |
| 62 |
onsubmit={(ev) => { |
| 63 |
ev.preventDefault(); |
| 64 |
submit(); |
| 65 |
}} |
| 66 |
> |
| 67 |
<AutocompleteInput |
| 68 |
bind:value={time_filter_value} |
| 69 |
placeholder={_("Time")} |
| 70 |
suggestions={$years} |
| 71 |
key="f t" |
| 72 |
clearButton={true} |
| 73 |
setSize={true} |
| 74 |
onBlur={submit} |
| 75 |
onSelect={submit} |
| 76 |
onEnter={submit} |
| 77 |
/> |
| 78 |
<AutocompleteInput |
| 79 |
bind:value={account_filter_value} |
| 80 |
placeholder={_("Account")} |
| 81 |
suggestions={$accounts} |
| 82 |
key="f a" |
| 83 |
clearButton={true} |
| 84 |
setSize={true} |
| 85 |
onBlur={submit} |
| 86 |
onSelect={submit} |
| 87 |
onEnter={submit} |
| 88 |
/> |
| 89 |
<AutocompleteInput |
| 90 |
bind:value={fql_filter_value} |
| 91 |
placeholder={_("Filter by tag, payee, ...")} |
| 92 |
suggestions={fql_filter_suggestions} |
| 93 |
key="f f" |
| 94 |
clearButton={true} |
| 95 |
setSize={true} |
| 96 |
{valueExtractor} |
| 97 |
{valueSelector} |
| 98 |
onBlur={submit} |
| 99 |
onSelect={submit} |
| 100 |
onEnter={submit} |
| 101 |
/> |
| 102 |
<!-- svelte-ignore a11y_consider_explicit_label --> |
| 103 |
<button type="submit"></button> |
| 104 |
</form> |
| 105 |
|
| 106 |
<style> |
| 107 |
form { |
| 108 |
display: flex; |
| 109 |
flex-wrap: wrap; |
| 110 |
gap: 0.5em; |
| 111 |
margin: 0; |
| 112 |
color: var(--text-color); |
| 113 |
|
| 114 |
--placeholder-color: var(--header-placeholder-color); |
| 115 |
--placeholder-background: var(--header-placeholder-background); |
| 116 |
} |
| 117 |
|
| 118 |
form > :global(span) { |
| 119 |
max-width: 18rem; |
| 120 |
} |
| 121 |
|
| 122 |
form :global(input) { |
| 123 |
padding: 8px 25px 8px 10px; |
| 124 |
outline: none; |
| 125 |
background-color: var(--background); |
| 126 |
border: 0; |
| 127 |
} |
| 128 |
|
| 129 |
form :global([type="text"]:focus) { |
| 130 |
background-color: var(--background); |
| 131 |
} |
| 132 |
|
| 133 |
[type="submit"] { |
| 134 |
display: none; |
| 135 |
} |
| 136 |
|
| 137 |
@media print { |
| 138 |
form :global(input) { |
| 139 |
padding: 8px 10px; |
| 140 |
} |
| 141 |
|
| 142 |
form :global(input):placeholder-shown { |
| 143 |
display: none; |
| 144 |
} |
| 145 |
} |
| 146 |
</style> |