-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtodolistproje.js
162 lines (133 loc) · 4.36 KB
/
todolistproje.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
//Tüm Elementleri Seçmek
const form = document.querySelector("#todoAddForm");
const addInput = document.querySelector("#todoName");
const todoList = document.querySelector(".list-group");
const firstCardBody = document.querySelectorAll(".card-body")[0];
const secondCardBody = document.querySelectorAll(".card-body")[1];
const clearButton = document.querySelector("#clearButton");
const filterInput = document.querySelector("#todoSearch");
let todos = [];
runEvents();
function runEvents() {
form.addEventListener("submit", addTodo);
document.addEventListener("DOMContentLoaded",pageLoaded);
secondCardBody.addEventListener("click",removeTodoToUI);
clearButton.addEventListener("click",allTodosEverywhere);
filterInput.addEventListener("keyup",filter);
}
function pageLoaded(){
checkTodosFromStorage();
todos.forEach(function(todo){
addTodoToUI(todo);
});
}
function filter(e){
const filterValue = e.target.value.toLowerCase().trim();
const todoListesi = document.querySelectorAll(".list-group-item");
if(todoListesi.length>0){
todoListesi.forEach(function(todo){
if(todo.textContent.toLowerCase().trim().includes(filterValue)){
//
todo.setAttribute("style","display : block");
}else{
todo.setAttribute("style","display : none !important");
}
});
}else{
showAlert("warning","Filtreleme yapmak için en az bir todo olmalıdır!");
}
}
function allTodosEverywhere(){
const todoListesi = document.querySelectorAll(".list-group-item");
if(todoListesi.length>0){
// Ekrandan Silme
todoListesi.forEach(function(todo){
todo.remove();
});
// Storage'dan Silme
todos=[];
localStorage.setItem("todos",JSON.stringify(todos));
showAlert("success","Başarılı bir şekilde silindi");
}else{
showAlert("warning","Silmek için en az bir todo olmalıdır");
}
}
function removeTodoToUI(e){
if(e.target.className==="fa fa-remove"){
// Ekrandan Silme
const todo = e.target.parentElement.parentElement;
todo.remove();
// Storage'dan Silme
removeTodoToStorage(todo.textContent);
showAlert("success","Todo başarıyla silindi.");
}
}
function removeTodoToStorage(removeTodo){
checkTodosFromStorage();
todos.forEach(function(todo,index){
if(removeTodo===todo){
todos.splice(index,1);
}
});
localStorage.setItem("todos",JSON.stringify(todos));
}
function addTodo(e) {
const inputText = addInput.value.trim();
if (inputText == null || inputText == "") {
showAlert("warning", "Lütfen boş bırakmayınız!");
} else {
// Arayüze Ekleme
addTodoToUI(inputText);
addTodoToStorage(inputText);
showAlert("success", "Todo Eklendi.");
}
// Storage Ekleme
e.preventDefault();
}
function addTodoToUI(newTodo) {
/*
<li class="list-group-item d-flex justify-content-between">Todo 1
<a href="#" class="delete-item">
<i class="fa fa-remove"></i>
</a>
</li>
*/
const li = document.createElement("li");
li.className = "list-group-item d-flex justify-content-between";
li.textContent = newTodo;
const a = document.createElement("a");
a.href = "#";
a.className = "delete-item";
const i = document.createElement("i");
i.className = "fa fa-remove";
a.appendChild(i);
li.appendChild(a);
todoList.appendChild(li);
addInput.value = "";
}
function addTodoToStorage(newTodo) {
checkTodosFromStorage();
todos.push(newTodo);
localStorage.setItem("todos", JSON.stringify(todos));
}
function checkTodosFromStorage() {
if (localStorage.getItem("todos") === null) {
todos = [];
} else {
todos = JSON.parse(localStorage.getItem("todos"));
}
}
function showAlert(type, message) {
/*
<div class="alert alert-warning" role="alert">
This is a warning alert—check it out!
</div>*/
const div = document.createElement("div");
// div.className="alert alert-"+type;
div.className = `alert alert-${type}`; // Litirel Template
div.textContent = message;
firstCardBody.appendChild(div);
setTimeout(function(){
div.remove();
},2500);
}