From 08dc24605dbb9ca5cf47d34d8f08936a4f3000aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Tue, 28 Feb 2023 22:09:39 +0800 Subject: [PATCH] Replaced the forEach loops with the for-of loops in the JavaScript for the currency form, account form, and the drag-and-drop reorder library functions. --- src/accounting/static/js/account-form.js | 18 +++++++++--------- src/accounting/static/js/currency-form.js | 4 ++-- .../static/js/drag-and-drop-reorder.js | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/accounting/static/js/account-form.js b/src/accounting/static/js/account-form.js index ac6300e..2023129 100644 --- a/src/accounting/static/js/account-form.js +++ b/src/accounting/static/js/account-form.js @@ -46,9 +46,9 @@ function initializeBaseAccountSelector() { const btnClear = document.getElementById("accounting-btn-clear-base"); selector.addEventListener("show.bs.modal", function () { base.classList.add("accounting-not-empty"); - options.forEach(function (item) { - item.classList.remove("active"); - }); + for (const option of options) { + option.classList.remove("active"); + } const selected = document.getElementById("accounting-base-option-" + baseCode.value); if (selected !== null) { selected.classList.add("active"); @@ -59,7 +59,7 @@ function initializeBaseAccountSelector() { base.classList.remove("accounting-not-empty"); } }); - options.forEach(function (option) { + for (const option of options) { option.onclick = function () { baseCode.value = option.dataset.code; baseContent.innerText = option.dataset.content; @@ -69,7 +69,7 @@ function initializeBaseAccountSelector() { validateBase(); bootstrap.Modal.getInstance(selector).hide(); }; - }); + } btnClear.onclick = function () { baseCode.value = ""; baseContent.innerText = ""; @@ -94,15 +94,15 @@ function initializeBaseAccountQuery() { const queryNoResult = document.getElementById("accounting-base-option-no-result"); query.addEventListener("input", function () { if (query.value === "") { - options.forEach(function (option) { + for (const option of options) { option.classList.remove("d-none"); - }); + } optionList.classList.remove("d-none"); queryNoResult.classList.add("d-none"); return } let hasAnyMatched = false; - options.forEach(function (option) { + for (const option of options) { const queryValues = JSON.parse(option.dataset.queryValues); let isMatched = false; for (const queryValue of queryValues) { @@ -117,7 +117,7 @@ function initializeBaseAccountQuery() { } else { option.classList.add("d-none"); } - }); + } if (!hasAnyMatched) { optionList.classList.add("d-none"); queryNoResult.classList.remove("d-none"); diff --git a/src/accounting/static/js/currency-form.js b/src/accounting/static/js/currency-form.js index aa5b7cc..c97e582 100644 --- a/src/accounting/static/js/currency-form.js +++ b/src/accounting/static/js/currency-form.js @@ -65,9 +65,9 @@ function validateForm() { */ function submitFormIfAllAsyncValid() { let isValid = true; - Object.keys(isAsyncValid).forEach(function (key) { + for (const key of Object.keys(isAsyncValid)) { isValid = isAsyncValid[key] && isValid; - }); + } if (isValid) { document.getElementById("accounting-form").submit() } diff --git a/src/accounting/static/js/drag-and-drop-reorder.js b/src/accounting/static/js/drag-and-drop-reorder.js index 9f8141e..0ff2733 100644 --- a/src/accounting/static/js/drag-and-drop-reorder.js +++ b/src/accounting/static/js/drag-and-drop-reorder.js @@ -42,7 +42,7 @@ function initializeDragAndDropReordering(list, onReorder) { function initializeMouseDragAndDropReordering(list, onReorder) { const items = Array.from(list.children); let dragged = null; - items.forEach(function (item) { + for (const item of items) { item.draggable = true; item.addEventListener("dragstart", function () { dragged = item; @@ -56,7 +56,7 @@ function initializeMouseDragAndDropReordering(list, onReorder) { dragged.classList.remove("accounting-dragged"); dragged = null; }); - }); + } } /** @@ -68,7 +68,7 @@ function initializeMouseDragAndDropReordering(list, onReorder) { */ function initializeTouchDragAndDropReordering(list, onReorder) { const items = Array.from(list.children); - items.forEach(function (item) { + for (const item of items) { item.addEventListener("touchstart", function () { item.classList.add("accounting-dragged"); }); @@ -81,7 +81,7 @@ function initializeTouchDragAndDropReordering(list, onReorder) { item.addEventListener("touchend", function () { item.classList.remove("accounting-dragged"); }); - }); + } } /**