-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_class.js
97 lines (82 loc) · 2.73 KB
/
index_class.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
// TODO
const todoList = [];
class TodoList {
constructor(listElementParam) {
this.todoListElement = listElementParam;
}
idCreater(){
let randomId;
do {
randomId = Math.floor(Math.random() * 10000);
} while (todoList.includes(randomId));
return randomId;
}
add(todoText) {
const todoObject = {
// id: todoList.length * 5,
// id: todoList.length,
id: this.idCreater(),
todoText: todoText,
isDone: false,
index: todoList.length,
};
todoList.push(todoObject);
this.display();
}
deleteObject(todoId){
//delete todoList[todoId];
const selectedTodoIndex= todoList.findIndex(item=>item.id==todoId)
todoList.splice(selectedTodoIndex,1);
this.display();
}
done(todoId) {
const selectedTodoIndex = todoList.findIndex((item) => item.id == todoId);
todoList[selectedTodoIndex].isDone = !todoList[selectedTodoIndex].isDone;
this.display();
}
display() {
this.todoListElement.innerHTML = "";
todoList.forEach((item) => {
const listElement = document.createElement("li");
listElement.innerText = item.todoText;
listElement.setAttribute("data-id", item.id);
// listElement.style.display="inline";
const divElement = document.createElement("div");
const listImgElement = document.createElement("img");
listImgElement.setAttribute("src","./image/dustbin.png");
listImgElement.classList.add("dustbin");
listImgElement.setAttribute("data-id",item.id);
listImgElement.addEventListener("click",e=>{
const selectedId = e.target.getAttribute("data-id");
this.deleteObject(selectedId);
})
if (item.isDone) {
listElement.classList.add("checked");
}
listElement.addEventListener("click", function (e) {
const selectedId = e.target.getAttribute("data-id");
myTodoList.done(selectedId);
});
divElement.appendChild(listElement);
divElement.appendChild(listImgElement);
this.todoListElement.appendChild(divElement);
});
}
}
const listSection = document.querySelector("#myUL");
const secondList = document.querySelector("#severekAyrilanlar");
const myTodoList = new TodoList(listSection);
const inputElement=document.querySelector("#myInput");
inputElement.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.querySelector("#todo_button").click();
}
});
document.querySelector("#todo_button").addEventListener("click", function () {
const todoText = inputElement.value;
if(todoText=="") alert("Please write something to do");
else myTodoList.add(todoText);
inputElement.value="";
inputElement.focus();
});