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.

This commit is contained in:
依瑪貓 2023-03-23 08:40:19 +08:00
parent faee1e61c6
commit 562801692a

View File

@ -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.
*