From 09c1f453f421a219b2121cc11aaa4ce3c013449d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Fri, 7 Aug 2020 22:31:08 +0800 Subject: [PATCH] Replaced parsing the HTML element ID with the HTML custom data attributes in the JavaScripts. --- .../static/accounting/js/summary-helper.js | 45 +++++-------- .../static/accounting/js/transaction-form.js | 64 +++++++++---------- .../include/form-record-non-transfer.html | 12 ++-- .../include/form-record-transfer.html | 10 +-- .../accounting/include/summary-helper.html | 10 +-- .../accounting/transactions/expense/form.html | 2 +- .../accounting/transactions/income/form.html | 2 +- .../transactions/transfer/form.html | 6 +- mia_core/static/mia_core/js/period-chooser.js | 25 ++------ .../mia_core/include/period-chooser.html | 8 +-- 10 files changed, 81 insertions(+), 103 deletions(-) diff --git a/accounting/static/accounting/js/summary-helper.js b/accounting/static/accounting/js/summary-helper.js index 8b35294..92009b5 100644 --- a/accounting/static/accounting/js/summary-helper.js +++ b/accounting/static/accounting/js/summary-helper.js @@ -30,7 +30,7 @@ $(function () { }); $(".record-summary") .on("click", function () { - startSummaryHelper(this); + startSummaryHelper($(this)); }); $("#summary-summary") .on("change", function () { @@ -39,7 +39,7 @@ $(function () { }); $(".summary-tab") .on("click", function () { - switchSummaryTab(this); + switchSummaryTab($(this)); }); // The general categories $("#summary-general-category") @@ -136,17 +136,17 @@ function loadSummaryCategoryData() { /** * Starts the summary helper. * - * @param {HTMLInputElement} summary the summary input element + * @param {jQuery} summary the summary input element */ function startSummaryHelper(summary) { - const type = summary.id.substring(0, summary.id.indexOf("-")); - const no = parseInt(summary.id.substring(type.length + 1, summary.id.indexOf("-", type.length + 1))); - $("#summary-record").get(0).value = type + "-" + no; - $("#summary-summary").get(0).value = summary.value; + const type = summary.data("type"); + const no = summary.data("no"); + $("#summary-record").val(type + "-" + no); + $("#summary-summary").val(summary.val()); // Loads the know summary categories into the summary helper loadKnownSummaryCategories(type); // Parses the summary and sets up the summary helper - parseSummaryForHelper(summary.value); + parseSummaryForHelper(summary.val()); // Focus on the summary input setTimeout(function () { $("#summary-summary").get(0).focus(); @@ -274,7 +274,7 @@ function parseSummaryForCategoryHelpers(summary) { $("#summary-bus-route").get(0).value = matchBus[2]; $("#summary-bus-from").get(0).value = matchBus[3]; $("#summary-bus-to").get(0).value = matchBus[4]; - switchSummaryTab($("#summary-tab-bus").get(0)); + switchSummaryTab($("#summary-tab-bus")); return; } @@ -288,12 +288,12 @@ function parseSummaryForCategoryHelpers(summary) { $("#summary-travel-direction").get(0).value = matchTravel[3]; setSummaryTravelDirectionButtons(matchTravel[3]); $("#summary-travel-to").get(0).value = matchTravel[4]; - switchSummaryTab($("#summary-tab-travel").get(0)); + switchSummaryTab($("#summary-tab-travel")); return; } // A general category - const generalCategoryTab = $("#summary-tab-category").get(0); + const generalCategoryTab = $("#summary-tab-category"); const matchCategory = summary.match(/^(.+)—.+(?:×[0-9]+)?$/); if (matchCategory !== null) { $("#summary-general-category").get(0).value = matchCategory[1]; @@ -312,26 +312,15 @@ function parseSummaryForCategoryHelpers(summary) { /** * Switch the summary helper to tab. * - * @param {HTMLElement} tab the navigation tab corresponding to a type - * of helper + * @param {jQuery} tab the navigation tab corresponding to a type + * of helper * @private */ function switchSummaryTab(tab) { - const tabName = tab.id.substr("summary-tab-".length); // "summary-tab-" - $(".summary-tab-content").each(function () { - if (this.id === "summary-tab-content-" + tabName) { - this.classList.remove("d-none"); - } else { - this.classList.add("d-none"); - } - }); - $(".summary-tab").each(function () { - if (this.id === tab.id) { - this.classList.add("active"); - } else { - this.classList.remove("active"); - } - }); + $(".summary-tab-content").addClass("d-none"); + $("#summary-tab-content-" + tab.data("tab")).removeClass("d-none"); + $(".summary-tab").removeClass("active"); + tab.addClass("active"); } /** diff --git a/accounting/static/accounting/js/transaction-form.js b/accounting/static/accounting/js/transaction-form.js index 41d860f..9808e3b 100644 --- a/accounting/static/accounting/js/transaction-form.js +++ b/accounting/static/accounting/js/transaction-form.js @@ -45,7 +45,7 @@ $(function () { validateAmount(this); }) .on("change", function () { - updateTotalAmount(this); + updateTotalAmount($(this)); validateBalance(); }); $("#txn-note") @@ -58,11 +58,11 @@ $(function () { }); $(".btn-new") .on("click", function () { - addNewRecord(this); + addNewRecord($(this)); }); $(".btn-del-record") .on("click", function () { - deleteRecord(this); + deleteRecord($(this)); }); }); @@ -95,7 +95,7 @@ function getAccountOptions() { if (this.readyState === 4 && this.status === 200) { accountOptions = JSON.parse(this.responseText); $(".record-account").each(function () { - initializeAccountOptions(this); + initializeAccountOptions($(this)); }); } }; @@ -106,27 +106,26 @@ function getAccountOptions() { /** * Initialize the account options. * - * @param {HTMLSelectElement} account the account select element + * @param {jQuery} account the account select element * @private */ function initializeAccountOptions(account) { - const jAccount = $(account); - const type = account.id.substring(0, account.id.indexOf("-")); - const selectedAccount = account.value; + const type = account.data("type"); + const selectedAccount = account.val(); let isCash = false; if (type === "debit") { isCash = ($(".credit-record").length === 0); } else if (type === "credit") { isCash = ($(".debit-record").length === 0); } - jAccount.html(""); + account.html(""); if (selectedAccount === "") { - jAccount.append($("