From 562801692ab43c7efac3e6e4eb6172990abd875d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 23 Mar 2023 08:40:19 +0800 Subject: [PATCH] Added the JavaScript setDeleteButtonShown method to the CurrencySubForm and LineItemSubForm forms, and hides the implementation of the delete buttons from outside. Changed the delete buttons to private. --- .../static/js/journal-entry-form.js | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/accounting/static/js/journal-entry-form.js b/src/accounting/static/js/journal-entry-form.js index 456e856..200456d 100644 --- a/src/accounting/static/js/journal-entry-form.js +++ b/src/accounting/static/js/journal-entry-form.js @@ -159,7 +159,7 @@ class JournalEntryForm { */ #resetDeleteCurrencyButtons() { if (this.#currencies.length === 1) { - this.#currencies[0].deleteButton.classList.add("d-none"); + this.#currencies[0].setDeleteButtonShown(false); } else { for (const currency of this.#currencies) { let isAnyLineItemMatched = false; @@ -169,11 +169,7 @@ class JournalEntryForm { break; } } - if (isAnyLineItemMatched) { - currency.deleteButton.classList.add("d-none"); - } else { - currency.deleteButton.classList.remove("d-none"); - } + currency.setDeleteButtonShown(!isAnyLineItemMatched); } } } @@ -397,7 +393,7 @@ class CurrencySubForm { * The button to delete the currency * @type {HTMLButtonElement} */ - deleteButton; + #deleteButton; /** * The debit sub-form @@ -427,13 +423,13 @@ class CurrencySubForm { this.#no = document.getElementById(prefix + "-no"); this.#code = document.getElementById(prefix + "-code"); this.#codeSelect = document.getElementById(prefix + "-code-select"); - this.deleteButton = document.getElementById(prefix + "-delete"); + this.#deleteButton = document.getElementById(prefix + "-delete"); const debitElement = document.getElementById(prefix + "-debit"); this.#debit = debitElement === null? null: new DebitCreditSubForm(this, debitElement, "debit"); const creditElement = document.getElementById(prefix + "-credit"); this.#credit = creditElement == null? null: new DebitCreditSubForm(this, creditElement, "credit"); this.#codeSelect.onchange = () => this.#code.value = this.#codeSelect.value; - this.deleteButton.onclick = () => { + this.#deleteButton.onclick = () => { this.#element.parentElement.removeChild(this.#element); this.form.deleteCurrency(this); }; @@ -466,6 +462,19 @@ class CurrencySubForm { return this.#code.value; } + /** + * Sets whether the delete button is shown. + * + * @param isShown {boolean} true to show, or false otherwise + */ + setDeleteButtonShown(isShown) { + if (isShown) { + this.#deleteButton.classList.remove("d-none"); + } else { + this.#deleteButton.classList.add("d-none"); + } + } + /** * Returns all the line items in the form. * @@ -665,14 +674,10 @@ class DebitCreditSubForm { */ #resetDeleteLineItemButtons() { if (this.lineItems.length === 1) { - this.lineItems[0].deleteButton.classList.add("d-none"); + this.lineItems[0].setDeleteButtonShown(false); } else { for (const lineItem of this.lineItems) { - if (lineItem.isMatched) { - lineItem.deleteButton.classList.add("d-none"); - } else { - lineItem.deleteButton.classList.remove("d-none"); - } + lineItem.setDeleteButtonShown(!lineItem.isMatched); } } } @@ -859,7 +864,7 @@ class LineItemSubForm { * The button to delete line item * @type {HTMLButtonElement} */ - deleteButton; + #deleteButton; /** * Constructs the line item sub-form. @@ -886,9 +891,9 @@ class LineItemSubForm { this.#offsets = document.getElementById(prefix + "-offsets"); this.#amount = document.getElementById(prefix + "-amount"); this.#amountText = document.getElementById(prefix + "-amount-text"); - this.deleteButton = document.getElementById(prefix + "-delete"); + this.#deleteButton = document.getElementById(prefix + "-delete"); this.#control.onclick = () => this.debitCreditSubForm.currency.form.lineItemEditor.onEdit(this); - this.deleteButton.onclick = () => { + this.#deleteButton.onclick = () => { this.#element.parentElement.removeChild(this.#element); this.debitCreditSubForm.deleteLineItem(this); }; @@ -993,6 +998,19 @@ class LineItemSubForm { return this.#amount.dataset.min === ""? null: new Decimal(this.#amount.dataset.min); } + /** + * Sets whether the delete button is shown. + * + * @param isShown {boolean} true to show, or false otherwise + */ + setDeleteButtonShown(isShown) { + if (isShown) { + this.#deleteButton.classList.remove("d-none"); + } else { + this.#deleteButton.classList.add("d-none"); + } + } + /** * Validates the form. *