-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (129 loc) · 6.15 KB
/
script.js
File metadata and controls
162 lines (129 loc) · 6.15 KB
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
//* ======================================================
//* TODO APP
//* ======================================================
//?Selectors
const addBtn = document.getElementById("todo-button");
const todoInput = document.getElementById("todo-input");
const todoUl = document.getElementById("todo-ul");
// let todos = []; //*local storage verilerini JSON ile tutmak için boş array oluşturuldu. fakat verilerin güncellenmesi için değiştirildi
// todos dizisini localStorage'daki veriler ile guncelle
//! eger localStroge'de todos adinda bir item bulunmaz ise bos array atamasi yap.
let todos = JSON.parse(localStorage.getItem("TODOS")) || [];
const renderSavedTodos = () => {
todos.forEach((todo) => {
createListElement(todo);
});
};
renderSavedTodos();
//* bunun içinde kullanılan createListElement(todo) fonksiyonu aşağıda arrow ile tanımlanmıştı, hata vereceğinden dolayı hoisting yapmak için aşağıdaki fonksiyon expression yöntemine çevrildi.
addBtn.addEventListener("click", () => {
//* trim ile başı ve sonundaki boşlukları sildi. böylece sadece boşluk bırakılması kabul edilmeyecek.
if (todoInput.value.trim() === "") {
alert("PLease enter new todo");
} else {
//* new toDo ile JSON formatında bilgiler saklandı.
const newTodo = {
//* getTime() fonksiyonu ile uniq id değeri oluşturmak bir yöntem. mevcut saatin milisecond cinsinden değerini verir.
id: new Date().getTime(),
completed: false,
text: todoInput.value,
};
//! yeni bir li elementi olusturup bunu DOM'a bas
createListElement(newTodo);
//?Yeni olusturulan todo'yu diziye sakla
todos.push(newTodo); //* girilen her bilgi obje formatında dizi içinde saklansın
localStorage.setItem("TODOS", JSON.stringify(todos)); //* her eklemede dizinin tamamı stringe çevrilerek localStorage atılır.
console.log(todos);
todoInput.value = "";
}
});
//* li'leri innerHTML ile yapabiliriz fakat örnek olsun diye createElement ile yapımını gösterdi.
// hoisting yapmak için aşağıdaki fonksiyon expression yöntemine çevrildi. (ilk başta arrow function ile yazıldı.)
function createListElement(newTodo) {
const { id, completed, text } = newTodo; //todo, destructig yapıldı
//! yeni bir li elementi olustur ve bu elemente obje icerisindeki
//! id degerini ve completed class'ini ata
const li = document.createElement("li");
//* li'lerin id'sini newTodo objesinden çekiyoruz. 2 yöntemle yapılabilir.
// li.id = newTodo.id;
// li.setAttribute("id", newTodo.id);
li.setAttribute("id", id); //todo, destruct olduğundan
// newTodo.completed ? li.classList.add("checked") : null (aynısı)
// newTodo.completed && li.classList.add("checked");
completed && li.classList.add("checked"); //todo, destruct olduğundan
//! okey ikonu olustur ve li elementine bagla
//* li'lerin içinde önce bir check ikonu sonra p elemeti sonra yine trash ikonu var, bunlarıda tek tek oluşturmak gerekiyor.
const okIcon = document.createElement("i");
okIcon.setAttribute("class", "fas fa-check");
li.appendChild(okIcon);
//! todo basligi icin bir p elementi ve yazi dugumu olusturarak li'ye bagla
const p = document.createElement("p");
// const pTextNode = document.createTextNode(newTodo.text);
const pTextNode = document.createTextNode(text); //todo, destruct olduğundan
p.appendChild(pTextNode);
li.appendChild(p);
//! delete ikonu olustur ve li elementine bagla
const deleteIcon = document.createElement("i");
deleteIcon.setAttribute("class", "fas fa-trash");
li.appendChild(deleteIcon);
//! meydana gelen li elementini ul'ye bağla
todoUl.appendChild(li);
}
//* ul içinde capturing yapıldı, ve içindeki her element yakalandı, şimdi yakalanan elementin klasında trash varsa, yakalanan elemanın parentini sil diyoruz, yani li'nin hepsini.
//* eğer yakalanan elementin klasında check varsa, yakalanan elemanın next sibliginine check
//! Ul elementinin cocuklarindan herhangi birisinden bir event gelirse
//! bunu tespit et ve gerekini yap. (Capturing)
todoUl.addEventListener("click", (e) => {
console.log(e.target);
const id = e.target.parentElement.getAttribute("id");
//! event, bir delete butonundan geldi ise
if (e.target.classList.contains("fa-trash")) {
//? delete butonunun parent'ini DOM'dan sil
e.target.parentElement.remove();
//? Dizinin ilgili elementini sil
todos = todos.filter((todo) => todo.id !== Number(id));
//? todos dizisinin son halini localStorage'e sakla
localStorage.setItem("TODOS", JSON.stringify(todos));
} else if (e.target.classList.contains("fa-check")) {
//! event, bir okey butonundan geldi ise
//? ilgili li elementinde checked adinda bir class'i varsa bunu sil
//? aksi takdirde ekle (DOM)
e.target.parentElement.classList.toggle("checked");
//? todos dizisindeki ilgili elementin completed kismini guncelle
todos.map((todo, index) => {
if (todo.id == id) {
todos[index].completed = !todos[index].completed;
}
});
console.log(todos);
//? todos dizisinin son halini localStorage'e sakla
localStorage.setItem("TODOS", JSON.stringify(todos));
}
});
//! Enter tusu ile ekleme mumkun olsun
todoInput.addEventListener("keydown", (e) => {
if (e.code === "Enter") {
addBtn.click();
}
});
//! Baslangicta input aktif olsun
window.onload = function () {
todoInput.focus();
};
//! ÖDEC: check yapılan (TAMAMLANAN) değişiklik localstorage da işlensin ve dizi güncellensin.
//! 117-126 ARASINA EKLENDİ
// } else if (e.target.classList.contains("fa-check")) {
// bunun altına
//? Dizinin ilgili elementini sil
//* todos = todos.filter((todo) => todo.id !== Number(id));
// böyle bir işlem yapılacak, sonra
//? todos dizisinin son halini localStorage'e sakla
//* localStorage.setItem("TODOS", JSON.stringify(todos));
// bunun aynısı yazılacak (105-109 arasını incele)
//? grup olark biz böyle çözmüştük;
// let todosChecked = todos.filter((todo) => todo.id === Number(id));
// if (todosChecked[0].completed == true) {
// todosChecked[0]["completed"] = false;
// } else {
// todosChecked[0]["completed"] = true;
// }