| 1 |
/** |
| 2 |
* Fava's main Javascript entry point. |
| 3 |
* |
| 4 |
* The code for Fava's UI is split into several modules that are all imported |
| 5 |
* below. The most interactive parts are written as Svelte components. Some |
| 6 |
* other functionality is written in plain Javascript and or web components |
| 7 |
* extending normal HTML elements. |
| 8 |
*/ |
| 9 |
|
| 10 |
import "../css/style.css"; |
| 11 |
import "../css/base.css"; |
| 12 |
import "../css/layout.css"; |
| 13 |
import "../css/charts.css"; |
| 14 |
import "../css/components.css"; |
| 15 |
import "../css/editor.css"; |
| 16 |
import "../css/grid.css"; |
| 17 |
import "../css/fonts.css"; |
| 18 |
import "../css/help.css"; |
| 19 |
import "../css/journal-table.css"; |
| 20 |
import "../css/notifications.css"; |
| 21 |
import "../css/tree-table.css"; |
| 22 |
// Polyfill for customised builtin elements in Webkit |
| 23 |
import "@ungap/custom-elements"; |
| 24 |
|
| 25 |
import { get as store_get } from "svelte/store"; |
| 26 |
|
| 27 |
import { get } from "./api"; |
| 28 |
import { ledgerDataValidator } from "./api/validators"; |
| 29 |
import { CopyableText } from "./clipboard"; |
| 30 |
import { BeancountTextarea } from "./codemirror/setup"; |
| 31 |
import { handleExtensionPageLoad } from "./extensions"; |
| 32 |
import { _ } from "./i18n"; |
| 33 |
import { FavaJournal } from "./journal"; |
| 34 |
import { initGlobalKeyboardShortcuts } from "./keyboard-shortcuts"; |
| 35 |
import { getScriptTagValue } from "./lib/dom"; |
| 36 |
import { log_error } from "./log"; |
| 37 |
import { notify, notify_err } from "./notifications"; |
| 38 |
import { frontend_routes } from "./reports/routes"; |
| 39 |
import router, { setStoreValuesFromURL, syncStoreValuesToURL } from "./router"; |
| 40 |
import { initSidebar } from "./sidebar"; |
| 41 |
import { has_changes, updatePageTitle } from "./sidebar/page-title"; |
| 42 |
import { SortableTable } from "./sort/sortable-table"; |
| 43 |
import { errors, fava_options, ledgerData } from "./stores"; |
| 44 |
import { ledger_mtime, read_mtime } from "./stores/mtime"; |
| 45 |
import { SvelteCustomElement } from "./svelte-custom-elements"; |
| 46 |
import { TreeTableCustomElement } from "./tree-table/tree-table-custom-element"; |
| 47 |
|
| 48 |
/** |
| 49 |
* Define the custom elements that Fava uses. |
| 50 |
*/ |
| 51 |
function defineCustomElements() { |
| 52 |
customElements.define("beancount-textarea", BeancountTextarea, { |
| 53 |
extends: "textarea", |
| 54 |
}); |
| 55 |
customElements.define("copyable-text", CopyableText); |
| 56 |
customElements.define("fava-journal", FavaJournal); |
| 57 |
customElements.define("sortable-table", SortableTable, { extends: "table" }); |
| 58 |
customElements.define("svelte-component", SvelteCustomElement); |
| 59 |
|
| 60 |
// for extension compatibility |
| 61 |
customElements.define("tree-table", TreeTableCustomElement); |
| 62 |
} |
| 63 |
|
| 64 |
router.on("page-loaded", () => { |
| 65 |
read_mtime(); |
| 66 |
updatePageTitle(); |
| 67 |
has_changes.set(false); |
| 68 |
handleExtensionPageLoad(); |
| 69 |
}); |
| 70 |
|
| 71 |
/** |
| 72 |
* Update the ledger data and errors; Reload if automatic reloading is configured. |
| 73 |
*/ |
| 74 |
function onChanges() { |
| 75 |
get("ledger_data") |
| 76 |
.then((v) => { |
| 77 |
ledgerData.set(v); |
| 78 |
}) |
| 79 |
.catch((e: unknown) => { |
| 80 |
notify_err(e, (err) => `Error fetching ledger data: ${err.message}`); |
| 81 |
}); |
| 82 |
if (store_get(fava_options).auto_reload && !router.hasInteruptHandler) { |
| 83 |
router.reload(); |
| 84 |
} else { |
| 85 |
get("errors").then((v) => { |
| 86 |
errors.set(v); |
| 87 |
}, log_error); |
| 88 |
notify(_("File change detected. Click to reload."), "warning", () => { |
| 89 |
router.reload(); |
| 90 |
}); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Check the `changed` API endpoint. |
| 96 |
* |
| 97 |
* Updates of the mtime returned by this endpoint will fire the appropriate events if some |
| 98 |
* file changed. |
| 99 |
* |
| 100 |
* This will be scheduled every 5 seconds. |
| 101 |
*/ |
| 102 |
function pollForChanges(): void { |
| 103 |
get("changed").catch(log_error); |
| 104 |
} |
| 105 |
|
| 106 |
function init(): void { |
| 107 |
const initial = getScriptTagValue("#ledger-data", ledgerDataValidator); |
| 108 |
if (initial.is_ok) { |
| 109 |
ledgerData.set(initial.value); |
| 110 |
} else { |
| 111 |
log_error(initial.error); |
| 112 |
} |
| 113 |
read_mtime(); |
| 114 |
|
| 115 |
let initial_mtime = true; |
| 116 |
ledger_mtime.subscribe(() => { |
| 117 |
if (initial_mtime) { |
| 118 |
initial_mtime = false; |
| 119 |
return; |
| 120 |
} |
| 121 |
has_changes.set(true); |
| 122 |
onChanges(); |
| 123 |
}); |
| 124 |
|
| 125 |
router.init(frontend_routes); |
| 126 |
setStoreValuesFromURL(); |
| 127 |
syncStoreValuesToURL(); |
| 128 |
initSidebar(); |
| 129 |
initGlobalKeyboardShortcuts(); |
| 130 |
defineCustomElements(); |
| 131 |
setInterval(pollForChanges, 5000); |
| 132 |
|
| 133 |
ledgerData.subscribe((val) => { |
| 134 |
errors.set(val.errors); |
| 135 |
}); |
| 136 |
|
| 137 |
router.trigger("page-loaded"); |
| 138 |
} |
| 139 |
|
| 140 |
init(); |