-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (81 loc) · 2.55 KB
/
app.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
const clear = document.querySelector(".clear");
const dateElement = document.getElementById("date");
const list = doucment.getElementById("list");
const input = document.getElementById("input");
const CHECK= "fa-check-circle";
const UNCHECK = "fa-cricle-thin";
const LINE_THROUGH = "lineThrough";
const options={weekday : "long",month :"short",day : "numeric"};
const today = new Date();
dateElement.innerHTML = today.toLocaleDateString("en-US",options);
function addToDo(toDo, id, done, trash)
{
if(trash){ return; }
const DONE = done ? CHECK : UNCHECK;
const LINE = done ? LINE_THROUGH : "";
let LIST , id;
let data = localStorage.getItem("TODO");
if(data){
LSIT = JSON.parse(data);
id = LIST.length;
loadList(LIST);
}
else{
LIST = [];
id = 0;
}
function loadList(array){
array.forEach(function(item){
addToDo(item.name,item.id,item.done,item.trash);
})
}
const item = `
<li class = "item">
<i class = "fa ${DONE} co" job = "complete" id="${id}"></i>
<p class = "text ${LINE}"> ${toDo} </p>
<i class = "fa fa-trash-o de" job = "delete" id ="${id}"></i>
</li>
`;
const position = "beforeend";
list.insertAdjacentHTML(position, item);
}
document.addEventListener("keyup",function(even){
if(event.keyCode == 13){
const toDo = input.nodeValue;
if(toDo){
addToDo(toDo, id , false , false);
LIST.push({
name : toDo,
id : id,
done : false,
trash : false
});
//Problem
localStorage.setItem("TODO",JSON.stringify(LIST));
}
input.value="";
}
});
function completeToDo(element)
{
element.classList.toggle(CHECK);
element.classList.toggle(UNCHECK);
element.parentNode.querySelector(".text").classList.toggle(LINETHROUGH);
LIST[element.id].done = LIST[element.id].done ? false : true;
}
function removeToDo(element)
{
element.parentNode.parentNode.removeChild(element.parentnode);
LIST[element.id].trash = true;
}
list.addEventListener("click",function(event){
const element = event.target;
const elementjob = element.attributes.job.value;
if(elementjob == "complete"){
completeToDo(element);
}
else if(elementJob == "remove"){
removeToDo(element);
}
localStorage.setItem("TODO",JSON.stringify(LIST));
}