commit
2e6e54fb19
6 files changed
+93
-67
No files matched your search
@@ -6,7 +6,7 @@
|
||||
font-size : 1.5rem;
|
||||
}
|
||||
|
||||
.item:has(.item__markAsDone:checked) .item__text{
|
||||
shopping-item[is-checked="true"] .item__text{
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,52 +1,59 @@
|
||||
export class ShoppingItem {
|
||||
export class ShoppingItem extends HTMLElement {
|
||||
|
||||
constructor(itemName, itemAmount) {
|
||||
this.itemName = itemName;
|
||||
this.itemAmount = itemAmount;
|
||||
this.itemID = null;
|
||||
this.isEditing = true;
|
||||
static get observedAttributes() { return ["item-name", "item-amount"] }
|
||||
constructor() {
|
||||
super();
|
||||
this._rendered = false;
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
if (this._rendered) return;
|
||||
|
||||
static fromJSON(itemAsJSON) {
|
||||
const item = new ShoppingItem(itemAsJSON.itemName, itemAsJSON.itemAmount);
|
||||
item.itemID = itemAsJSON.itemID;
|
||||
return item;
|
||||
}
|
||||
|
||||
// gibt das darzustellende <li> zurück
|
||||
render() {
|
||||
const li = document.createElement("li");
|
||||
|
||||
console.log(`rendering element shopping-item`);
|
||||
const checkboxDone = document.createElement("input");
|
||||
const span = document.createElement("span");
|
||||
const spanText = document.createElement("span");
|
||||
const spanName = document.createElement("span");
|
||||
const spanAmount = document.createElement("span");
|
||||
const buttonDelete = document.createElement("button");
|
||||
|
||||
const deleteItemButton = document.createElement("button");
|
||||
|
||||
li.dataset.id = this.itemID;
|
||||
li.classList.add("item");
|
||||
|
||||
checkboxDone.classList.add("item__checkboxDone");
|
||||
checkboxDone.setAttribute("type", "checkbox");
|
||||
checkboxDone.setAttribute("autocomplete", "off");
|
||||
checkboxDone.classList.add("item__markAsDone");
|
||||
checkboxDone.checked = this.getAttribute("is-checked") === "true";
|
||||
|
||||
span.classList.add("item__text");
|
||||
spanName.classList.add("item__textName");
|
||||
spanAmount.classList.add("item__textAmount");
|
||||
spanText.classList.add("item__text");
|
||||
|
||||
spanName.textContent = this.itemName;
|
||||
spanAmount.textContent = this.itemAmount;
|
||||
spanName.classList.add("item__itemName");
|
||||
spanAmount.classList.add("item__itemAmount");
|
||||
|
||||
deleteItemButton.classList.add("item__deleteButton");
|
||||
deleteItemButton.textContent = "Löschen";
|
||||
spanName.textContent = this.getAttribute("item-name");
|
||||
spanAmount.textContent = this.getAttribute("item-amount");
|
||||
|
||||
span.append(spanName, spanAmount);
|
||||
buttonDelete.classList.add("item__buttonDeleteItem");
|
||||
buttonDelete.setAttribute("type", "button ");
|
||||
buttonDelete.textContent = "Löschen";
|
||||
|
||||
li.append(checkboxDone, span, deleteItemButton);
|
||||
spanText.append(spanName, spanAmount);
|
||||
|
||||
return li;
|
||||
this.append(checkboxDone, spanText, buttonDelete);
|
||||
this._rendered = true;
|
||||
|
||||
}
|
||||
|
||||
attributeChangedCallback(name, oldVal, newVal) {
|
||||
if (!this._rendered) return;
|
||||
if (newVal == oldVal) return;
|
||||
switch (name) {
|
||||
case "item-name":
|
||||
this.querySelector(".item__itemName").textContent = newVal;
|
||||
break;
|
||||
case "item-amount":
|
||||
this.querySelector(".item__itemAmount").textContent = newVal;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,13 +7,20 @@ export function addItem(itemAsJSON) {
|
||||
} else {
|
||||
list = document.querySelector(`li[data-id='${itemAsJSON.categoryID}']`).querySelector("ul");
|
||||
}
|
||||
const li = document.createElement("li");
|
||||
const item = document.createElement("shopping-item");
|
||||
|
||||
const item = ShoppingItem.fromJSON(itemAsJSON);
|
||||
li.dataset.id = itemAsJSON.itemID;
|
||||
li.classList.add("item");
|
||||
|
||||
list.append(item.render());
|
||||
item.setAttribute("item-name", itemAsJSON.itemName);
|
||||
item.setAttribute("item-amount", itemAsJSON.itemAmount);
|
||||
|
||||
li.append(item);
|
||||
list.append(li);
|
||||
}
|
||||
|
||||
export function createEditItemForm(textElement) {
|
||||
export function createEditItemForm(currentName, currentAmount) {
|
||||
const editForm = document.createElement("form");
|
||||
const inputName = document.createElement("input");
|
||||
const inputAmount = document.createElement("input");
|
||||
@@ -21,16 +28,20 @@ export function createEditItemForm(textElement) {
|
||||
const saveButton = document.createElement("button");
|
||||
|
||||
inputName.setAttribute("name", "newItemName");
|
||||
inputName.setAttribute("value", textElement.querySelector(".item__textName").textContent);
|
||||
inputName.setAttribute("value", currentName);
|
||||
inputName.classList.add("editForm__name");
|
||||
|
||||
inputAmount.setAttribute("name", "newItemAmount");
|
||||
inputAmount.setAttribute("value", textElement.querySelector(".item__textAmount").textContent);
|
||||
inputAmount.setAttribute("value", currentAmount);
|
||||
inputAmount.classList.add("editForm__amount");
|
||||
|
||||
|
||||
abortButton.setAttribute("type", "button");
|
||||
abortButton.classList.add("editForm__buttonAbort");
|
||||
abortButton.textContent = "Abbrechen";
|
||||
|
||||
saveButton.textContent = "Speichern";
|
||||
saveButton.setAttribute("type", "submit");
|
||||
|
||||
|
||||
editForm.append(inputName, inputAmount, saveButton, abortButton);
|
||||
@@ -38,14 +49,15 @@ export function createEditItemForm(textElement) {
|
||||
return editForm;
|
||||
}
|
||||
|
||||
export function editItem(textElement, formElement, formData) {
|
||||
export function editItem(shoppingItemElement, formElement, formData) {
|
||||
|
||||
let newName = formData.newItemName;
|
||||
let newAmount = formData.newItemAmount;
|
||||
textElement.querySelector(".item__textName").textContent = newName;
|
||||
textElement.querySelector(".item__textAmount").textContent = newAmount;
|
||||
shoppingItemElement.setAttribute("item-name", newName);
|
||||
shoppingItemElement.setAttribute("item-amount", newAmount);
|
||||
|
||||
formElement.replaceWith(textElement);
|
||||
|
||||
formElement.replaceWith(shoppingItemElement);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ export * from "./itemUI.js";
|
||||
export * from "./categoryUI.js";
|
||||
|
||||
export function deleteAllDoneItems() {
|
||||
const doneItems = document.querySelectorAll("li.item:has(input[type=checkbox]:checked)");
|
||||
const doneItems = document.querySelectorAll("li.item:has(shopping-item[is-checked=true])");
|
||||
doneItems.forEach((li) => {
|
||||
li.remove();
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ShoppingItem } from "../../models/ShoppingItem.js";
|
||||
import { showError, initErrors } from "../../shared/error.js";
|
||||
import * as api from "./api/listAPI.js"
|
||||
import * as ui from "./frontend/listUI.js"
|
||||
@@ -41,15 +42,14 @@ async function addCategory(categoryName) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//textElement ist immer das li mit der id
|
||||
async function editItem(textElement) {
|
||||
//shoppingItem ist immer das li mit der id
|
||||
async function editItem(shoppingItem, id) {
|
||||
|
||||
|
||||
//erstellen das Forms
|
||||
const formElement = ui.createEditItemForm(textElement)
|
||||
let id = textElement.dataset.id;
|
||||
const formElement = ui.createEditItemForm(shoppingItem.querySelector(".item__itemName").textContent, shoppingItem.querySelector(".item__itemAmount").textContent);
|
||||
|
||||
textElement.replaceWith(formElement);
|
||||
shoppingItem.replaceWith(formElement);
|
||||
|
||||
formElement.querySelector(".editForm__name").focus();
|
||||
|
||||
@@ -67,7 +67,11 @@ async function editItem(textElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
ui.editItem(textElement, formElement, formData);
|
||||
ui.editItem(shoppingItem, formElement, formData);
|
||||
}
|
||||
|
||||
function abort() {
|
||||
formElement.replaceWith(shoppingItem);
|
||||
}
|
||||
|
||||
formElement.addEventListener("submit", (event) => {
|
||||
@@ -75,8 +79,12 @@ async function editItem(textElement) {
|
||||
save();
|
||||
});
|
||||
|
||||
formElement.addEventListener("click", (e) => {
|
||||
if (e.target.matches(".editForm__buttonAbort")) abort();
|
||||
})
|
||||
|
||||
formElement.addEventListener("keydown", (e) => {
|
||||
if (e.key == "Escape") formElement.replaceWith(textElement);
|
||||
if (e.key == "Escape") abort();
|
||||
})
|
||||
|
||||
|
||||
@@ -111,6 +119,7 @@ function editCategory(categoryElement) {
|
||||
ui.editCategory(id, formElement, formData, categoryDiv);
|
||||
}
|
||||
|
||||
|
||||
formElement.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
save();
|
||||
@@ -165,6 +174,7 @@ function init() {
|
||||
|
||||
initErrors(document.querySelector(".nav"));
|
||||
|
||||
customElements.define("shopping-item", ShoppingItem);
|
||||
addCategoryForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
let formData = Object.fromEntries(new FormData(addCategoryForm));
|
||||
@@ -183,24 +193,31 @@ function init() {
|
||||
|
||||
//event delegation ist schon geil
|
||||
document.querySelector(".displayedList").addEventListener("click", (event) => {
|
||||
if (event.target.matches(".item__deleteButton")) {
|
||||
if (event.target.matches(".item__buttonDeleteItem")) {
|
||||
deleteItem(event.target.closest("li"));
|
||||
|
||||
} else if (event.target.matches(".category__deleteButton")) {
|
||||
|
||||
const li = event.target.closest("li");
|
||||
const checkbox = li.querySelector("input[type=checkbox]");
|
||||
deleteCategory(checkbox.checked, li.dataset.id);
|
||||
|
||||
} else if (event.target.closest(".item__text")) {
|
||||
editItem(event.target.closest("li"));
|
||||
|
||||
const shoppingItemElement = event.target.closest("shopping-item");
|
||||
editItem(shoppingItemElement, shoppingItemElement.closest("li").dataset.id);
|
||||
|
||||
} else if (event.target.closest(".category__name")) {
|
||||
|
||||
editCategory(event.target.closest("li"));
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
document.querySelector(".displayedList").addEventListener("change", (e) => {
|
||||
if (e.target.matches(".item__markAsDone")) {
|
||||
if (e.target.matches(".item__checkboxDone")) {
|
||||
const item = e.target.closest("li");
|
||||
e.target.closest("shopping-item").setAttribute("is-checked", e.target.checked);
|
||||
api.editItem(item.dataset.id, { isDone: e.target.checked });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -63,24 +63,14 @@
|
||||
</div>
|
||||
<!-- Items der Kategorie -->
|
||||
<ul>
|
||||
<li th:each="item: ${category.itemsInCategory}" th:data-id="${item.id}" class="item"> <!-- erstellung von einem Item -->
|
||||
<input type="checkbox" class="item__markAsDone" th:checked="${item.isDone}" autocomplete="off">
|
||||
<span class="item__text">
|
||||
<span th:text="${item.itemName}" class="item__textName"></span>
|
||||
<span th:text="${item.itemAmount}" class="item__textAmount"></span>
|
||||
</span>
|
||||
<button class="item__deleteButton">Löschen</button>
|
||||
</li>
|
||||
<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>
|
||||
</li>
|
||||
|
||||
<li th:each="item: ${items}" th:data-id="${item.id}" class="item"> <!-- erstellung items ohne Kategorie -->
|
||||
<input type="checkbox" class="item__markAsDone" th:checked="${item.isDone}" autocomplete="off">
|
||||
<span class="item__text">
|
||||
<span th:text="${item.itemName}" class="item__textName"></span>
|
||||
<span th:text="${item.itemAmount}" class="item__textAmount"></span>
|
||||
</span>
|
||||
<button class="item__deleteButton">Löschen</button>
|
||||
<shopping-item th:attr="item-name=${item.itemName}, item-amount=${item.itemAmount}, is-checked=${item.isDone}"></shopping-item>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
|
||||
Reference in new issue
Block a user