-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
122 lines (92 loc) · 2.91 KB
/
script.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
const clear = document.querySelector('.clear');
const dateElement = document.querySelector('#date');
const list = document.querySelector('#list');
const input = document.querySelector('#input');
//class names
const CHECK = "fa-check-circle";
const UNCHECK = "fa-circle-thin";
const LineThrough = "lineThrough";
let List,id;
let data = localStorage.getItem('toDo');
console.log(data);
//checking for empty data
if(data){
List = JSON.parse(data);
id= List.length;
loadList(List);
}
else{
List = [];
id=0;
}
//load list function
function loadList(array){
array.forEach( item => {
addTodo(item.name,item.id,item.done,item.trash)
})
}
//clear the list
clear.addEventListener('click', ()=>{
localStorage.clear();
location.reload();
})
//Display date
const options = {weekday: "short", month:"long", day:"numeric"};
const today = new Date();
dateElement.textContent = today.toLocaleDateString("en-us",options);
function addTodo(toDo, id, done, trash){
if (trash){return;}
const DONE = done ? CHECK : UNCHECK;
const LINE = done ? LineThrough: "";
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 pos = "beforeend";
list.insertAdjacentHTML(pos,item);
}
//add an item to list as user press enter key
document.addEventListener("keyup", function(e){
if(e.keyCode === 13){
const toDo = input.value;
//if input isn't empty
if(toDo){
addTodo(toDo,id, false, false);
List.push({
name: toDo,
id: id,
done: false,
trash: false
});
}
localStorage.setItem('toDo', JSON.stringify(List));
console.log(List);
id++;
input.value = "";
}
})
//function complete todo
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;
}
//remove todo
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 === "delete"){
removetoDo(element);
}
localStorage.setItem('toDo', JSON.stringify(List))
})