umbennen von ShoppingListItem zu ShoppingItem, erstellung des items aus JSON und render zu einem <li> implementiert
This commit is contained in:
1 parent
ffb4a7e2c6
commit
600c39352a
2 files changed
+42
-104
No files matched your search
@@ -0,0 +1,42 @@
|
||||
export class ShoppingItem {
|
||||
|
||||
constructor(itemName, itemAmount) {
|
||||
this.itemName = itemName;
|
||||
this.itemAmount = itemAmount;
|
||||
this.itemID = null;
|
||||
this.isEditing = true;
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
|
||||
const checkboxDone = document.createElement("input");
|
||||
const span = document.createElement("span");
|
||||
const deleteItemButton = document.createElement("button");
|
||||
|
||||
li.id = this.itemID;
|
||||
li.classList.add("item");
|
||||
|
||||
checkboxDone.setAttribute("type", "checkbox");
|
||||
|
||||
span.textContent = this.itemName + " " + this.itemAmount;
|
||||
|
||||
deleteItemButton.classList.add("item__deleteButton");
|
||||
deleteItemButton.textContent = "Löschen";
|
||||
|
||||
li.appendChild(checkboxDone)
|
||||
li.appendChild(span);
|
||||
li.appendChild(deleteItemButton);
|
||||
|
||||
return li;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
export class ShoppingListItem {
|
||||
|
||||
constructor(itemName, itemAmount, onChange) {
|
||||
this.itemName = itemName;
|
||||
this.itemAmount = itemAmount;
|
||||
this.isEditing = false;
|
||||
this.isDone = false;
|
||||
this.remove = false;
|
||||
this.onChange = onChange;
|
||||
}
|
||||
|
||||
//gibt ein <li> Element zurück
|
||||
render() {
|
||||
let li;
|
||||
if (this.isEditing) {
|
||||
li = this.#renderEditing();
|
||||
} else {
|
||||
li = this.#renderNormal()
|
||||
}
|
||||
return li;
|
||||
|
||||
}
|
||||
|
||||
#renderEditing() {
|
||||
const li = document.createElement("li");
|
||||
const editForm = document.createElement("form");
|
||||
const editItemNameField = document.createElement("input");
|
||||
const editItemAmountField = document.createElement("input");
|
||||
const saveButton = document.createElement("button");
|
||||
const abortButton = document.createElement("button");
|
||||
|
||||
|
||||
editItemNameField.setAttribute("name", "newItemName");
|
||||
editItemNameField.setAttribute("value", this.itemName);
|
||||
|
||||
editItemAmountField.setAttribute("itemName", "newItemAmount");
|
||||
editItemAmountField.setAttribute("value", this.itemAmount);
|
||||
|
||||
abortButton.textContent = "Abbrechen";
|
||||
abortButton.addEventListener("click", (event) => {
|
||||
event.preventDefault();
|
||||
this.isEditing = false;
|
||||
this.onChange();
|
||||
});
|
||||
|
||||
saveButton.textContent = "Speichern";
|
||||
editForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
const editFormData = Object.fromEntries(new FormData(editForm));
|
||||
|
||||
if (!editFormData.newItemName && !editFormData.newItemAmount) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.itemName = editFormData.newItemName;
|
||||
this.itemAmount = editFormData.newItemAmount;
|
||||
this.isEditing = false;
|
||||
this.onChange();
|
||||
});
|
||||
|
||||
editForm.appendChild(editItemNameField);
|
||||
editForm.appendChild(editItemAmountField);
|
||||
editForm.appendChild(saveButton);
|
||||
editForm.appendChild(abortButton);
|
||||
li.appendChild(editForm);
|
||||
|
||||
return li;
|
||||
}
|
||||
|
||||
#renderNormal() {
|
||||
const li = document.createElement("li");
|
||||
const listContent = document.createElement("span");
|
||||
const removeButton = document.createElement("button");
|
||||
const doneButton = document.createElement("input");
|
||||
|
||||
|
||||
doneButton.setAttribute("type", "checkbox");
|
||||
doneButton.addEventListener("change", () => {
|
||||
this.isDone = true;
|
||||
li.classList.toggle("displayedList__item--checked");
|
||||
});
|
||||
|
||||
listContent.addEventListener("click", () => {
|
||||
this.isEditing = true;
|
||||
this.onChange();
|
||||
});
|
||||
|
||||
removeButton.textContent = "Löschen";
|
||||
removeButton.addEventListener("click", () => {
|
||||
this.remove = true;
|
||||
this.onChange();
|
||||
});
|
||||
|
||||
listContent.textContent = this.itemName + " " + this.itemAmount;
|
||||
|
||||
li.appendChild(doneButton);
|
||||
li.appendChild(listContent);
|
||||
li.appendChild(removeButton);
|
||||
|
||||
return li;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user