From f5149a0c37017308806c4a0a666c402e3068ed91 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 22:36:50 +0800 Subject: [PATCH] Replaced the long parameter list with the JournalEntrySubForm instance in the onEdit method of the JavaScript JournalEntryEditor, to simplify the code. --- .../static/js/journal-entry-editor.js | 40 ++++++++----------- src/accounting/static/js/transaction-form.js | 38 +++++++++++++++++- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/accounting/static/js/journal-entry-editor.js b/src/accounting/static/js/journal-entry-editor.js index c60810b..7c091db 100644 --- a/src/accounting/static/js/journal-entry-editor.js +++ b/src/accounting/static/js/journal-entry-editor.js @@ -519,51 +519,43 @@ class JournalEntryEditor { * The callback when editing a journal entry. * * @param entry {JournalEntrySubForm} the journal entry sub-form - * @param originalEntryId {string} the ID of the original entry - * @param originalEntryDate {string} the date of the original entry - * @param originalEntryText {string} the text of the original entry - * @param summary {string} the summary - * @param accountCode {string} the account code - * @param accountText {string} the account text - * @param amount {string} the amount - * @param amountMin {string} the minimal amount */ - onEdit(entry, originalEntryId, originalEntryDate, originalEntryText, summary, accountCode, accountText, amount, amountMin) { + onEdit(entry) { this.entry = entry; this.#side = entry.side; this.entryType = this.#side.entryType; this.isNeedOffset = entry.isNeedOffset(); - if (originalEntryId === "") { + this.originalEntryId = entry.getOriginalEntryId(); + this.originalEntryDate = entry.getOriginalEntryDate(); + this.originalEntryText = entry.getOriginalEntryText(); + this.#originalEntry.innerText = this.originalEntryText; + if (this.originalEntryId === null) { this.#originalEntryContainer.classList.add("d-none"); this.#originalEntryControl.classList.remove("accounting-not-empty"); } else { this.#originalEntryContainer.classList.remove("d-none"); this.#originalEntryControl.classList.add("accounting-not-empty"); } - this.originalEntryId = originalEntryId === ""? null: originalEntryId; - this.originalEntryDate = originalEntryDate === ""? null: originalEntryDate; - this.originalEntryText = originalEntryText === ""? null: originalEntryText; - this.#originalEntry.innerText = originalEntryText; - this.#setEnableSummaryAccount(!entry.isMatched && originalEntryId === ""); - if (summary === "") { + this.#setEnableSummaryAccount(!entry.isMatched && this.originalEntryId === null); + this.summary = entry.getSummary(); + if (this.summary === null) { this.#summaryControl.classList.remove("accounting-not-empty"); } else { this.#summaryControl.classList.add("accounting-not-empty"); } - this.summary = summary === ""? null: summary; - this.#summary.innerText = summary; - if (accountCode === "") { + this.#summary.innerText = this.summary === null? "": this.summary; + if (entry.getAccountCode() === null) { this.#accountControl.classList.remove("accounting-not-empty"); } else { this.#accountControl.classList.add("accounting-not-empty"); } - this.accountCode = accountCode; - this.accountText = accountText; - this.#account.innerText = accountText; - this.#amount.value = amount; + this.accountCode = entry.getAccountCode(); + this.accountText = entry.getAccountText(); + this.#account.innerText = this.accountText; + this.#amount.value = entry.getAmount() === null? "": String(entry.getAmount()); const maxAmount = this.#getMaxAmount(); this.#amount.max = maxAmount === null? "": maxAmount; - this.#amount.min = amountMin; + this.#amount.min = entry.getAmountMin() === null? "": String(entry.getAmountMin()); this.#validate(); } diff --git a/src/accounting/static/js/transaction-form.js b/src/accounting/static/js/transaction-form.js index 4628827..7b6b325 100644 --- a/src/accounting/static/js/transaction-form.js +++ b/src/accounting/static/js/transaction-form.js @@ -881,7 +881,7 @@ class JournalEntrySubForm { this.#amount = document.getElementById(this.#prefix + "-amount"); this.#amountText = document.getElementById(this.#prefix + "-amount-text"); this.deleteButton = document.getElementById(this.#prefix + "-delete"); - this.#control.onclick = () => this.side.currency.form.entryEditor.onEdit(this, this.#originalEntryId.value, this.#originalEntryId.dataset.date, this.#originalEntryId.dataset.text, this.#summary.value, this.#accountCode.value, this.#accountCode.dataset.text, this.#amount.value, this.#amount.dataset.min); + this.#control.onclick = () => this.side.currency.form.entryEditor.onEdit(this); this.deleteButton.onclick = () => { this.element.parentElement.removeChild(this.element); this.side.deleteJournalEntry(this); @@ -915,6 +915,24 @@ class JournalEntrySubForm { return this.#originalEntryId.dataset.date === ""? null: this.#originalEntryId.dataset.date; } + /** + * Returns the text of the original entry. + * + * @return {string|null} the text of the original entry + */ + getOriginalEntryText() { + return this.#originalEntryId.dataset.text === ""? null: this.#originalEntryId.dataset.text; + } + + /** + * Returns the summary. + * + * @return {string|null} the summary + */ + getSummary() { + return this.#summary.value === ""? null: this.#summary.value; + } + /** * Returns the account code. * @@ -924,6 +942,15 @@ class JournalEntrySubForm { return this.#accountCode.value === ""? null: this.#accountCode.value; } + /** + * Returns the account text. + * + * @return {string|null} the account text + */ + getAccountText() { + return this.#accountCode.dataset.text === ""? null: this.#accountCode.dataset.text; + } + /** * Returns the amount. * @@ -933,6 +960,15 @@ class JournalEntrySubForm { return this.#amount.value === ""? null: new Decimal(this.#amount.value); } + /** + * Returns the minimal amount. + * + * @return {Decimal|null} the minimal amount + */ + getAmountMin() { + return this.#amount.dataset.min === ""? null: new Decimal(this.#amount.dataset.min); + } + /** * Validates the form. *