Compare commits

..
9 Commits
Author SHA1 Message Date
Technoir c6bc6cf8f9 Merge branch 'feature/35-custom-element-category', fixes #35
eine Kategorie hat jetzt ein eigenes element <shopping-category> mit dem
Attribut category-name. Name wird geändert über setAttribute. Das
Element erstellt eine eigene <ul> falls es keine hat (zB wenn Kategorie
an sich leer oder im Frtonend erstellt)
2026-10-14 14:49:08 +02:00
Technoir a39eee06e9 editCategory angepasst, dass shopping-category und id übergeben wird 2026-10-14 14:24:36 +02:00
Technoir 4137b511bb abortButton hat Klasse 2026-10-14 14:24:16 +02:00
Technoir 2e11917ea1 addCategory erstellt ein li an was das cateogyr-element angehangen wird 2026-10-14 13:54:36 +02:00
Technoir 148c33cf6e neuer name wird über setAttribute gesetzt 2026-10-14 13:54:01 +02:00
Technoir d9fd91ad8d ul wird erstellt falls keins existiert
passiert wenn man im Frontend eine neue Kategorie erstellt
2026-10-14 13:53:44 +02:00
Technoir b5293e246c shopping-category in list.html eingefügt 2026-10-14 13:37:08 +02:00
Technoir b1aa5bec07 shopping-category implementiert 2026-10-14 13:37:01 +02:00
Technoir fd984529e7 shopping-category wird definiert 2026-10-14 13:36:55 +02:00
4 changed files with 72 additions and 47 deletions

No files matched your search

@@ -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;
}
}
}
@@ -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";
@@ -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);
}
});
+7 -14
View File
@@ -53,20 +53,13 @@
<ul class="displayedList">
<!-- Es werden erste alle Kategorien erstellt, jede Kategorie muss ein ul in einem li sein um Verschachtelung zu ermöglichen -->
<li th:each="category: ${categories}" th:data-id="${category.id}" class="category">
<div class="category__div">
<span class="category__name category__name--big category__name--bold" th:text="${category.categoryName}"></span>
<button class="category__deleteButton">Kategorie löschen</button>
<label class="category__labelCheckbox">
Gegenstände mit Kategorie löschen?
<input name="deleteAllInCategory" type="checkbox">
</label>
</div>
<!-- Items der Kategorie -->
<ul>
<li th:each="item: ${category.itemsInCategory}" th:data-id="${item.id}" class="item">
<shopping-item th:attr="item-name=${item.itemName}, item-amount=${item.itemAmount}, is-checked=${item.isDone}"></shopping-item>
</li>
</ul>
<shopping-category th:attr="category-name=${category.categoryName}">
<ul>
<li th:each="item: ${category.itemsInCategory}" th:data-id="${item.id}" class="item">
<shopping-item th:attr="item-name=${item.itemName}, item-amount=${item.itemAmount}, is-checked=${item.isDone}"></shopping-item>
</li>
</ul>
</shopping-category>
</li>
<li th:each="item: ${items}" th:data-id="${item.id}" class="item"> <!-- erstellung items ohne Kategorie -->