From 889e4c058e72497ea1c7de1aa371a16c86571dcb 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 00:45:19 +0800 Subject: [PATCH] Revised the option form to have a better look when there is no recurring expense and income. --- src/accounting/static/js/option-form.js | 45 +++++++++++++++++-- .../form-recurring-expense-income.html | 30 +++++++------ 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/accounting/static/js/option-form.js b/src/accounting/static/js/option-form.js index 23c98c1..702c1b2 100644 --- a/src/accounting/static/js/option-form.js +++ b/src/accounting/static/js/option-form.js @@ -202,6 +202,18 @@ class RecurringExpenseIncomeSubForm { */ #prefix; + /** + * The element + * @type {HTMLDivElement} + */ + #element; + + /** + * The content + * @type {HTMLDivElement} + */ + #content; + /** * The recurring items list * @type {HTMLUListElement} @@ -231,10 +243,13 @@ class RecurringExpenseIncomeSubForm { this.expenseIncome = expenseIncome; this.editor = new RecurringItemEditor(this); this.#prefix = "accounting-recurring-" + expenseIncome; + this.#element = document.getElementById(this.#prefix); + this.#content = document.getElementById(this.#prefix + "-content"); this.#itemList = document.getElementById(this.#prefix + "-list"); this.#items = Array.from(document.getElementsByClassName(this.#prefix + "-item")).map((element) => new RecurringItemSubForm(this, element)); this.#addButton = document.getElementById(this.#prefix + "-add"); + this.#resetContent(); this.#addButton.onclick = () => this.editor.onAddNew(); this.#initializeDragAndDropReordering(); } @@ -253,6 +268,7 @@ class RecurringExpenseIncomeSubForm { const element = document.getElementById(this.#prefix + "-" + String(newIndex)) const item = new RecurringItemSubForm(this, element); this.#items.push(item); + this.#resetContent(); this.#initializeDragAndDropReordering(); this.validate(); return item; @@ -266,6 +282,29 @@ class RecurringExpenseIncomeSubForm { deleteItem(item) { const index = this.#items.indexOf(item); this.#items.splice(index, 1); + this.#resetContent(); + } + + /** + * Resets the layout of the content. + * + */ + #resetContent() { + if (this.#items.length === 0) { + this.#element.classList.remove("accounting-not-empty"); + this.#element.classList.add("accounting-clickable"); + this.#element.dataset.bsToggle = "modal" + this.#element.dataset.bsTarget = "#" + this.editor.modal.id; + this.#element.onclick = () => this.editor.onAddNew(); + this.#content.classList.add("d-none"); + } else { + this.#element.classList.add("accounting-not-empty"); + this.#element.classList.remove("accounting-clickable"); + delete this.#element.dataset.bsToggle; + delete this.#element.dataset.bsTarget; + this.#element.onclick = null; + this.#content.classList.remove("d-none"); + } } /** @@ -530,7 +569,7 @@ class RecurringItemEditor { * The modal * @type {HTMLDivElement} */ - #modal; + modal; /** * The name @@ -608,7 +647,7 @@ class RecurringItemEditor { this.expenseIncome = subForm.expenseIncome; const prefix = "accounting-recurring-item-editor-" + subForm.expenseIncome; this.#form = document.getElementById(prefix); - this.#modal = document.getElementById(prefix + "-modal"); + this.modal = document.getElementById(prefix + "-modal"); this.#name = document.getElementById(prefix + "-name"); this.#nameError = document.getElementById(prefix + "-name-error"); this.#accountControl = document.getElementById(prefix + "-account-control"); @@ -627,7 +666,7 @@ class RecurringItemEditor { this.#item = this.#subForm.addItem(); } this.#item.save(this); - bootstrap.Modal.getInstance(this.#modal).hide(); + bootstrap.Modal.getInstance(this.modal).hide(); } return false; }; diff --git a/src/accounting/templates/accounting/option/include/form-recurring-expense-income.html b/src/accounting/templates/accounting/option/include/form-recurring-expense-income.html index 39e2e07..8c1cbce 100644 --- a/src/accounting/templates/accounting/option/include/form-recurring-expense-income.html +++ b/src/accounting/templates/accounting/option/include/form-recurring-expense-income.html @@ -19,21 +19,23 @@ form-recurring-expense-income.html: The recurring expense or income sub-form in Author: imacat@mail.imacat.idv.tw (imacat) First written: 2023/3/22 #} -
+
-
    - {% for recurring_item in recurring_items %} - {% with form = recurring_item.form, - item_index = loop.index %} - {% include "accounting/option/include/form-recurring-item.html" %} - {% endwith %} - {% endfor %} -
+
+
    + {% for recurring_item in recurring_items %} + {% with form = recurring_item.form, + item_index = loop.index %} + {% include "accounting/option/include/form-recurring-item.html" %} + {% endwith %} + {% endfor %} +
-
- +
+ +