Compare commits
10
Commits
3be624df0d
...
1750f8059b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1750f8059b | ||
|
|
49df5b01f5 | ||
|
|
f870421f66 | ||
|
|
be2a38fedc | ||
|
|
882bc0e577 | ||
|
|
5f3a6c56f9 | ||
|
|
4af57ba1de | ||
|
|
ce93c7a56e | ||
|
|
07b58ccad7 | ||
|
|
b3702c0029 |
No files matched your search
+11
-4
@@ -13,14 +13,21 @@
|
||||
</div>
|
||||
|
||||
<div class="list">
|
||||
<form class="list__inputForm">
|
||||
<form class="list__form__addCategory">
|
||||
<span>Neue Kategorie hinzufügen:</span>
|
||||
<input type="text" class="list__inputForm__categoryName" name="categoryName" placeholder="Neue Kategorie hinzufügen...">
|
||||
<button class="list__inputForm_submitCategoryButton" type="submit">Hinzufügen</button>
|
||||
</form>
|
||||
|
||||
<br>
|
||||
|
||||
<form class="list__form__addItem">
|
||||
<span>Neuen Gegenstand hinzufügen:</span>
|
||||
<input type="text" class="list__inputForm__itemName" name="itemName" placeholder="Gegenstand eingeben...">
|
||||
<input type="text" class="list__inputForm__itemAmount" name="itemAmount" placeholder="Menge eingeben...">
|
||||
<select class="list__inputForm_selectCategory">
|
||||
|
||||
<select class="list__inputForm__selectCategory" name="itemCategory">
|
||||
</select>
|
||||
<button class="list__inputForm__submitButton" type="submit">Hinzufügen</button>
|
||||
<button class="list__inputForm__submitItemButton" type="submit">Hinzufügen</button>
|
||||
</form>
|
||||
|
||||
<ul class="list__displayedList">
|
||||
|
||||
@@ -1,3 +1,31 @@
|
||||
export class ShoppingCategory {
|
||||
|
||||
constructor(categoryName) {
|
||||
this.categoryName = categoryName;
|
||||
this.itemsInCategory = [];
|
||||
this.remove = false;
|
||||
this.isEditing = false;
|
||||
}
|
||||
|
||||
render() {
|
||||
let element;
|
||||
if (this.isEditing) {
|
||||
element = this.#renderEditing();
|
||||
} else {
|
||||
element = this.#renderNormal();
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
#renderEditing() {
|
||||
|
||||
}
|
||||
|
||||
#renderNormal() {
|
||||
}
|
||||
|
||||
addItem(item) {
|
||||
this.itemsInCategory.push(item);
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,10 @@ export class ShoppingListItem {
|
||||
|
||||
|
||||
editNameField.setAttribute("name", "newItemName");
|
||||
editNameField.setAttribute("value", this.name);
|
||||
|
||||
editAmountField.setAttribute("name", "newItemAmount");
|
||||
editAmountField.setAttribute("value", this.amount);
|
||||
|
||||
abortButton.textContent = "Abbrechen";
|
||||
abortButton.addEventListener("click", (event) => {
|
||||
|
||||
+55
-13
@@ -1,38 +1,80 @@
|
||||
import { ShoppingListItem } from "./ShoppingListItem";
|
||||
import { ShoppingCategory } from "./ShoppingCategory";
|
||||
|
||||
const form = document.querySelector(".list__inputForm");
|
||||
const addItemForm = document.querySelector(".list__form__addItem");
|
||||
const addCategoryForm = document.querySelector(".list__form__addCategory")
|
||||
const itemListElement = document.querySelector(".list__displayedList");
|
||||
|
||||
let listItems = []; //list of items that the user has
|
||||
let listElements = []; // Liste der erstellten Items und Kategorien
|
||||
|
||||
function addNewCategory(categoryName) {
|
||||
listElements.push(new ShoppingCategory(categoryName));
|
||||
return 0;
|
||||
}
|
||||
|
||||
function handleListElement(itemName, itemAmount,) {
|
||||
listItems.push(new ShoppingListItem(itemName, itemAmount, renderList));
|
||||
renderList();
|
||||
function addNewItem(itemName, itemAmount, category) {
|
||||
if (category) {
|
||||
category = listElements.find((element) => element instanceof ShoppingCategory && element.categoryName == category)
|
||||
category.addItem(new ShoppingListItem(itemName, itemAmount, renderItemList));
|
||||
} else {
|
||||
listElements.push(new ShoppingListItem(itemName, itemAmount, renderItemList));
|
||||
}
|
||||
renderItemList();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function renderList() {
|
||||
listItems = listItems.filter(item => !item.remove)
|
||||
function renderItemList() {
|
||||
listElements = listElements.filter(item => !item.remove)
|
||||
itemListElement.innerHTML = ""; //reset innerItemList
|
||||
|
||||
listItems.forEach((element, index) => {
|
||||
listElements.forEach((element, index) => {
|
||||
itemListElement.appendChild(element.render(index));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function renderAddItemPrompt() {
|
||||
const selectMenu = document.querySelector(".list__inputForm__selectCategory");
|
||||
let categories = listElements.filter(element => element instanceof ShoppingCategory);
|
||||
const emptySelection = document.createElement("option");
|
||||
|
||||
emptySelection.setAttribute("value", "");
|
||||
emptySelection.selected = true;
|
||||
emptySelection.textContent = "Keine"
|
||||
|
||||
selectMenu.innerHTML = "";
|
||||
selectMenu.appendChild(emptySelection);
|
||||
|
||||
categories.forEach(category => {
|
||||
const option = document.createElement("option");
|
||||
option.setAttribute("value", category.categoryName);
|
||||
option.textContent = category.categoryName;
|
||||
|
||||
selectMenu.appendChild(option);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
document.querySelector(".list__inputForm").addEventListener("submit", (event) => {
|
||||
if (!form.checkValidity()) return;
|
||||
renderAddItemPrompt();
|
||||
|
||||
document.querySelector(".list__form__addItem").addEventListener("submit", (event) => {
|
||||
if (!addItemForm.checkValidity()) return;
|
||||
event.preventDefault();
|
||||
const formData = Object.fromEntries(new FormData(form));
|
||||
handleListElement(formData.itemName, formData.itemAmount);
|
||||
const addItemFormData = Object.fromEntries(new FormData(addItemForm));
|
||||
addNewItem(addItemFormData.itemName, addItemFormData.itemAmount, addItemFormData.itemCategory);
|
||||
});
|
||||
|
||||
|
||||
document.querySelector(".list__form__addCategory").addEventListener("submit", (event) => {
|
||||
if (!addCategoryForm.checkValidity()) return;
|
||||
event.preventDefault();
|
||||
const addCategoryFormData = Object.fromEntries(new FormData(addCategoryForm));
|
||||
addNewCategory(addCategoryFormData.categoryName);
|
||||
renderAddItemPrompt();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -2,3 +2,10 @@
|
||||
li:has(input:checked){
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
ul[title]::before {
|
||||
content: attr(title);
|
||||
display: block;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user