From cb397910f813ca2ff68c0b325bcb6db251e5f6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 23 Mar 2023 08:55:16 +0800 Subject: [PATCH] Added the "resetNo" method to the RecurringItemSubForm, CurrencySubForm, and LineItemSubForm forms to provide a simpler way to reset the order number, and removed the "elementId" getter and "no" setter. --- .../static/js/journal-entry-form.js | 46 +++++-------------- src/accounting/static/js/option-form.js | 23 +++------- 2 files changed, 18 insertions(+), 51 deletions(-) diff --git a/src/accounting/static/js/journal-entry-form.js b/src/accounting/static/js/journal-entry-form.js index e167070..eb648a1 100644 --- a/src/accounting/static/js/journal-entry-form.js +++ b/src/accounting/static/js/journal-entry-form.js @@ -180,10 +180,8 @@ class JournalEntryForm { */ #initializeDragAndDropReordering() { initializeDragAndDropReordering(this.#currencyList, () => { - const currencyId = Array.from(this.#currencyList.children).map((currency) => currency.id); - this.#currencies.sort((a, b) => currencyId.indexOf(a.elementId) - currencyId.indexOf(b.elementId)); - for (let i = 0; i < this.#currencies.length; i++) { - this.#currencies[i].no = i + 1; + for (const currency of this.#currencies) { + currency.resetNo(); } }); } @@ -436,21 +434,12 @@ class CurrencySubForm { } /** - * Returns the element ID. + * Reset the order number. * - * @return {string|null} element ID */ - get elementId() { - return this.#element.id; - } - - /** - * Sets the order number. - * - * @param value {number} the order number - */ - set no(value) { - this.#no.value = String(value); + resetNo() { + const siblings = Array.from(this.#element.parentElement.children); + this.#no.value = String(siblings.indexOf(this.#element) + 1); } /** @@ -713,10 +702,8 @@ class DebitCreditSubForm { */ #initializeDragAndDropReordering() { initializeDragAndDropReordering(this.#lineItemList, () => { - const lineItemId = Array.from(this.#lineItemList.children).map((lineItem) => lineItem.id); - this.lineItems.sort((a, b) => lineItemId.indexOf(a.elementId) - lineItemId.indexOf(b.elementId)); - for (let i = 0; i < this.lineItems.length; i++) { - this.lineItems[i].no = i + 1; + for (const lineItem of this.lineItems) { + lineItem.resetNo(); } }); } @@ -900,21 +887,12 @@ class LineItemSubForm { } /** - * Returns the element ID. + * Reset the order number. * - * @return {string|null} element ID */ - get elementId() { - return this.#element.id; - } - - /** - * Sets the order number. - * - * @param value {number} the order number - */ - set no(value) { - this.#no.value = String(value); + resetNo() { + const siblings = Array.from(this.#element.parentElement.children); + this.#no.value = String(siblings.indexOf(this.#element) + 1); } /** diff --git a/src/accounting/static/js/option-form.js b/src/accounting/static/js/option-form.js index 27d105f..fe339a5 100644 --- a/src/accounting/static/js/option-form.js +++ b/src/accounting/static/js/option-form.js @@ -313,10 +313,8 @@ class RecurringExpenseIncomeSubForm { */ #initializeDragAndDropReordering() { initializeDragAndDropReordering(this.#itemList, () => { - const itemId = Array.from(this.#itemList.children).map((item) => item.id); - this.#items.sort((a, b) => itemId.indexOf(a.elementId) - itemId.indexOf(b.elementId)); - for (let i = 0; i < this.#items.length; i++) { - this.#items[i].no = i + 1; + for (const item of this.#items) { + item.resetNo(); } }); } @@ -463,21 +461,12 @@ class RecurringItemSubForm { } /** - * Returns the element ID. + * Reset the order number. * - * @return {string|null} element ID */ - get elementId() { - return this.#element.id; - } - - /** - * Sets the order number. - * - * @param value {number} the order number - */ - set no(value) { - this.#no.value = String(value); + resetNo() { + const siblings = Array.from(this.#element.parentElement.children); + this.#no.value = String(siblings.indexOf(this.#element) + 1); } /**