From 915e4408e143c76003372fe734e78d7086221ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sat, 18 Mar 2023 20:45:31 +0800 Subject: [PATCH] Revised the JavaScript to initialize the OriginalEntrySelector instance in JournalEntryEditor, so that the journal entry editor holds the OriginalEntrySelector instance. It can find the OriginalEntrySelector instance without needing to invoke its static methods. Removed the redundant static methods from the OriginalEntrySelector class. --- .../static/js/journal-entry-editor.js | 11 ++++- .../static/js/original-entry-selector.js | 45 +------------------ src/accounting/static/js/summary-editor.js | 2 +- 3 files changed, 12 insertions(+), 46 deletions(-) diff --git a/src/accounting/static/js/journal-entry-editor.js b/src/accounting/static/js/journal-entry-editor.js index 5db61d8..c60810b 100644 --- a/src/accounting/static/js/journal-entry-editor.js +++ b/src/accounting/static/js/journal-entry-editor.js @@ -208,6 +208,12 @@ class JournalEntryEditor { */ #accountSelectors = {}; + /** + * The original entry selector + * @type {OriginalEntrySelector} + */ + originalEntrySelector; + /** * Constructs a new journal entry editor. * @@ -236,7 +242,8 @@ class JournalEntryEditor { for (const entryType of ["debit", "credit"]) { this.#accountSelectors[entryType] = new AccountSelector(this, entryType); } - this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId); + this.originalEntrySelector = new OriginalEntrySelector(); + this.#originalEntryControl.onclick = () => this.originalEntrySelector.onOpen(this, this.originalEntryId) this.#originalEntryDelete.onclick = () => this.clearOriginalEntry(); this.#summaryControl.onclick = () => this.#summaryEditors[this.entryType].onOpen(); this.#accountControl.onclick = () => this.#accountSelectors[this.entryType].onOpen(); @@ -569,7 +576,7 @@ class JournalEntryEditor { if (this.originalEntryId === null) { return null; } - return OriginalEntrySelector.getNetBalance(this.entry, this.form, this.originalEntryId); + return this.originalEntrySelector.getNetBalance(this.entry, this.form, this.originalEntryId); } /** diff --git a/src/accounting/static/js/original-entry-selector.js b/src/accounting/static/js/original-entry-selector.js index d97c2a3..bf7957f 100644 --- a/src/accounting/static/js/original-entry-selector.js +++ b/src/accounting/static/js/original-entry-selector.js @@ -22,11 +22,6 @@ */ "use strict"; -// Initializes the page JavaScript. -document.addEventListener("DOMContentLoaded", () => { - OriginalEntrySelector.initialize(); -}); - /** * The original entry selector. * @@ -108,7 +103,7 @@ class OriginalEntrySelector { * @param originalEntryId {string} the ID of the original entry * @return {Decimal} the net balance of the original entry */ - #getNetBalance(currentEntry, form, originalEntryId) { + getNetBalance(currentEntry, form, originalEntryId) { const otherEntries = form.getEntries().filter((entry) => entry !== currentEntry); let otherOffset = new Decimal(0); for (const otherEntry of otherEntries) { @@ -178,7 +173,7 @@ class OriginalEntrySelector { * @param entryEditor {JournalEntryEditor} the journal entry editor * @param originalEntryId {string|null} the ID of the original entry */ - #onOpen(entryEditor, originalEntryId) { + onOpen(entryEditor, originalEntryId = null) { this.entryEditor = entryEditor this.#modal.dataset.currencyCode = entryEditor.getCurrencyCode(); this.#modal.dataset.entryType = entryEditor.entryType; @@ -189,42 +184,6 @@ class OriginalEntrySelector { this.#updateNetBalances(); this.#filterOptions(); } - - /** - * The original entry selector. - * @type {OriginalEntrySelector} - */ - static #selector; - - /** - * Initializes the original entry selector. - * - */ - static initialize() { - this.#selector = new OriginalEntrySelector(); - } - - /** - * Starts the original entry selector. - * - * @param entryEditor {JournalEntryEditor} the journal entry editor - * @param originalEntryId {string|null} the ID of the original entry - */ - static start(entryEditor, originalEntryId = null) { - this.#selector.#onOpen(entryEditor, originalEntryId); - } - - /** - * Returns the net balance for an original entry. - * - * @param currentEntry {JournalEntrySubForm} the journal entry sub-form that is currently editing - * @param form {TransactionForm} the transaction form - * @param originalEntryId {string} the ID of the original entry - * @return {Decimal} the net balance of the original entry - */ - static getNetBalance(currentEntry, form, originalEntryId) { - return this.#selector.#getNetBalance(currentEntry, form, originalEntryId); - } } /** diff --git a/src/accounting/static/js/summary-editor.js b/src/accounting/static/js/summary-editor.js index dad7d2f..8f336e0 100644 --- a/src/accounting/static/js/summary-editor.js +++ b/src/accounting/static/js/summary-editor.js @@ -132,7 +132,7 @@ class SummaryEditor { this.currentTab = this.tabPlanes.general; this.#initializeSuggestedAccounts(); this.summary.onchange = () => this.#onSummaryChange(); - this.#offsetButton.onclick = () => OriginalEntrySelector.start(this.#entryEditor); + this.#offsetButton.onclick = () => this.#entryEditor.originalEntrySelector.onOpen(this.#entryEditor); this.#form.onsubmit = () => { if (this.currentTab.validate()) { this.#submit();