diff --git a/src/main/resources/static/js/models/ShoppingCategory.js b/src/main/resources/static/js/models/ShoppingCategory.js index bf2e00d..a8545a2 100644 --- a/src/main/resources/static/js/models/ShoppingCategory.js +++ b/src/main/resources/static/js/models/ShoppingCategory.js @@ -1,31 +1,27 @@ -export class ShoppingCategory { - constructor(categoryName) { - this.categoryName = categoryName; - this.categoryID = null; +export class ShoppingCategory extends HTMLElement { + + static get observedAttributes() { return ["category-name"] } + + constructor() { + super(); + this._rendered = false; } - static fromJSON(categoryAsJSON) { - const category = new ShoppingCategory(categoryAsJSON.categoryName); - category.categoryID = categoryAsJSON.categoryID; - return category; - } - - render() { - const li = document.createElement("li"); + connectedCallback() { + if (this._rendered) return; const categoryHeaderDiv = document.createElement("div"); const span = document.createElement("span"); const deleteCategoryButton = document.createElement("button"); const labelDeleteAllItems = document.createElement("label"); const deleteAllItemsCheckbox = document.createElement("input"); - const innerUl = document.createElement("ul"); - li.dataset.id = this.categoryID; - li.classList.add("category"); + const ul = document.createElement("ul"); - categoryHeaderDiv.classList.add("category__div"); - span.textContent = this.categoryName; + categoryHeaderDiv.classList.add("category__header"); + + span.textContent = this.getAttribute("category-name"); span.classList.add("category__name", "category__name--bold", "category__name--big"); labelDeleteAllItems.classList.add("category__labelDeleteAll"); @@ -40,8 +36,24 @@ export class ShoppingCategory { categoryHeaderDiv.append(span, deleteCategoryButton, labelDeleteAllItems); - li.append(categoryHeaderDiv, innerUl); + this.prepend(categoryHeaderDiv); - return li; + if (!this.querySelector("ul")) this.append(ul); + + this._rendered = true; + + } + + attributeChangeCallback(name, oldVal, newVal) { + if (!this._rendered) return; + if (newVal == oldVal) return; + switch (name) { + case "category-name": + this.querySelector(".category__name").textContent = newVal; + break; + + default: + break; + } } } diff --git a/src/main/resources/static/js/pages/list/frontend/categoryUI.js b/src/main/resources/static/js/pages/list/frontend/categoryUI.js index 5da9439..46dd162 100644 --- a/src/main/resources/static/js/pages/list/frontend/categoryUI.js +++ b/src/main/resources/static/js/pages/list/frontend/categoryUI.js @@ -1,16 +1,22 @@ -import { ShoppingCategory } from "../../../models/ShoppingCategory.js"; export function addCategory(categoryAsJSON) { const list = document.querySelector(".displayedList"); - const category = ShoppingCategory.fromJSON(categoryAsJSON); + const category = document.createElement("shopping-category"); + const li = document.createElement("li"); - list.append(category.render()); //fügt Kategorie der Liste hinzu + li.classList.add("category"); + li.dataset.id = categoryAsJSON.categoryID; + category.setAttribute("category-name", categoryAsJSON.categoryName); + + li.append(category); //fügt Kategorie der Liste hinzu + + list.append(li); const categorySelect = document.querySelector(".addItemForm__categorySelect"); const newCategoryOption = document.createElement("option"); - newCategoryOption.setAttribute("value", category.categoryID); - newCategoryOption.textContent = category.categoryName; + newCategoryOption.setAttribute("value", categoryAsJSON.categoryID); + newCategoryOption.textContent = categoryAsJSON.categoryName; categorySelect.append(newCategoryOption); } @@ -26,16 +32,18 @@ export function deleteCategory(deleteAllItems, categoryID) { } -export function createEditCategoryForm(textElement) { +export function createEditCategoryForm(categoryName) { const editForm = document.createElement("form"); const inputName = document.createElement("input"); const abortButton = document.createElement("button"); const saveButton = document.createElement("button"); inputName.setAttribute("name", "newCategoryName"); - inputName.setAttribute("value", textElement.querySelector(".category__name").textContent); + inputName.setAttribute("value", categoryName); inputName.classList.add("editForm__name"); + abortButton.setAttribute("type", "button"); + abortButton.classList.add("editForm__buttonAbort"); abortButton.textContent = "Abbrechen"; saveButton.textContent = "Speichern"; diff --git a/src/main/resources/static/js/pages/list/list.js b/src/main/resources/static/js/pages/list/list.js index 92fc2ab..2a9ef86 100644 --- a/src/main/resources/static/js/pages/list/list.js +++ b/src/main/resources/static/js/pages/list/list.js @@ -1,3 +1,4 @@ +import { ShoppingCategory } from "../../models/ShoppingCategory.js"; import { ShoppingItem } from "../../models/ShoppingItem.js"; import { showError, initErrors } from "../../shared/error.js"; import * as api from "./api/listAPI.js" @@ -85,17 +86,16 @@ async function editItem(shoppingItem, id) { formElement.addEventListener("keydown", (e) => { if (e.key == "Escape") abort(); - }) + }); } -function editCategory(categoryElement) { +function editCategory(categoryElement, id) { //erstellen das Forms //wir nutzen für das Element nur das div, weil sonst alle items mitgelöscht werden würden - const categoryDiv = categoryElement.querySelector(".category__div"); - const formElement = ui.createEditCategoryForm(categoryDiv); - let id = categoryElement.dataset.id; + const categoryDiv = categoryElement.querySelector(".category__header"); + const formElement = ui.createEditCategoryForm(categoryDiv.querySelector(".category__name").textContent); categoryDiv.replaceWith(formElement); @@ -119,14 +119,23 @@ function editCategory(categoryElement) { ui.editCategory(id, formElement, formData, categoryDiv); } + function abort() { + formElement.replaceWith(categoryDiv); + } + formElement.addEventListener("submit", (event) => { event.preventDefault(); save(); }); + formElement.addEventListener("click", (e) => { + if (e.target.matches(".editForm__buttonAbort")) abort(); + }) + + formElement.addEventListener("keydown", (e) => { - if (e.key == "Escape") formElement.replaceWith(categoryDiv); + if (e.key == "Escape") abort(); }) } @@ -175,6 +184,8 @@ function init() { initErrors(document.querySelector(".nav")); customElements.define("shopping-item", ShoppingItem); + customElements.define("shopping-category", ShoppingCategory); + addCategoryForm.addEventListener("submit", (event) => { event.preventDefault(); let formData = Object.fromEntries(new FormData(addCategoryForm)); @@ -209,7 +220,8 @@ function init() { } else if (event.target.closest(".category__name")) { - editCategory(event.target.closest("li")); + const li = event.target.closest("li"); + editCategory(event.target.closest("shopping-category"), li.dataset.id); } }); diff --git a/src/main/resources/templates/list.html b/src/main/resources/templates/list.html index 583b4f0..9c0066a 100644 --- a/src/main/resources/templates/list.html +++ b/src/main/resources/templates/list.html @@ -53,20 +53,13 @@