Replaced the long parameter list with the JournalEntrySubForm instance in the onEdit method of the JavaScript JournalEntryEditor, to simplify the code.

This commit is contained in:
依瑪貓 2023-03-18 22:36:50 +08:00
parent ca928636fd
commit f5149a0c37
2 changed files with 53 additions and 25 deletions

View File

@ -519,51 +519,43 @@ class JournalEntryEditor {
* The callback when editing a journal entry.
*
* @param entry {JournalEntrySubForm} the journal entry sub-form
* @param originalEntryId {string} the ID of the original entry
* @param originalEntryDate {string} the date of the original entry
* @param originalEntryText {string} the text of the original entry
* @param summary {string} the summary
* @param accountCode {string} the account code
* @param accountText {string} the account text
* @param amount {string} the amount
* @param amountMin {string} the minimal amount
*/
onEdit(entry, originalEntryId, originalEntryDate, originalEntryText, summary, accountCode, accountText, amount, amountMin) {
onEdit(entry) {
this.entry = entry;
this.#side = entry.side;
this.entryType = this.#side.entryType;
this.isNeedOffset = entry.isNeedOffset();
if (originalEntryId === "") {
this.originalEntryId = entry.getOriginalEntryId();
this.originalEntryDate = entry.getOriginalEntryDate();
this.originalEntryText = entry.getOriginalEntryText();
this.#originalEntry.innerText = this.originalEntryText;
if (this.originalEntryId === null) {
this.#originalEntryContainer.classList.add("d-none");
this.#originalEntryControl.classList.remove("accounting-not-empty");
} else {
this.#originalEntryContainer.classList.remove("d-none");
this.#originalEntryControl.classList.add("accounting-not-empty");
}
this.originalEntryId = originalEntryId === ""? null: originalEntryId;
this.originalEntryDate = originalEntryDate === ""? null: originalEntryDate;
this.originalEntryText = originalEntryText === ""? null: originalEntryText;
this.#originalEntry.innerText = originalEntryText;
this.#setEnableSummaryAccount(!entry.isMatched && originalEntryId === "");
if (summary === "") {
this.#setEnableSummaryAccount(!entry.isMatched && this.originalEntryId === null);
this.summary = entry.getSummary();
if (this.summary === null) {
this.#summaryControl.classList.remove("accounting-not-empty");
} else {
this.#summaryControl.classList.add("accounting-not-empty");
}
this.summary = summary === ""? null: summary;
this.#summary.innerText = summary;
if (accountCode === "") {
this.#summary.innerText = this.summary === null? "": this.summary;
if (entry.getAccountCode() === null) {
this.#accountControl.classList.remove("accounting-not-empty");
} else {
this.#accountControl.classList.add("accounting-not-empty");
}
this.accountCode = accountCode;
this.accountText = accountText;
this.#account.innerText = accountText;
this.#amount.value = amount;
this.accountCode = entry.getAccountCode();
this.accountText = entry.getAccountText();
this.#account.innerText = this.accountText;
this.#amount.value = entry.getAmount() === null? "": String(entry.getAmount());
const maxAmount = this.#getMaxAmount();
this.#amount.max = maxAmount === null? "": maxAmount;
this.#amount.min = amountMin;
this.#amount.min = entry.getAmountMin() === null? "": String(entry.getAmountMin());
this.#validate();
}

View File

@ -881,7 +881,7 @@ class JournalEntrySubForm {
this.#amount = document.getElementById(this.#prefix + "-amount");
this.#amountText = document.getElementById(this.#prefix + "-amount-text");
this.deleteButton = document.getElementById(this.#prefix + "-delete");
this.#control.onclick = () => this.side.currency.form.entryEditor.onEdit(this, this.#originalEntryId.value, this.#originalEntryId.dataset.date, this.#originalEntryId.dataset.text, this.#summary.value, this.#accountCode.value, this.#accountCode.dataset.text, this.#amount.value, this.#amount.dataset.min);
this.#control.onclick = () => this.side.currency.form.entryEditor.onEdit(this);
this.deleteButton.onclick = () => {
this.element.parentElement.removeChild(this.element);
this.side.deleteJournalEntry(this);
@ -915,6 +915,24 @@ class JournalEntrySubForm {
return this.#originalEntryId.dataset.date === ""? null: this.#originalEntryId.dataset.date;
}
/**
* Returns the text of the original entry.
*
* @return {string|null} the text of the original entry
*/
getOriginalEntryText() {
return this.#originalEntryId.dataset.text === ""? null: this.#originalEntryId.dataset.text;
}
/**
* Returns the summary.
*
* @return {string|null} the summary
*/
getSummary() {
return this.#summary.value === ""? null: this.#summary.value;
}
/**
* Returns the account code.
*
@ -924,6 +942,15 @@ class JournalEntrySubForm {
return this.#accountCode.value === ""? null: this.#accountCode.value;
}
/**
* Returns the account text.
*
* @return {string|null} the account text
*/
getAccountText() {
return this.#accountCode.dataset.text === ""? null: this.#accountCode.dataset.text;
}
/**
* Returns the amount.
*
@ -933,6 +960,15 @@ class JournalEntrySubForm {
return this.#amount.value === ""? null: new Decimal(this.#amount.value);
}
/**
* Returns the minimal amount.
*
* @return {Decimal|null} the minimal amount
*/
getAmountMin() {
return this.#amount.dataset.min === ""? null: new Decimal(this.#amount.dataset.min);
}
/**
* Validates the form.
*