-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
151 lines (118 loc) · 4.08 KB
/
todo.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
const form = document.querySelector("#todo-form");
const addButton = document.querySelector(".btn.btn-danger");
const todoInput = document.querySelector("#todo");
const filter = document.querySelector("#filter");
const clearButton = document.querySelector("#clear-todos");
const todoList = document.querySelector(".list-group");
const firstCard = document.querySelectorAll(".card-body")[0];
const secondCard = document.querySelectorAll(".card-body")[1];
eventListeners();
function eventListeners(){
form.addEventListener("submit",addTodo);
document.addEventListener("DOMContentLoaded",loadAllTodo);
secondCard.addEventListener("click",deleteTodo);
clearButton.addEventListener("click",clearAllTodos);
filter.addEventListener("keyup",filterTodos);
}
function filterTodos(e){
const filterInput = e.target.value.toLowerCase();
const listItems = document.querySelectorAll(".list-group-item");
listItems.forEach(function(listit){
const licont = listit.textContent.toLowerCase();
if(licont.indexOf(filterInput) === -1){
listit.setAttribute("style","display:none !important");
}else {
listit.setAttribute("style","display:block ");
}
});
}
function deleteTodo(e){
if(e.target.className === "fa fa-remove"){
e.target.parentElement.parentElement.remove();
deleteTodoFromStorage(e.target.parentElement.parentElement.textContent);
showAlert("success","Todo Başarıyla silindi.");
}
}
function deleteTodoFromStorage(deletetodo){
let todos = getTodos();
todos.forEach(function(todo,index){
if(todo === deletetodo){
todos.splice(index,1);
}
});
localStorage.setItem("todos",JSON.stringify(todos));
}
function clearAllTodos(e){
if(confirm("Tüm taskları silmek istediğinize emin misiniz?")){
showAlert("info","Tüm todolar siliniyor...")
localStorage.removeItem("todos");
//todoList.innerHTML = "";
while(todoList.firstElementChild!=null){
todoList.firstElementChild.remove();
}
}
e.preventDefault();
}
function loadAllTodo(){
todos = getTodos();
todos.forEach(function(todo){
addTodotoList(todo);
});
}
function addTodo(e){
const newTodo = todoInput.value.trim();
if(newTodo === ""){
showAlert("danger","Lütfen bir todo giriniz.");
}else{
addTodotoList(newTodo);
addTodoToStorage(newTodo);
showAlert("success","Todonuz başarıyla eklendi.");
}
e.preventDefault();
}
function getTodos(){
let todos;
if(localStorage.getItem("todos")===null){
todos = [];
}else {
todos = JSON.parse(localStorage.getItem("todos"));
}
return todos;
}
function addTodoToStorage(newTodo){
let todos = getTodos();
todos.push(newTodo);
localStorage.setItem("todos",JSON.stringify(todos));
}
function showAlert(type,message){
/*
<div class="alert alert-danger" role="alert">
This is a danger alert—check it out!
</div>*/
const alertItem = document.createElement("div");
alertItem.className = `alert alert-${type}`;
alertItem.appendChild(document.createTextNode(message));
firstCard.appendChild(alertItem);
setTimeout(function(){
alertItem.remove();
},1000);
}
function addTodotoList(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>--> */
// List item ekleme
const listItem = document.createElement("li");
const link = document.createElement("a");
link.href = "#";
link.className = "delete-item";
link.innerHTML = '<i class = "fa fa-remove"></i>';
listItem.className = "list-group-item d-flex justify-content-between";
listItem.appendChild(document.createTextNode(newTodo));
listItem.appendChild(link);
todoList.appendChild(listItem);
todoInput.value = "";
}