From fa94cd407e59c7b2c431c9efad2f489ad6f4d650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 3 Apr 2023 21:37:51 +0800 Subject: [PATCH] Added the JavaScript setElementShown function in the journal entry form for readability. --- .../static/js/journal-entry-form.js | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/accounting/static/js/journal-entry-form.js b/src/accounting/static/js/journal-entry-form.js index 7aa7da3..83adcfd 100644 --- a/src/accounting/static/js/journal-entry-form.js +++ b/src/accounting/static/js/journal-entry-form.js @@ -457,11 +457,7 @@ class CurrencySubForm { * @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"); - } + setElementShown(this.#deleteButton, isShown); } /** @@ -705,15 +701,14 @@ class DebitCreditSubForm { this.#element.classList.add("accounting-not-empty"); this.currency.form.lineItemEditor.onAddNew(this); }; - this.#content.classList.add("d-none"); } else { this.#element.classList.add("accounting-not-empty"); this.#element.classList.remove("accounting-clickable"); delete this.#element.dataset.bsToggle; delete this.#element.dataset.bsTarget; this.#element.onclick = null; - this.#content.classList.remove("d-none"); } + setElementShown(this.#content, this.lineItems.length !== 0); } /** @@ -1056,11 +1051,7 @@ class LineItemSubForm { * @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"); - } + setElementShown(this.#deleteButton, isShown); } /** @@ -1090,19 +1081,14 @@ class LineItemSubForm { * @param editor {JournalEntryLineItemEditor} the line item editor */ save(editor) { - if (editor.account.isNeedOffset) { - this.#offsets.classList.remove("d-none"); - } else { - this.#offsets.classList.add("d-none"); - } + setElementShown(this.#offsets, editor.account.isNeedOffset); this.#originalLineItemId.value = editor.originalLineItemId === null? "": editor.originalLineItemId; this.#originalLineItemId.dataset.date = editor.originalLineItemDate === null? "": editor.originalLineItemDate; this.#originalLineItemId.dataset.text = editor.originalLineItemText === null? "": editor.originalLineItemText; + setElementShown(this.#originalLineItemText, editor.originalLineItemText !== null); if (editor.originalLineItemText === null) { - this.#originalLineItemText.classList.add("d-none"); this.#originalLineItemText.innerText = ""; } else { - this.#originalLineItemText.classList.remove("d-none"); this.#originalLineItemText.innerText = A_("Offset %(item)s", {item: editor.originalLineItemText}); } this.#accountCode.value = editor.account.code; @@ -1153,3 +1139,18 @@ function formatDecimal(number) { const whole = Number(number.minus(frac)).toLocaleString(); return whole + String(frac).substring(1); } + +/** + * Sets whether an element is shown. + * + * @param element {HTMLElement} the element + * @param isShown {boolean} true to show, or false otherwise + * @private + */ +function setElementShown(element, isShown) { + if (isShown) { + element.classList.remove("d-none"); + } else { + element.classList.add("d-none"); + } +}