| 1 |
import { mount, unmount } from "svelte"; |
| 2 |
|
| 3 |
import { delegate } from "../lib/events"; |
| 4 |
import { sortableJournal } from "../sort"; |
| 5 |
import { fql_filter } from "../stores/filters"; |
| 6 |
import { journalShow } from "../stores/journal"; |
| 7 |
import JournalFilters from "./JournalFilters.svelte"; |
| 8 |
|
| 9 |
/** |
| 10 |
* Escape the value to produce a valid regex for the Fava filter. |
| 11 |
*/ |
| 12 |
export function escape(value: string): string { |
| 13 |
return value.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Add a filter to the existing list of filters. Any parts that are interpreted |
| 18 |
* as a regex must be escaped. |
| 19 |
*/ |
| 20 |
function addFilter(value: string): void { |
| 21 |
fql_filter.update((fql_filter_val) => |
| 22 |
fql_filter_val ? `${fql_filter_val} ${value}` : value, |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
function handleClick({ target }: Event): void { |
| 27 |
if (!(target instanceof HTMLElement) || target instanceof HTMLAnchorElement) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if (target.className === "tag" || target.className === "link") { |
| 32 |
// Filter for tags and links when clicking on them. |
| 33 |
addFilter(target.innerText); |
| 34 |
} else if (target.className === "payee") { |
| 35 |
// Filter for payees when clicking on them. |
| 36 |
// Note: any special characters in the payee string are escaped so the |
| 37 |
// filter matches against the payee literally. |
| 38 |
addFilter(`payee:"^${escape(target.innerText)}$"`); |
| 39 |
} else if (target.tagName === "DT") { |
| 40 |
// Filter for metadata key when clicking on the key. The key tag text |
| 41 |
// includes the colon. |
| 42 |
const expr = `${target.innerText}""`; |
| 43 |
if (target.closest(".postings")) { |
| 44 |
// Posting metadata. |
| 45 |
addFilter(`any(${expr})`); |
| 46 |
} else { |
| 47 |
// Entry metadata. |
| 48 |
addFilter(expr); |
| 49 |
} |
| 50 |
} else if (target.tagName === "DD") { |
| 51 |
// Filter for metadata key and value when clicking on the value. The key |
| 52 |
// tag text includes the colon. |
| 53 |
const key = (target.previousElementSibling as HTMLElement).innerText; |
| 54 |
const value = `"^${escape(target.innerText)}$"`; |
| 55 |
const expr = `${key}${value}`; |
| 56 |
if (target.closest(".postings")) { |
| 57 |
// Posting metadata. |
| 58 |
addFilter(`any(${expr})`); |
| 59 |
} else { |
| 60 |
// Entry metadata. |
| 61 |
addFilter(expr); |
| 62 |
} |
| 63 |
} else if (target.closest(".indicators")) { |
| 64 |
// Toggle postings and metadata by clicking on indicators. |
| 65 |
const entry = target.closest(".journal > li"); |
| 66 |
if (entry) { |
| 67 |
entry.classList.toggle("show-full-entry"); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
export class FavaJournal extends HTMLElement { |
| 73 |
/** Unmount the Svelte component. */ |
| 74 |
unmount?: () => void; |
| 75 |
|
| 76 |
/** Unsubscribe store listener. */ |
| 77 |
unsubscribe?: () => void; |
| 78 |
|
| 79 |
connectedCallback(): void { |
| 80 |
const ol = this.querySelector("ol"); |
| 81 |
if (!ol) { |
| 82 |
throw new Error("fava-journal is missing its <ol>"); |
| 83 |
} |
| 84 |
|
| 85 |
this.unsubscribe = journalShow.subscribe((show) => { |
| 86 |
const classes = [...show].map((s) => `show-${s}`).join(" "); |
| 87 |
ol.className = `flex-table journal ${classes}`; |
| 88 |
}); |
| 89 |
const component = mount(JournalFilters, { target: this, anchor: ol }); |
| 90 |
this.unmount = () => { |
| 91 |
void unmount(component); |
| 92 |
}; |
| 93 |
|
| 94 |
sortableJournal(ol); |
| 95 |
delegate(this, "click", "li", handleClick); |
| 96 |
} |
| 97 |
|
| 98 |
disconnectedCallback(): void { |
| 99 |
this.unsubscribe?.(); |
| 100 |
this.unmount?.(); |
| 101 |
} |
| 102 |
} |