-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-storage.html
236 lines (197 loc) · 8.33 KB
/
local-storage.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"/>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.dropdown-toggle::after {
display: none;;
}
.task {
display: flex;
align-items: center;
justify-content: space-between;
}
.task label.checked {
text-decoration: line-through;
}
.controls {
display: flex;
align-items: center;
justify-content: space-between;
}
.filters span.active {
color: #3C87FF;
}
.filters span {
margin-right: 5px;
font-size: 15px;
column-rule-color: #444444;
cursor: pointer;
}
</style>
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
Todo App
</div>
<div class="card-body">
<form>
<div class="input-group">
<input type="text" id="txtTaskName" class="form-control">
<button type="submit" class="btn btn-primary" id="btnAddNewTask">
Add
</button>
</div>
</form>
</div>
</div>
<div class="card mt-3">
<div class="card-header controls">
<div class="filters">
<span class="active" id="all">All</span>
<span id="pending">To-Do</span>
<span id="completed">Completed</span>
</div>
<button id="btnClear" class="btn btn-danger btn-sm float-end">Clear All</button>
</div>
<ul id="task-list" class="list-group list-group-flush">
</ul>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
"use strict";
let gorevListesi = [];
if (localStorage.getItem("gorevListesi") !== null) {
gorevListesi = JSON.parse(localStorage.getItem("gorevListesi"));
}
let editId;
let isEditTask = false;
const taskInput = document.querySelector("#txtTaskName");
const btnClear = document.querySelector("#btnClear");
const filters = document.querySelectorAll(".filters span")
displayTasks("all");
function displayTasks(filter) {
let ul = document.getElementById("task-list");
ul.innerHTML = "";
if (gorevListesi.length == 0) {
ul.innerHTML = "<p class='p-3 m-0'>Task list is empty.</p>"
} else {
for(let gorev of gorevListesi) {
let completed = gorev.durum == "completed" ? "checked": "";
if (filter == gorev.durum || filter == "all") {
let li = `
<li class="task list-group-item">
<div class="form-check">
<input type="checkbox" onclick="updateStatus(this)" id="${gorev.id}" class="form-check-input" ${completed}>
<label for="${gorev.id}" class="form-check-label ${completed}">${gorev.gorevAdi}</label>
</div>
<div class="dropdown">
<button class="btn btn-link dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fa-solid fa-ellipsis"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a onclick="deleteTask(${gorev.id})" class="dropdown-item" href="#"><i class="fa-solid fa-trash-can"></i> Delete</a></li>
<li><a onclick='editTask(${gorev.id}, "${gorev.gorevAdi}")' class="dropdown-item" href="#"><i class="fa-solid fa-pen"></i> Edit</a></li>
</ul>
</div>
</li>
`;
ul.insertAdjacentHTML("beforeend", li);
}
}
}
}
document.querySelector("#btnAddNewTask").addEventListener("click", newTask);
document.querySelector("#btnAddNewTask").addEventListener("keypress", function(){
if (event.key == "Enter") {
document.getElementById("btnAddNewTask").click();
}
});
for(let span of filters) {
span.addEventListener("click", function() {
document.querySelector("span.active").classList.remove("active");
span.classList.add("active");
displayTasks(span.id);
})
}
function newTask(event) {
if(taskInput.value == "") {
alert("görev girmelisiniz");
} else {
if(!isEditTask) {
// ekleme
gorevListesi.push({"id": gorevListesi.length + 1, "gorevAdi": taskInput.value, "durum": "pending"});
} else {
// güncelleme
for(let gorev of gorevListesi) {
if(gorev.id == editId) {
gorev.gorevAdi = taskInput.value;
}
isEditTask = false;
}
}
taskInput.value = "";
displayTasks(document.querySelector("span.active").id);
localStorage.setItem("gorevListesi", JSON.stringify(gorevListesi));
}
event.preventDefault();
}
function deleteTask(id) {
let deletedId;
for(let index in gorevListesi) {
if(gorevListesi[index].id == id) {
deletedId = index;
}
}
gorevListesi.splice(deletedId, 1);
displayTasks(document.querySelector("span.active").id);
localStorage.setItem("gorevListesi", JSON.stringify(gorevListesi));
}
function editTask(taskId, taskName) {
editId = taskId;
isEditTask = true;
taskInput.value = taskName;
taskInput.focus();
taskInput.classList.add("active");
console.log("edit id:", editId);
console.log("edit mode", isEditTask);
}
btnClear.addEventListener("click", function() {
gorevListesi.splice(0, gorevListesi.length);
localStorage.setItem("gorevListesi", JSON.stringify(gorevListesi));
displayTasks();
})
function updateStatus(selectedTask) {
// console.log(selectedTask.parentElement.lastElementChild);
let label = selectedTask.nextElementSibling;
let durum;
if(selectedTask.checked) {
label.classList.add("checked");
durum = "completed";
} else {
label.classList.remove("checked");
durum = "pending";
}
for (let gorev of gorevListesi) {
if(gorev.id == selectedTask.id) {
gorev.durum = durum;
}
}
displayTasks(document.querySelector("span.active").id);
localStorage.setItem("gorevListesi", JSON.stringify(gorevListesi));
}
</script>
</body>
</html>