-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
169 lines (142 loc) · 4.29 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
console.log("TEST");
//Variables
const todoInput = document.querySelector("#task-input");
const taskContainer = document.querySelector(".ctn-tasks");
const form = document.querySelector(".ctn-input");
let todos = [];
console.log(todos);
//Todos im Lokalen Speicher speichern und abrufen ✅
// Function to save todos to local storage
function saveTodos() {
localStorage.setItem("todos", JSON.stringify(todos));
}
// Function to load todos from local storage
function loadTodos() {
const storedTodos = localStorage.getItem("todos");
if (storedTodos !== null) {
todos = JSON.parse(storedTodos);
todos.forEach(function (todo) {
renderTodo(todo);
});
}
}
//add todos to todolist
function addTodo(event) {
event.preventDefault();
const inputValue = todoInput.value.trim();
if (inputValue !== "") {
//creating object
const newTodo = {
description: inputValue,
done: false,
id: Math.floor(Math.random() * 99999999),
};
//Push & render
todos.push(newTodo);
console.log(todos);
renderTodo(newTodo);
//TEST
console.log(todos);
//Clear Value
todoInput.value = "";
//TEST
console.log(newTodo);
}
saveTodos();
}
//Creating new todo ✅
function renderTodo(todo) {
const taskItem = document.createElement("div");
taskItem.classList.add("task-item");
//Create Checkbox and connect with Object status done: true/false
const checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.id = "task-" + todo.id;
//TEST
console.log("log in renderTodo : ", todo.done);
checkBox.todoObj = todo;
//Create label, set ID and connect with Object description
const taskText = document.createElement("label");
taskText.textContent = todo.description;
taskText.setAttribute("for", "task-" + todo.id);
//Add checkbox and label to task-Item (html-div task-item)
taskItem.appendChild(checkBox);
taskItem.appendChild(taskText);
//add to task-container (html-div ctn-task)
taskContainer.appendChild(taskItem);
}
//Toggle Checkbox
function toggleCheckbox(event) {
if (event.target.type === "checkbox") {
const todo = event.target.todoObj;
todo.done = event.target.checked;
console.log("Updated todo:", todo);
}
saveTodos();
}
//Eventlistener submit input
form.addEventListener("submit", addTodo);
//Eventlistener Checkbox checked
taskContainer.addEventListener("change", toggleCheckbox);
// Duplikatprüfung
//Filtern von Todos ✅
const active = document.querySelector("#active");
const done = document.querySelector("#done");
const all = document.querySelector("#all");
function showTodos(filter) {
const taskItems = document.querySelectorAll(".task-item");
taskItems.forEach(function (item) {
switch (filter) {
case "all":
item.style.display = "block";
break;
case "active":
if (!item.querySelector('input[type="checkbox"]').checked) {
item.style.display = "block";
} else {
item.style.display = "none";
}
break;
case "done":
if (item.querySelector('input[type="checkbox"]').checked) {
item.style.display = "block";
} else {
item.style.display = "none";
}
break;
}
});
}
// Eventlistener for filteroption "Active"
active.addEventListener("click", function () {
showTodos("active");
});
// Eventlistener for filteroption "Done"
done.addEventListener("click", function () {
showTodos("done");
});
// Eventlistener for filteroption "All"
all.addEventListener("click", function () {
showTodos("all");
});
//Erledigte ToDos entfernen ✅
const clear = document.querySelector(".btn-clear");
function removeDone(_event) {
// Iteriere rückwärts durch das Array
for (let i = todos.length - 1; i >= 0; i--) {
if (todos[i].done) {
//Get the ID of the task which is supposed to be deleted
const checkboxId = "task-" + todos[i].id;
const taskItem = document.getElementById(checkboxId).parentNode;
taskItem.remove();
todos.splice(i, 1);
}
}
saveTodos();
}
clear.addEventListener("click", removeDone);
// Initial load of todos from local storage
document.addEventListener("DOMContentLoaded", function () {
loadTodos(); // Zuerst die Todos laden
saveTodos(); // Dann die Todos speichern (falls welche geladen wurden)
});