-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (79 loc) · 2.93 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
// insert current date
const currentDate = new Date().toDateString();
document.getElementById("date").innerHTML = currentDate;
const clearAllList = document.querySelector(".fa-sync-alt");
const listOfElements = document.querySelector(".all-items");
const inputField = document.querySelector(".add-to-list");
const inputBtn = document.querySelector(".fa-plus-circle");
const item = document.querySelectorAll(".item");
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;
// load the list to the user interface
LIST.forEach(function(item){
addToDo(item.task, item.id, item.done, item.trash);
});
} else {
LIST = [];
id = 0;
}
clearAllList.addEventListener("click",function(){
localStorage.clear();
location.reload();
// listOfElements.innerHTML = "";
});
function addToDo(task, id, done, trash){
if(trash){
return;
}
const DONE = done ? "fa-check-circle" : "fa-circle";
const LINE = done ? "lineThrough" : "";
const text = `<li class="item">
<i class="far ${DONE}" id="${id}" job="complete"></i>
<i class="paragraph ${LINE}">${task}</i>
<i class="far fa-trash-alt" id="${id}" job="delete"></i>
</li>`;
listOfElements.insertAdjacentHTML("beforeend",text);
}
inputBtn.addEventListener("click",executa);
// enter key
document.addEventListener("keyup",function(event){
if(event.keyCode == 13){
executa();
}
});
function executa(){
const inputText = inputField.value;
if(inputText !== ""){
addToDo(inputText, id, false, false);
LIST.push({
task: inputText,
id: id,
done: false,
trash: false
});
localStorage.setItem("ToDo",JSON.stringify(LIST)); // (this code must be added where the LIST array is updated)
id++;
}
inputField.value = "";
}
listOfElements.addEventListener("click",function(event){
let element = event.target; // <i class="far fa-trash-alt....." (return the element we clicked inside our listOfElements)
const elementJob = event.target.attributes.job.value; // delete or complete
if(elementJob === "complete"){
element.classList.toggle("fa-circle");
element.classList.toggle("fa-check-circle");
element.parentNode.querySelector(".paragraph").classList.toggle("lineThrough");
//console.log(LIST[element.id]);
LIST[element.id].done = LIST[element.id].done ? false : true;
} else if(elementJob === "delete"){
element.parentNode.parentNode.removeChild(element.parentNode);
LIST[element.id].trash = true;
}
localStorage.setItem("ToDo",JSON.stringify(LIST)); // save ToDo list to localStorage
});
// save ToDo list to localStorage ---->> localStorage.setItem('key','value');