Replaced parsing the HTML element ID with the HTML custom data attributes in the JavaScripts.

This commit is contained in:
2020-08-07 22:31:08 +08:00
parent d6e68717fd
commit 09c1f453f4
10 changed files with 81 additions and 103 deletions

View File

@ -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");
}
/**

View File

@ -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($("<option/>"));
account.append($("<option/>"));
}
const headerInUse = $("<option/>")
.attr("disabled", "disabled")
.text(accountOptions["header_in_use"]);
jAccount.append(headerInUse);
account.append(headerInUse);
accountOptions[type + "_in_use"].forEach(function (item) {
// Skips the cash account on cash transactions.
if (item["code"] === 1111 && isCash) {
@ -138,12 +137,12 @@ function initializeAccountOptions(account) {
if (String(item["code"]) === selectedAccount) {
option.attr("selected", "selected");
}
jAccount.append(option);
account.append(option);
});
const headerNotInUse = $("<option/>")
.attr("disabled", "disabled")
.text(accountOptions["header_not_in_use"]);
jAccount.append(headerNotInUse);
account.append(headerNotInUse);
accountOptions[type + "_not_in_use"].forEach(function (item) {
const option = $("<option/>")
.attr("value", item["code"])
@ -151,7 +150,7 @@ function initializeAccountOptions(account) {
if (String(item["code"]) === selectedAccount) {
option.attr("selected", "selected");
}
jAccount.append(option);
account.append(option);
});
}
@ -172,16 +171,12 @@ function removeBlankOption(select) {
/**
* Updates the total amount.
*
* @param {HTMLButtonElement|HTMLInputElement} element the amount
* element that
* changed, or the
* button that
* was hit to
* delete a record
* @param {jQuery} element the amount element that changed, or the
* button that was hit to delete a record
* @private
*/
function updateTotalAmount(element) {
const type = element.id.substring(0, element.id.indexOf("-"));
const type = element.data("type")
let total = 0;
$("." + type + "-to-sum").each(function () {
if (this.value !== "") {
@ -198,16 +193,16 @@ function updateTotalAmount(element) {
/**
* Adds a new accounting record.
*
* @param {HTMLButtonElement} button the button element that was hit
* to add a new record
* @param {jQuery} button the button element that was hit to add a
* new record
* @private
*/
function addNewRecord(button) {
const type = button.id.substring(0, button.id.indexOf("-"));
const type = button.data("type");
// Finds the new number that is the maximum number plus 1.
let newNo = 0;
$("." + type + "-record").each(function () {
const no = parseInt(this.id.substring(type.length + 1));
const no = parseInt($(this).data("no"));
if (newNo < no) {
newNo = no;
}
@ -242,7 +237,7 @@ function insertNewRecord(type, newNo) {
validateAccount(this);
})
.each(function () {
initializeAccountOptions(this);
initializeAccountOptions($(this));
});
$("#" + type + "-" + newNo + "-summary")
.on("blur", function () {
@ -258,25 +253,30 @@ function insertNewRecord(type, newNo) {
validateAmount(this);
})
.on("change", function () {
updateTotalAmount(this);
updateTotalAmount($(this));
validateBalance();
});
$("#" + type + "-" + newNo + "-delete")
.on("click", function () {
deleteRecord(this);
deleteRecord($(this));
});
$("#" + type + "-" + newNo + "-m-delete")
.on("click", function () {
deleteRecord($(this));
});
}
/**
* Deletes a record.
*
* @param {HTMLButtonElement} button the button element that was hit
* to delete this record
* @param {jQuery} button the button element that was hit to delete
* this record
* @private
*/
function deleteRecord(button) {
const type = button.id.substring(0, button.id.indexOf("-"));
const no = parseInt(button.id.substring(type.length + 1, button.id.indexOf("-", type.length + 1)));
const type = button.data("type");
const no = button.data("no");
console.log("#" + type + "-" + no);
$("#" + type + "-" + no).remove();
resetRecordOrders(type);
resetRecordButtons();