Replaces the datasets with object attributes to store the column values in the JavaScript JournalEntryEditor class.

This commit is contained in:
依瑪貓 2023-03-18 10:41:17 +08:00 committed by 依瑪貓
parent 0a1bbbdd47
commit 94a527caf2
5 changed files with 97 additions and 72 deletions

View File

@ -151,8 +151,8 @@ class AccountSelector {
*/ */
#getCodesUsedInForm() { #getCodesUsedInForm() {
const inUse = this.#entryEditor.getTransactionForm().getAccountCodesUsed(this.#entryType); const inUse = this.#entryEditor.getTransactionForm().getAccountCodesUsed(this.#entryType);
if (this.#entryEditor.getAccountCode() !== null) { if (this.#entryEditor.accountCode !== null) {
inUse.push(this.#entryEditor.getAccountCode()); inUse.push(this.#entryEditor.accountCode);
} }
return inUse return inUse
} }
@ -199,13 +199,13 @@ class AccountSelector {
this.#more.classList.remove("d-none"); this.#more.classList.remove("d-none");
this.#filterOptions(); this.#filterOptions();
for (const option of this.#options) { for (const option of this.#options) {
if (option.dataset.code === entryEditor.getAccountCode()) { if (option.dataset.code === entryEditor.accountCode) {
option.classList.add("active"); option.classList.add("active");
} else { } else {
option.classList.remove("active"); option.classList.remove("active");
} }
} }
if (entryEditor.getAccountCode() === null) { if (entryEditor.accountCode === null) {
this.#clearButton.classList.add("btn-secondary"); this.#clearButton.classList.add("btn-secondary");
this.#clearButton.classList.remove("btn-danger"); this.#clearButton.classList.remove("btn-danger");
this.#clearButton.disabled = true; this.#clearButton.disabled = true;

View File

@ -153,6 +153,42 @@ class JournalEntryEditor {
*/ */
isNeedOffset = false; isNeedOffset = false;
/**
* The ID of the original entry
* @type {string|null}
*/
originalEntryId = null;
/**
* The date of the original entry
* @type {string|null}
*/
originalEntryDate = null;
/**
* The text of the original entry
* @type {string|null}
*/
originalEntryText = null;
/**
* The account code
* @type {string|null}
*/
accountCode = null;
/**
* The account text
* @type {string|null}
*/
accountText = null;
/**
* The summary
* @type {string|null}
*/
summary = null;
/** /**
* Constructs a new journal entry editor. * Constructs a new journal entry editor.
* *
@ -173,11 +209,9 @@ class JournalEntryEditor {
this.#accountError = document.getElementById(this.#prefix + "-account-error") this.#accountError = document.getElementById(this.#prefix + "-account-error")
this.#amount = document.getElementById(this.#prefix + "-amount"); this.#amount = document.getElementById(this.#prefix + "-amount");
this.#amountError = document.getElementById(this.#prefix + "-amount-error"); this.#amountError = document.getElementById(this.#prefix + "-amount-error");
this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.#originalEntry.dataset.id); this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId);
this.#originalEntryDelete.onclick = () => this.clearOriginalEntry(); this.#originalEntryDelete.onclick = () => this.clearOriginalEntry();
this.#summaryControl.onclick = () => { this.#summaryControl.onclick = () => SummaryEditor.start(this, this.summary);
SummaryEditor.start(this, this.#summary.dataset.value);
};
this.#accountControl.onclick = () => { this.#accountControl.onclick = () => {
AccountSelector.start(this, this.entryType); AccountSelector.start(this, this.entryType);
} }
@ -189,7 +223,7 @@ class JournalEntryEditor {
if (this.entry === null) { if (this.entry === null) {
this.entry = this.#side.addJournalEntry(); this.entry = this.#side.addJournalEntry();
} }
this.entry.save(this.isNeedOffset, this.#originalEntry.dataset.id, this.#originalEntry.dataset.date, this.#originalEntry.dataset.text, this.#account.dataset.code, this.#account.dataset.text, this.#summary.dataset.value, this.#amount.value); this.entry.save(this.isNeedOffset, this.originalEntryId, this.originalEntryDate, this.originalEntryText, this.accountCode, this.accountText, this.summary, this.#amount.value);
bootstrap.Modal.getInstance(this.#modal).hide(); bootstrap.Modal.getInstance(this.#modal).hide();
} }
return false; return false;
@ -205,9 +239,9 @@ class JournalEntryEditor {
this.isNeedOffset = false; this.isNeedOffset = false;
this.#originalEntryContainer.classList.remove("d-none"); this.#originalEntryContainer.classList.remove("d-none");
this.#originalEntryControl.classList.add("accounting-not-empty"); this.#originalEntryControl.classList.add("accounting-not-empty");
this.#originalEntry.dataset.id = originalEntry.id; this.originalEntryId = originalEntry.id;
this.#originalEntry.dataset.date = originalEntry.date; this.originalEntryDate = originalEntry.date;
this.#originalEntry.dataset.text = originalEntry.text; this.originalEntryText = originalEntry.text;
this.#originalEntry.innerText = originalEntry.text; this.#originalEntry.innerText = originalEntry.text;
this.#setEnableSummaryAccount(false); this.#setEnableSummaryAccount(false);
if (originalEntry.summary === "") { if (originalEntry.summary === "") {
@ -215,11 +249,11 @@ class JournalEntryEditor {
} else { } else {
this.#summaryControl.classList.add("accounting-not-empty"); this.#summaryControl.classList.add("accounting-not-empty");
} }
this.#summary.dataset.value = originalEntry.summary; this.summary = originalEntry.summary === ""? null: originalEntry.summary;
this.#summary.innerText = originalEntry.summary; this.#summary.innerText = originalEntry.summary;
this.#accountControl.classList.add("accounting-not-empty"); this.#accountControl.classList.add("accounting-not-empty");
this.#account.dataset.code = originalEntry.accountCode; this.accountCode = originalEntry.accountCode;
this.#account.dataset.text = originalEntry.accountText; this.accountText = originalEntry.accountText
this.#account.innerText = originalEntry.accountText; this.#account.innerText = originalEntry.accountText;
this.#amount.value = String(originalEntry.netBalance); this.#amount.value = String(originalEntry.netBalance);
this.#amount.max = String(originalEntry.netBalance); this.#amount.max = String(originalEntry.netBalance);
@ -235,14 +269,14 @@ class JournalEntryEditor {
this.isNeedOffset = false; this.isNeedOffset = false;
this.#originalEntryContainer.classList.add("d-none"); this.#originalEntryContainer.classList.add("d-none");
this.#originalEntryControl.classList.remove("accounting-not-empty"); this.#originalEntryControl.classList.remove("accounting-not-empty");
this.#originalEntry.dataset.id = ""; this.originalEntryId = null;
this.#originalEntry.dataset.date = ""; this.originalEntryDate = null;
this.#originalEntry.dataset.text = ""; this.originalEntryText = null;
this.#originalEntry.innerText = ""; this.#originalEntry.innerText = "";
this.#setEnableSummaryAccount(true); this.#setEnableSummaryAccount(true);
this.#accountControl.classList.remove("accounting-not-empty"); this.#accountControl.classList.remove("accounting-not-empty");
this.#account.dataset.code = ""; this.accountCode = null;
this.#account.dataset.text = ""; this.accountText = null;
this.#account.innerText = ""; this.#account.innerText = "";
this.#amount.max = ""; this.#amount.max = "";
} }
@ -276,7 +310,7 @@ class JournalEntryEditor {
} else { } else {
this.#summaryControl.classList.add("accounting-not-empty"); this.#summaryControl.classList.add("accounting-not-empty");
} }
this.#summary.dataset.value = summary; this.summary = summary === ""? null: summary;
this.#summary.innerText = summary; this.#summary.innerText = summary;
this.#validateSummary(); this.#validateSummary();
bootstrap.Modal.getOrCreateInstance(this.#modal).show(); bootstrap.Modal.getOrCreateInstance(this.#modal).show();
@ -293,22 +327,13 @@ class JournalEntryEditor {
saveSummaryWithAccount(summary, accountCode, accountText, isAccountOffsetNeeded) { saveSummaryWithAccount(summary, accountCode, accountText, isAccountOffsetNeeded) {
this.isNeedOffset = isAccountOffsetNeeded; this.isNeedOffset = isAccountOffsetNeeded;
this.#accountControl.classList.add("accounting-not-empty"); this.#accountControl.classList.add("accounting-not-empty");
this.#account.dataset.code = accountCode; this.accountCode = accountCode;
this.#account.dataset.text = accountText; this.accountText = accountText;
this.#account.innerText = accountText; this.#account.innerText = accountText;
this.#validateAccount(); this.#validateAccount();
this.saveSummary(summary) this.saveSummary(summary)
} }
/**
* Returns the account code.
*
* @return {string|null} the account code
*/
getAccountCode() {
return this.#account.dataset.code === "" ? null : this.#account.dataset.code;
}
/** /**
* Clears the account. * Clears the account.
* *
@ -316,8 +341,8 @@ class JournalEntryEditor {
clearAccount() { clearAccount() {
this.isNeedOffset = false; this.isNeedOffset = false;
this.#accountControl.classList.remove("accounting-not-empty"); this.#accountControl.classList.remove("accounting-not-empty");
this.#account.dataset.code = ""; this.accountCode = null;
this.#account.dataset.text = ""; this.accountText = null;
this.#account.innerText = ""; this.#account.innerText = "";
this.#validateAccount(); this.#validateAccount();
} }
@ -332,8 +357,8 @@ class JournalEntryEditor {
saveAccount(code, text, isOffsetNeeded) { saveAccount(code, text, isOffsetNeeded) {
this.isNeedOffset = isOffsetNeeded; this.isNeedOffset = isOffsetNeeded;
this.#accountControl.classList.add("accounting-not-empty"); this.#accountControl.classList.add("accounting-not-empty");
this.#account.dataset.code = code; this.accountCode = code;
this.#account.dataset.text = text; this.accountText = text;
this.#account.innerText = text; this.#account.innerText = text;
this.#validateAccount(); this.#validateAccount();
} }
@ -382,7 +407,7 @@ class JournalEntryEditor {
* @return {boolean} true if valid, or false otherwise * @return {boolean} true if valid, or false otherwise
*/ */
#validateAccount() { #validateAccount() {
if (this.#account.dataset.code === "") { if (this.accountCode === null) {
this.#accountControl.classList.add("is-invalid"); this.#accountControl.classList.add("is-invalid");
this.#accountError.innerText = A_("Please select the account."); this.#accountError.innerText = A_("Please select the account.");
return false; return false;
@ -446,20 +471,20 @@ class JournalEntryEditor {
this.#originalEntryContainer.classList.add("d-none"); this.#originalEntryContainer.classList.add("d-none");
this.#originalEntryControl.classList.remove("accounting-not-empty"); this.#originalEntryControl.classList.remove("accounting-not-empty");
this.#originalEntryControl.classList.remove("is-invalid"); this.#originalEntryControl.classList.remove("is-invalid");
this.#originalEntry.dataset.id = ""; this.originalEntryId = null;
this.#originalEntry.dataset.date = ""; this.originalEntryDate = null;
this.#originalEntry.dataset.text = ""; this.originalEntryText = null;
this.#originalEntry.innerText = ""; this.#originalEntry.innerText = "";
this.#setEnableSummaryAccount(true); this.#setEnableSummaryAccount(true);
this.#summaryControl.classList.remove("accounting-not-empty"); this.#summaryControl.classList.remove("accounting-not-empty");
this.#summaryControl.classList.remove("is-invalid"); this.#summaryControl.classList.remove("is-invalid");
this.#summary.dataset.value = ""; this.summary = null;
this.#summary.innerText = "" this.#summary.innerText = ""
this.#summaryError.innerText = "" this.#summaryError.innerText = ""
this.#accountControl.classList.remove("accounting-not-empty"); this.#accountControl.classList.remove("accounting-not-empty");
this.#accountControl.classList.remove("is-invalid"); this.#accountControl.classList.remove("is-invalid");
this.#account.dataset.code = ""; this.accountCode = null;
this.#account.dataset.text = ""; this.accountText = null;
this.#account.innerText = ""; this.#account.innerText = "";
this.#accountError.innerText = ""; this.#accountError.innerText = "";
this.#amount.value = ""; this.#amount.value = "";
@ -495,9 +520,9 @@ class JournalEntryEditor {
this.#originalEntryContainer.classList.remove("d-none"); this.#originalEntryContainer.classList.remove("d-none");
this.#originalEntryControl.classList.add("accounting-not-empty"); this.#originalEntryControl.classList.add("accounting-not-empty");
} }
this.#originalEntry.dataset.id = originalEntryId; this.originalEntryId = originalEntryId === ""? null: originalEntryId;
this.#originalEntry.dataset.date = originalEntryDate; this.originalEntryDate = originalEntryDate === ""? null: originalEntryDate;
this.#originalEntry.dataset.text = originalEntryText; this.originalEntryText = originalEntryText === ""? null: originalEntryText;
this.#originalEntry.innerText = originalEntryText; this.#originalEntry.innerText = originalEntryText;
this.#setEnableSummaryAccount(!entry.isMatched && originalEntryId === ""); this.#setEnableSummaryAccount(!entry.isMatched && originalEntryId === "");
if (summary === "") { if (summary === "") {
@ -505,15 +530,15 @@ class JournalEntryEditor {
} else { } else {
this.#summaryControl.classList.add("accounting-not-empty"); this.#summaryControl.classList.add("accounting-not-empty");
} }
this.#summary.dataset.value = summary; this.summary = summary === ""? null: summary;
this.#summary.innerText = summary; this.#summary.innerText = summary;
if (accountCode === "") { if (accountCode === "") {
this.#accountControl.classList.remove("accounting-not-empty"); this.#accountControl.classList.remove("accounting-not-empty");
} else { } else {
this.#accountControl.classList.add("accounting-not-empty"); this.#accountControl.classList.add("accounting-not-empty");
} }
this.#account.dataset.code = accountCode; this.accountCode = accountCode;
this.#account.dataset.text = accountText; this.accountText = accountText;
this.#account.innerText = accountText; this.#account.innerText = accountText;
this.#amount.value = amount; this.#amount.value = amount;
const maxAmount = this.#getMaxAmount(); const maxAmount = this.#getMaxAmount();
@ -528,10 +553,10 @@ class JournalEntryEditor {
* @return {Decimal|null} the max amount * @return {Decimal|null} the max amount
*/ */
#getMaxAmount() { #getMaxAmount() {
if (this.#originalEntry.dataset.id === "") { if (this.originalEntryId === null) {
return null; return null;
} }
return OriginalEntrySelector.getNetBalance(this.entry, this.getTransactionForm(), this.#originalEntry.dataset.id); return OriginalEntrySelector.getNetBalance(this.entry, this.getTransactionForm(), this.originalEntryId);
} }
/** /**

View File

@ -228,12 +228,12 @@ class SummaryEditor {
* The callback when the summary editor is shown. * The callback when the summary editor is shown.
* *
* @param entryEditor {JournalEntryEditor} the journal entry editor * @param entryEditor {JournalEntryEditor} the journal entry editor
* @param summary {string} the summary * @param summary {string|null} the summary
*/ */
#onOpen(entryEditor, summary) { #onOpen(entryEditor, summary) {
this.#entryEditor = entryEditor; this.#entryEditor = entryEditor;
this.#reset(); this.#reset();
this.summary.value = summary; this.summary.value = summary === null? "": summary;
this.#onSummaryChange(); this.#onSummaryChange();
} }
@ -271,7 +271,7 @@ class SummaryEditor {
* The callback when the summary editor is shown. * The callback when the summary editor is shown.
* *
* @param entryEditor {JournalEntryEditor} the journal entry editor * @param entryEditor {JournalEntryEditor} the journal entry editor
* @param summary {string} the summary * @param summary {string|null} the summary
*/ */
static start(entryEditor, summary) { static start(entryEditor, summary) {
this.#editors[entryEditor.entryType].#onOpen(entryEditor, summary); this.#editors[entryEditor.entryType].#onOpen(entryEditor, summary);

View File

@ -961,12 +961,12 @@ class JournalEntrySubForm {
* Stores the data into the journal entry sub-form. * Stores the data into the journal entry sub-form.
* *
* @param isOriginalEntry {boolean} true if this is an original entry, or false otherwise * @param isOriginalEntry {boolean} true if this is an original entry, or false otherwise
* @param originalEntryId {string} the ID of the original entry * @param originalEntryId {string|null} the ID of the original entry
* @param originalEntryDate {string} the date of the original entry * @param originalEntryDate {string|null} the date of the original entry
* @param originalEntryText {string} the text of the original entry * @param originalEntryText {string|null} the text of the original entry
* @param accountCode {string} the account code * @param accountCode {string|null} the account code
* @param accountText {string} the account text * @param accountText {string|null} the account text
* @param summary {string} the summary * @param summary {string|null} the summary
* @param amount {string} the amount * @param amount {string} the amount
*/ */
save(isOriginalEntry, originalEntryId, originalEntryDate, originalEntryText, accountCode, accountText, summary, amount) { save(isOriginalEntry, originalEntryId, originalEntryDate, originalEntryText, accountCode, accountText, summary, amount) {
@ -975,21 +975,21 @@ class JournalEntrySubForm {
} else { } else {
this.#offsets.classList.add("d-none"); this.#offsets.classList.add("d-none");
} }
this.#originalEntryId.value = originalEntryId; this.#originalEntryId.value = originalEntryId === null? "": originalEntryId;
this.#originalEntryId.dataset.date = originalEntryDate; this.#originalEntryId.dataset.date = originalEntryDate === null? "": originalEntryDate;
this.#originalEntryId.dataset.text = originalEntryText; this.#originalEntryId.dataset.text = originalEntryText === null? "": originalEntryText;
if (originalEntryText === "") { if (originalEntryText === null) {
this.#originalEntryText.classList.add("d-none"); this.#originalEntryText.classList.add("d-none");
this.#originalEntryText.innerText = ""; this.#originalEntryText.innerText = "";
} else { } else {
this.#originalEntryText.classList.remove("d-none"); this.#originalEntryText.classList.remove("d-none");
this.#originalEntryText.innerText = A_("Offset %(entry)s", {entry: originalEntryText}); this.#originalEntryText.innerText = A_("Offset %(entry)s", {entry: originalEntryText});
} }
this.#accountCode.value = accountCode; this.#accountCode.value = accountCode === null? "": accountCode;
this.#accountCode.dataset.text = accountText; this.#accountCode.dataset.text = accountText === null? "": accountText;
this.#accountText.innerText = accountText; this.#accountText.innerText = accountText === null? "": accountText;
this.#summary.value = summary; this.#summary.value = summary === null? "": summary;
this.#summaryText.innerText = summary; this.#summaryText.innerText = summary === null? "": summary;
this.#amount.value = amount; this.#amount.value = amount;
this.#amountText.innerText = formatDecimal(new Decimal(amount)); this.#amountText.innerText = formatDecimal(new Decimal(amount));
this.validate(); this.validate();

View File

@ -32,7 +32,7 @@ First written: 2023/2/25
<div class="accounting-entry-editor-original-entry-content"> <div class="accounting-entry-editor-original-entry-content">
<div id="accounting-entry-editor-original-entry-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target="#accounting-original-entry-selector-modal"> <div id="accounting-entry-editor-original-entry-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target="#accounting-original-entry-selector-modal">
<label class="form-label" for="accounting-entry-editor-original-entry">{{ A_("Original Entry") }}</label> <label class="form-label" for="accounting-entry-editor-original-entry">{{ A_("Original Entry") }}</label>
<div id="accounting-entry-editor-original-entry" data-id="" data-date="" data-text=""></div> <div id="accounting-entry-editor-original-entry"></div>
</div> </div>
<div id="accounting-entry-editor-original-entry-error" class="invalid-feedback"></div> <div id="accounting-entry-editor-original-entry-error" class="invalid-feedback"></div>
</div> </div>
@ -47,7 +47,7 @@ First written: 2023/2/25
<div class="mb-3"> <div class="mb-3">
<div id="accounting-entry-editor-summary-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target=""> <div id="accounting-entry-editor-summary-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target="">
<label class="form-label" for="accounting-entry-editor-summary">{{ A_("Summary") }}</label> <label class="form-label" for="accounting-entry-editor-summary">{{ A_("Summary") }}</label>
<div id="accounting-entry-editor-summary" data-value=""></div> <div id="accounting-entry-editor-summary"></div>
</div> </div>
<div id="accounting-entry-editor-summary-error" class="invalid-feedback"></div> <div id="accounting-entry-editor-summary-error" class="invalid-feedback"></div>
</div> </div>
@ -55,7 +55,7 @@ First written: 2023/2/25
<div class="mb-3"> <div class="mb-3">
<div id="accounting-entry-editor-account-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target=""> <div id="accounting-entry-editor-account-control" class="form-control accounting-clickable accounting-material-text-field" data-bs-toggle="modal" data-bs-target="">
<label class="form-label" for="accounting-entry-editor-account">{{ A_("Account") }}</label> <label class="form-label" for="accounting-entry-editor-account">{{ A_("Account") }}</label>
<div id="accounting-entry-editor-account" data-code="" data-text=""></div> <div id="accounting-entry-editor-account"></div>
</div> </div>
<div id="accounting-entry-editor-account-error" class="invalid-feedback"></div> <div id="accounting-entry-editor-account-error" class="invalid-feedback"></div>
</div> </div>