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

This commit is contained in:
依瑪貓 2023-03-18 18:25:17 +08:00
parent 46e34bb89a
commit 12fbe36b9a
2 changed files with 36 additions and 46 deletions

View File

@ -196,6 +196,12 @@ class JournalEntryEditor {
*/ */
amount = ""; amount = "";
/**
* The summary editors
* @type {{debit: SummaryEditor, credit: SummaryEditor}}
*/
#summaryEditors;
/** /**
* Constructs a new journal entry editor. * Constructs a new journal entry editor.
* *
@ -218,9 +224,10 @@ 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.#summaryEditors = this.#initializeSummaryEditors();
this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId); this.#originalEntryControl.onclick = () => OriginalEntrySelector.start(this, this.originalEntryId);
this.#originalEntryDelete.onclick = () => this.clearOriginalEntry(); this.#originalEntryDelete.onclick = () => this.clearOriginalEntry();
this.#summaryControl.onclick = () => SummaryEditor.start(this); this.#summaryControl.onclick = () => this.#summaryEditors[this.entryType].onOpen();
this.#accountControl.onclick = () => AccountSelector.start(this); this.#accountControl.onclick = () => AccountSelector.start(this);
this.#amount.onchange = () => this.#validateAmount(); this.#amount.onchange = () => this.#validateAmount();
this.#element.onsubmit = () => { this.#element.onsubmit = () => {
@ -236,6 +243,21 @@ class JournalEntryEditor {
}; };
} }
/**
* Initializes the summary editors.
*
* @return {{debit: SummaryEditor, credit: SummaryEditor}} the summary editors
*/
#initializeSummaryEditors() {
const editors = {};
const forms = Array.from(document.getElementsByClassName("accounting-summary-editor"));
for (const form of forms) {
const summaryEditor = new SummaryEditor(this, form);
editors[summaryEditor.entryType] = summaryEditor;
}
return editors;
}
/** /**
* Saves the original entry from the original entry selector. * Saves the original entry from the original entry selector.
* *

View File

@ -22,17 +22,18 @@
*/ */
"use strict"; "use strict";
// Initializes the page JavaScript.
document.addEventListener("DOMContentLoaded", () => {
SummaryEditor.initialize();
});
/** /**
* A summary editor. * A summary editor.
* *
*/ */
class SummaryEditor { class SummaryEditor {
/**
* The journal entry editor
* @type {JournalEntryEditor}
*/
#entryEditor;
/** /**
* The summary editor form * The summary editor form
* @type {HTMLFormElement} * @type {HTMLFormElement}
@ -55,7 +56,7 @@ class SummaryEditor {
* The entry type, either "debit" or "credit" * The entry type, either "debit" or "credit"
* @type {string} * @type {string}
*/ */
#entryType; entryType;
/** /**
* The current tab * The current tab
@ -99,12 +100,6 @@ class SummaryEditor {
*/ */
#selectedAccount = null; #selectedAccount = null;
/**
* The journal entry editor
* @type {JournalEntryEditor}
*/
#entryEditor;
/** /**
* The tab planes * The tab planes
* @type {{general: GeneralTagTab, travel: GeneralTripTab, bus: BusTripTab, regular: RegularPaymentTab, annotation: AnnotationTab}} * @type {{general: GeneralTagTab, travel: GeneralTripTab, bus: BusTripTab, regular: RegularPaymentTab, annotation: AnnotationTab}}
@ -114,11 +109,13 @@ class SummaryEditor {
/** /**
* Constructs a summary editor. * Constructs a summary editor.
* *
* @param entryEditor {JournalEntryEditor} the journal entry editor
* @param form {HTMLFormElement} the summary editor form * @param form {HTMLFormElement} the summary editor form
*/ */
constructor(form) { constructor(entryEditor, form) {
this.#entryEditor = entryEditor;
this.#form = form; this.#form = form;
this.#entryType = form.dataset.entryType; this.entryType = form.dataset.entryType;
this.prefix = "accounting-summary-editor-" + form.dataset.entryType; this.prefix = "accounting-summary-editor-" + form.dataset.entryType;
this.#modal = document.getElementById(this.prefix + "-modal"); this.#modal = document.getElementById(this.prefix + "-modal");
this.summary = document.getElementById(this.prefix + "-summary"); this.summary = document.getElementById(this.prefix + "-summary");
@ -227,12 +224,10 @@ 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
*/ */
#onOpen(entryEditor) { onOpen() {
this.#entryEditor = entryEditor;
this.#reset(); this.#reset();
this.summary.value = entryEditor.summary === null? "": entryEditor.summary; this.summary.value = this.#entryEditor.summary === null? "": this.#entryEditor.summary;
this.#onSummaryChange(); this.#onSummaryChange();
} }
@ -247,33 +242,6 @@ class SummaryEditor {
} }
this.tabPlanes.general.switchToMe(); this.tabPlanes.general.switchToMe();
} }
/**
* The summary editors.
* @type {{debit: SummaryEditor, credit: SummaryEditor}}
*/
static #editors = {}
/**
* Initializes the summary editors.
*
*/
static initialize() {
const forms = Array.from(document.getElementsByClassName("accounting-summary-editor"));
for (const form of forms) {
const editor = new SummaryEditor(form);
this.#editors[editor.#entryType] = editor;
}
}
/**
* The callback when the summary editor is shown.
*
* @param entryEditor {JournalEntryEditor} the journal entry editor
*/
static start(entryEditor) {
this.#editors[entryEditor.entryType].#onOpen(entryEditor);
}
} }
/** /**