diff --git a/src/accounting/static/js/journal-entry-editor.js b/src/accounting/static/js/journal-entry-editor.js index ccf4343..a3b6a82 100644 --- a/src/accounting/static/js/journal-entry-editor.js +++ b/src/accounting/static/js/journal-entry-editor.js @@ -196,6 +196,12 @@ class JournalEntryEditor { */ amount = ""; + /** + * The summary editors + * @type {{debit: SummaryEditor, credit: SummaryEditor}} + */ + #summaryEditors; + /** * Constructs a new journal entry editor. * @@ -218,9 +224,10 @@ class JournalEntryEditor { this.#accountError = document.getElementById(this.#prefix + "-account-error") this.#amount = document.getElementById(this.#prefix + "-amount"); this.#amountError = document.getElementById(this.#prefix + "-amount-error"); + this.#summaryEditors = this.#initializeSummaryEditors(); this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId); this.#originalEntryDelete.onclick = () => this.clearOriginalEntry(); - this.#summaryControl.onclick = () => SummaryEditor.start(this); + this.#summaryControl.onclick = () => this.#summaryEditors[this.entryType].onOpen(); this.#accountControl.onclick = () => AccountSelector.start(this); this.#amount.onchange = () => this.#validateAmount(); this.#element.onsubmit = () => { @@ -236,6 +243,21 @@ class JournalEntryEditor { }; } + /** + * Initializes the summary editors. + * + * @return {{debit: SummaryEditor, credit: SummaryEditor}} the summary editors + */ + #initializeSummaryEditors() { + const editors = {}; + const forms = Array.from(document.getElementsByClassName("accounting-summary-editor")); + for (const form of forms) { + const summaryEditor = new SummaryEditor(this, form); + editors[summaryEditor.entryType] = summaryEditor; + } + return editors; + } + /** * Saves the original entry from the original entry selector. * diff --git a/src/accounting/static/js/summary-editor.js b/src/accounting/static/js/summary-editor.js index 97e20d8..6e93ed4 100644 --- a/src/accounting/static/js/summary-editor.js +++ b/src/accounting/static/js/summary-editor.js @@ -22,17 +22,18 @@ */ "use strict"; -// Initializes the page JavaScript. -document.addEventListener("DOMContentLoaded", () => { - SummaryEditor.initialize(); -}); - /** * A summary editor. * */ class SummaryEditor { + /** + * The journal entry editor + * @type {JournalEntryEditor} + */ + #entryEditor; + /** * The summary editor form * @type {HTMLFormElement} @@ -55,7 +56,7 @@ class SummaryEditor { * The entry type, either "debit" or "credit" * @type {string} */ - #entryType; + entryType; /** * The current tab @@ -99,12 +100,6 @@ class SummaryEditor { */ #selectedAccount = null; - /** - * The journal entry editor - * @type {JournalEntryEditor} - */ - #entryEditor; - /** * The tab planes * @type {{general: GeneralTagTab, travel: GeneralTripTab, bus: BusTripTab, regular: RegularPaymentTab, annotation: AnnotationTab}} @@ -114,11 +109,13 @@ class SummaryEditor { /** * Constructs a summary editor. * + * @param entryEditor {JournalEntryEditor} the journal entry editor * @param form {HTMLFormElement} the summary editor form */ - constructor(form) { + constructor(entryEditor, form) { + this.#entryEditor = entryEditor; this.#form = form; - this.#entryType = form.dataset.entryType; + this.entryType = form.dataset.entryType; this.prefix = "accounting-summary-editor-" + form.dataset.entryType; this.#modal = document.getElementById(this.prefix + "-modal"); this.summary = document.getElementById(this.prefix + "-summary"); @@ -227,12 +224,10 @@ class SummaryEditor { /** * The callback when the summary editor is shown. * - * @param entryEditor {JournalEntryEditor} the journal entry editor */ - #onOpen(entryEditor) { - this.#entryEditor = entryEditor; + onOpen() { this.#reset(); - this.summary.value = entryEditor.summary === null? "": entryEditor.summary; + this.summary.value = this.#entryEditor.summary === null? "": this.#entryEditor.summary; this.#onSummaryChange(); } @@ -247,33 +242,6 @@ class SummaryEditor { } this.tabPlanes.general.switchToMe(); } - - /** - * The summary editors. - * @type {{debit: SummaryEditor, credit: SummaryEditor}} - */ - static #editors = {} - - /** - * Initializes the summary editors. - * - */ - static initialize() { - const forms = Array.from(document.getElementsByClassName("accounting-summary-editor")); - for (const form of forms) { - const editor = new SummaryEditor(form); - this.#editors[editor.#entryType] = editor; - } - } - - /** - * The callback when the summary editor is shown. - * - * @param entryEditor {JournalEntryEditor} the journal entry editor - */ - static start(entryEditor) { - this.#editors[entryEditor.entryType].#onOpen(entryEditor); - } } /**