Replaced the forEach loops with the for-of loops whenever appropriate in the JavaScript for the transaction form.

This commit is contained in:
依瑪貓 2023-02-28 22:00:19 +08:00
parent 2680a1c872
commit bb7e9e94ee

View File

@ -79,12 +79,12 @@ function initializeCurrencyForms() {
btnNew.onclick = function () { btnNew.onclick = function () {
const currencies = Array.from(document.getElementsByClassName("accounting-currency")); const currencies = Array.from(document.getElementsByClassName("accounting-currency"));
let maxIndex = 0; let maxIndex = 0;
currencies.forEach(function (currency) { for (const currency of currencies) {
const index = parseInt(currency.dataset.index); const index = parseInt(currency.dataset.index);
if (maxIndex < index) { if (maxIndex < index) {
maxIndex = index; maxIndex = index;
} }
}); }
const newIndex = String(maxIndex + 1); const newIndex = String(maxIndex + 1);
const html = form.dataset.currencyTemplate const html = form.dataset.currencyTemplate
.replaceAll("CURRENCY_INDEX", escapeHtml(newIndex)); .replaceAll("CURRENCY_INDEX", escapeHtml(newIndex));
@ -122,9 +122,9 @@ function initializeBtnDeleteCurrency(button) {
function resetDeleteCurrencyButtons() { function resetDeleteCurrencyButtons() {
const buttons = Array.from(document.getElementsByClassName("accounting-btn-delete-currency")); const buttons = Array.from(document.getElementsByClassName("accounting-btn-delete-currency"));
if (buttons.length > 1) { if (buttons.length > 1) {
buttons.forEach(function (button) { for (const button of buttons) {
button.classList.remove("d-none"); button.classList.remove("d-none");
}); }
} else { } else {
buttons[0].classList.add("d-none"); buttons[0].classList.add("d-none");
} }
@ -263,13 +263,13 @@ function initializeJournalEntryFormModal() {
query.value = ""; query.value = "";
more.classList.remove("d-none"); more.classList.remove("d-none");
filterAccountOptions(prefix); filterAccountOptions(prefix);
options.forEach(function (option) { for (const option of options) {
if (option.dataset.code === formAccount.dataset.code) { if (option.dataset.code === formAccount.dataset.code) {
option.classList.add("active"); option.classList.add("active");
} else { } else {
option.classList.remove("active"); option.classList.remove("active");
} }
}); }
if (formAccount.dataset.code === "") { if (formAccount.dataset.code === "") {
btnClear.classList.add("btn-secondary"); btnClear.classList.add("btn-secondary");
btnClear.classList.remove("btn-danger"); btnClear.classList.remove("btn-danger");
@ -376,12 +376,12 @@ function saveJournalEntryForm() {
const entries = Array.from(document.getElementsByClassName("accounting-currency-" + currencyIndex + "-" + entryType)); const entries = Array.from(document.getElementsByClassName("accounting-currency-" + currencyIndex + "-" + entryType));
const entryList = document.getElementById("accounting-currency-" + currencyIndex + "-" + entryType + "-list") const entryList = document.getElementById("accounting-currency-" + currencyIndex + "-" + entryType + "-list")
let maxIndex = 0; let maxIndex = 0;
entries.forEach(function (entry) { for (const entry of entries) {
const index = parseInt(entry.dataset.entryIndex); const index = parseInt(entry.dataset.entryIndex);
if (maxIndex < index) { if (maxIndex < index) {
maxIndex = index; maxIndex = index;
} }
}); }
entryIndex = String(maxIndex + 1); entryIndex = String(maxIndex + 1);
const html = form.dataset.entryTemplate const html = form.dataset.entryTemplate
.replaceAll("CURRENCY_INDEX", escapeHtml(currencyIndex)) .replaceAll("CURRENCY_INDEX", escapeHtml(currencyIndex))
@ -446,9 +446,9 @@ function initializeDeleteJournalEntryButton(button) {
function resetDeleteJournalEntryButtons(sameClass) { function resetDeleteJournalEntryButtons(sameClass) {
const buttons = Array.from(document.getElementsByClassName(sameClass)); const buttons = Array.from(document.getElementsByClassName(sameClass));
if (buttons.length > 1) { if (buttons.length > 1) {
buttons.forEach(function (button) { for (const button of buttons) {
button.classList.remove("d-none"); button.classList.remove("d-none");
}); }
} else { } else {
buttons[0].classList.add("d-none"); buttons[0].classList.add("d-none");
} }
@ -466,11 +466,11 @@ function updateBalance(currencyIndex, entryType) {
const amounts = Array.from(document.getElementsByClassName(prefix + "-amount")); const amounts = Array.from(document.getElementsByClassName(prefix + "-amount"));
const totalText = document.getElementById(prefix + "-total"); const totalText = document.getElementById(prefix + "-total");
let total = new Decimal("0"); let total = new Decimal("0");
amounts.forEach(function (amount) { for (const amount of amounts) {
if (amount.value !== "") { if (amount.value !== "") {
total = total.plus(new Decimal(amount.value)); total = total.plus(new Decimal(amount.value));
} }
}); }
totalText.innerText = formatDecimal(total); totalText.innerText = formatDecimal(total);
} }
@ -483,7 +483,7 @@ function initializeAccountSelectors() {
const selectors = Array.from(document.getElementsByClassName("accounting-selector-modal")); const selectors = Array.from(document.getElementsByClassName("accounting-selector-modal"));
const formAccountControl = document.getElementById("accounting-entry-form-account-control"); const formAccountControl = document.getElementById("accounting-entry-form-account-control");
const formAccount = document.getElementById("accounting-entry-form-account"); const formAccount = document.getElementById("accounting-entry-form-account");
selectors.forEach(function (selector) { for (const selector of selectors) {
const more = document.getElementById(selector.dataset.prefix + "-more"); const more = document.getElementById(selector.dataset.prefix + "-more");
const btnClear = document.getElementById(selector.dataset.prefix + "-btn-clear"); const btnClear = document.getElementById(selector.dataset.prefix + "-btn-clear");
const options = Array.from(document.getElementsByClassName(selector.dataset.prefix + "-option")); const options = Array.from(document.getElementsByClassName(selector.dataset.prefix + "-option"));
@ -499,7 +499,7 @@ function initializeAccountSelectors() {
formAccount.dataset.text = ""; formAccount.dataset.text = "";
validateJournalEntryAccount(); validateJournalEntryAccount();
}; };
options.forEach(function (option) { for (const option of options) {
option.onclick = function () { option.onclick = function () {
formAccountControl.classList.add("accounting-not-empty"); formAccountControl.classList.add("accounting-not-empty");
formAccount.innerText = option.dataset.content; formAccount.innerText = option.dataset.content;
@ -507,8 +507,8 @@ function initializeAccountSelectors() {
formAccount.dataset.text = option.dataset.content; formAccount.dataset.text = option.dataset.content;
validateJournalEntryAccount(); validateJournalEntryAccount();
}; };
}); }
}); }
} }
/** /**
@ -541,7 +541,7 @@ function filterAccountOptions(prefix) {
const queryNoResult = document.getElementById(prefix + "-option-no-result"); const queryNoResult = document.getElementById(prefix + "-option-no-result");
const codesInUse = getAccountCodeUsedInForm(); const codesInUse = getAccountCodeUsedInForm();
let shouldAnyShow = false; let shouldAnyShow = false;
options.forEach(function (option) { for (const option of options) {
const shouldShow = shouldAccountOptionShow(option, more, codesInUse, query); const shouldShow = shouldAccountOptionShow(option, more, codesInUse, query);
if (shouldShow) { if (shouldShow) {
option.classList.remove("d-none"); option.classList.remove("d-none");
@ -549,7 +549,7 @@ function filterAccountOptions(prefix) {
} else { } else {
option.classList.add("d-none"); option.classList.add("d-none");
} }
}); }
if (!shouldAnyShow && more.classList.contains("d-none")) { if (!shouldAnyShow && more.classList.contains("d-none")) {
optionList.classList.add("d-none"); optionList.classList.add("d-none");
queryNoResult.classList.remove("d-none"); queryNoResult.classList.remove("d-none");
@ -601,9 +601,9 @@ function getAccountCodeUsedInForm() {
const accountCodes = Array.from(document.getElementsByClassName("accounting-account-code")); const accountCodes = Array.from(document.getElementsByClassName("accounting-account-code"));
const formAccount = document.getElementById("accounting-entry-form-account"); const formAccount = document.getElementById("accounting-entry-form-account");
const inUse = [formAccount.dataset.code]; const inUse = [formAccount.dataset.code];
accountCodes.forEach(function (accountCode) { for (const accountCode of accountCodes) {
inUse.push(accountCode.value); inUse.push(accountCode.value);
}); }
return inUse return inUse
} }
@ -665,9 +665,9 @@ function validateCurrencies() {
const currencies = Array.from(document.getElementsByClassName("accounting-currency")); const currencies = Array.from(document.getElementsByClassName("accounting-currency"));
let isValid = true; let isValid = true;
isValid = validateCurrenciesReal() && isValid; isValid = validateCurrenciesReal() && isValid;
currencies.forEach(function (currency) { for (const currency of currencies) {
isValid = validateCurrency(currency) && isValid; isValid = validateCurrency(currency) && isValid;
}); }
return isValid; return isValid;
} }
@ -728,9 +728,9 @@ function validateJournalEntries(currency, entryType) {
const entries = Array.from(document.getElementsByClassName("accounting-currency-" + currencyIndex + "-" + entryType)); const entries = Array.from(document.getElementsByClassName("accounting-currency-" + currencyIndex + "-" + entryType));
let isValid = true; let isValid = true;
isValid = validateJournalEntriesReal(currencyIndex, entryType) && isValid; isValid = validateJournalEntriesReal(currencyIndex, entryType) && isValid;
entries.forEach(function (entry) { for (const entry of entries) {
isValid = validateJournalEntry(entry) && isValid; isValid = validateJournalEntry(entry) && isValid;
}) }
return isValid; return isValid;
} }
@ -801,17 +801,17 @@ function validateBalance(currency) {
const creditAmounts = Array.from(document.getElementsByClassName(prefix + "-credit-amount")); const creditAmounts = Array.from(document.getElementsByClassName(prefix + "-credit-amount"));
if (debit !== null && credit !== null) { if (debit !== null && credit !== null) {
let debitTotal = new Decimal("0"); let debitTotal = new Decimal("0");
debitAmounts.forEach(function (amount) { for (const amount of debitAmounts) {
if (amount.value !== "") { if (amount.value !== "") {
debitTotal = debitTotal.plus(new Decimal(amount.value)); debitTotal = debitTotal.plus(new Decimal(amount.value));
} }
}); }
let creditTotal = new Decimal("0"); let creditTotal = new Decimal("0");
creditAmounts.forEach(function (amount) { for (const amount of creditAmounts) {
if (amount.value !== "") { if (amount.value !== "") {
creditTotal = creditTotal.plus(new Decimal(amount.value)); creditTotal = creditTotal.plus(new Decimal(amount.value));
} }
}); }
if (!debitTotal.equals(creditTotal)) { if (!debitTotal.equals(creditTotal)) {
control.classList.add("is-invalid"); control.classList.add("is-invalid");
error.innerText = A_("The totals of the debit and credit amounts do not match."); error.innerText = A_("The totals of the debit and credit amounts do not match.");