Revised the JavaScript to initialize the OriginalEntrySelector instance in JournalEntryEditor, so that the journal entry editor holds the OriginalEntrySelector instance. It can find the OriginalEntrySelector instance without needing to invoke its static methods. Removed the redundant static methods from the OriginalEntrySelector class.

This commit is contained in:
依瑪貓 2023-03-18 20:45:31 +08:00
parent fd9eac06f6
commit 915e4408e1
3 changed files with 12 additions and 46 deletions

View File

@ -208,6 +208,12 @@ class JournalEntryEditor {
*/
#accountSelectors = {};
/**
* The original entry selector
* @type {OriginalEntrySelector}
*/
originalEntrySelector;
/**
* Constructs a new journal entry editor.
*
@ -236,7 +242,8 @@ class JournalEntryEditor {
for (const entryType of ["debit", "credit"]) {
this.#accountSelectors[entryType] = new AccountSelector(this, entryType);
}
this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId);
this.originalEntrySelector = new OriginalEntrySelector();
this.#originalEntryControl.onclick = () => this.originalEntrySelector.onOpen(this, this.originalEntryId)
this.#originalEntryDelete.onclick = () => this.clearOriginalEntry();
this.#summaryControl.onclick = () => this.#summaryEditors[this.entryType].onOpen();
this.#accountControl.onclick = () => this.#accountSelectors[this.entryType].onOpen();
@ -569,7 +576,7 @@ class JournalEntryEditor {
if (this.originalEntryId === null) {
return null;
}
return OriginalEntrySelector.getNetBalance(this.entry, this.form, this.originalEntryId);
return this.originalEntrySelector.getNetBalance(this.entry, this.form, this.originalEntryId);
}
/**

View File

@ -22,11 +22,6 @@
*/
"use strict";
// Initializes the page JavaScript.
document.addEventListener("DOMContentLoaded", () => {
OriginalEntrySelector.initialize();
});
/**
* The original entry selector.
*
@ -108,7 +103,7 @@ class OriginalEntrySelector {
* @param originalEntryId {string} the ID of the original entry
* @return {Decimal} the net balance of the original entry
*/
#getNetBalance(currentEntry, form, originalEntryId) {
getNetBalance(currentEntry, form, originalEntryId) {
const otherEntries = form.getEntries().filter((entry) => entry !== currentEntry);
let otherOffset = new Decimal(0);
for (const otherEntry of otherEntries) {
@ -178,7 +173,7 @@ class OriginalEntrySelector {
* @param entryEditor {JournalEntryEditor} the journal entry editor
* @param originalEntryId {string|null} the ID of the original entry
*/
#onOpen(entryEditor, originalEntryId) {
onOpen(entryEditor, originalEntryId = null) {
this.entryEditor = entryEditor
this.#modal.dataset.currencyCode = entryEditor.getCurrencyCode();
this.#modal.dataset.entryType = entryEditor.entryType;
@ -189,42 +184,6 @@ class OriginalEntrySelector {
this.#updateNetBalances();
this.#filterOptions();
}
/**
* The original entry selector.
* @type {OriginalEntrySelector}
*/
static #selector;
/**
* Initializes the original entry selector.
*
*/
static initialize() {
this.#selector = new OriginalEntrySelector();
}
/**
* Starts the original entry selector.
*
* @param entryEditor {JournalEntryEditor} the journal entry editor
* @param originalEntryId {string|null} the ID of the original entry
*/
static start(entryEditor, originalEntryId = null) {
this.#selector.#onOpen(entryEditor, originalEntryId);
}
/**
* Returns the net balance for an original entry.
*
* @param currentEntry {JournalEntrySubForm} the journal entry sub-form that is currently editing
* @param form {TransactionForm} the transaction form
* @param originalEntryId {string} the ID of the original entry
* @return {Decimal} the net balance of the original entry
*/
static getNetBalance(currentEntry, form, originalEntryId) {
return this.#selector.#getNetBalance(currentEntry, form, originalEntryId);
}
}
/**

View File

@ -132,7 +132,7 @@ class SummaryEditor {
this.currentTab = this.tabPlanes.general;
this.#initializeSuggestedAccounts();
this.summary.onchange = () => this.#onSummaryChange();
this.#offsetButton.onclick = () => OriginalEntrySelector.start(this.#entryEditor);
this.#offsetButton.onclick = () => this.#entryEditor.originalEntrySelector.onOpen(this.#entryEditor);
this.#form.onsubmit = () => {
if (this.currentTab.validate()) {
this.#submit();