-
Notifications
You must be signed in to change notification settings - Fork 0
/
appp.js
145 lines (96 loc) · 3.48 KB
/
appp.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
// Select the Elements
const clear = document.querySelector(".clear");
const dateElement = document.getElementById("date");
const list = document.getElementById("list");
const input = document.getElementById("input");
// Classes names
const CHECK = "fa-check-circle";
const UNCHECK = "fa-circle-thin";
const LINE_THROUGH = "lineThrough";
// Variables
let LIST, id;
// get item from localstorage
let data = localStorage.getItem("TODO");
// check if data is not empty
if(data){
LIST = JSON.parse(data);
id = LIST.length; // set the id to the last one in the list
loadList(LIST); // load the list to the user interface
}else{
// if data isn't empty
LIST = [];
id = 0;
}
// load items to the user's interface
function loadList(array){
array.forEach(function(item){
addToDo(item.name, item.id, item.done, item.trash);
});
}
// clear the local storage
clear.addEventListener("click", function(){
localStorage.clear();
location.reload();
});
// Show todays date
const options = {weekday : "long", month:"short", day:"numeric"};
const today = new Date();
dateElement.innerHTML = today.toLocaleDateString("en-US", options);
// add to do function
function addToDo(toDo, id, done, trash){
if(trash){ return; }
const DONE = done ? CHECK : UNCHECK;
const LINE = done ? LINE_THROUGH : "";
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);
}
// add an item to the list user the enter key
document.addEventListener("keyup",function(even){
if(event.keyCode == 13){
const toDo = input.value;
// if the input isn't empty
if(toDo){
addToDo(toDo, id, false, false);
LIST.push({
name : toDo,
id : id,
done : false,
trash : false
});
// add item to localstorage ( this code must be added where the LIST array is updated)
localStorage.setItem("TODO", JSON.stringify(LIST));
id++;
}
input.value = "";
}
});
// complete to do
function completeToDo(element){
element.classList.toggle(CHECK);
element.classList.toggle(UNCHECK);
element.parentNode.querySelector(".text").classList.toggle(LINE_THROUGH);
LIST[element.id].done = LIST[element.id].done ? false : true;
}
// remove to do
function removeToDo(element){
element.parentNode.parentNode.removeChild(element.parentNode);
LIST[element.id].trash = true;
}
// target the items created dynamically
list.addEventListener("click", function(event){
const element = event.target; // return the clicked element inside list
const elementJob = element.attributes.job.value; // complete or delete
if(elementJob == "complete"){
completeToDo(element);
}else if(elementJob == "delete"){
removeToDo(element);
}
// add item to localstorage ( this code must be added where the LIST array is updated)
localStorage.setItem("TODO", JSON.stringify(LIST));
});