From e6ccba021934ee2a0f300cb54db7860b0d40c3f5 Mon Sep 17 00:00:00 2001 From: Tim Aldinger Date: Thu, 15 Oct 2026 12:56:57 +0200 Subject: [PATCH] ShoppingList zu einem custom html-element gemacht --- .../static/js/models/ShoppingList.js | 56 +++++++++---------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/main/resources/static/js/models/ShoppingList.js b/src/main/resources/static/js/models/ShoppingList.js index 3974cae..c357777 100644 --- a/src/main/resources/static/js/models/ShoppingList.js +++ b/src/main/resources/static/js/models/ShoppingList.js @@ -1,31 +1,24 @@ +export class ShoppingList extends HTMLElement { + static get observedAttributes() { return ["list-name"] } -export class ShoppingList { - constructor(name) { - this.listName = name; - this.lastModified = ""; - this.created = ""; - this.listID = 0; + constructor() { + super(); + this._rendered = false } - static fromJSON(listAsJSON) { - const list = new ShoppingList(listAsJSON.listName); - list.listID = listAsJSON.listID; - return list; - } + connectedCallback() { + if (this._rendered) return; - render() { - const li = document.createElement("li"); const a = document.createElement("a"); const editButton = document.createElement("button"); const deleteButton = document.createElement("button"); - li.classList.add("list"); - li.dataset.id = this.listID; + const listID = this.closest("li").dataset.id; - a.setAttribute("href", `/list/${this.listID}`); + a.setAttribute("href", `/list/${listID}`); a.classList.add("list__name"); - a.textContent = this.listName; + a.textContent = this.getAttribute("list-name"); editButton.classList.add("list__editButton"); editButton.textContent = "Liste bearbeiten"; @@ -33,23 +26,24 @@ export class ShoppingList { deleteButton.classList.add("list__deleteButton"); deleteButton.textContent = "Liste löschen"; - li.append(a, editButton, deleteButton); - - return li; + this.append(a, editButton, deleteButton); + this._rendered = true; } - renderNavElement() { - const li = document.createElement("li"); - const a = document.createElement("a"); + attributeChangedCallback(name, oldVal, newVal) { + if (!this._rendered) return; + if (oldVal == newVal) return; - li.classList.add("navbar__list"); - li.dataset.id = this.listID; + const listLi = this.closest("li"); - a.setAttribute("href", `/list/${this.listID}`); - a.textContent = this.listName; - - li.append(a); - - return li; + switch (name) { + case "list-name": + this.querySelector("list__name").textContent = name; + document.querySelector(`.navbar__list[data-id='${listLi.dataset.id}'] > a`).textContent = newVal; + break; + default: + break; + } } } +