-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
209 lines (157 loc) · 5.29 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// TODO TASK MANAGER
// SELECTEURS
const button = document.querySelector(`.button`);
const todoList = document.querySelector(`.todo`);
const filterList = document.querySelector(".filter-category");
const allTaskBtn = document.querySelector("#all-task");
const achievedTaskBtn = document.querySelector("#achieved-task");
const standbyTaskBtn = document.querySelector("#standby-task");
//ECOUTEURS
todoList.addEventListener(`click`, checkDelete);
filterList.addEventListener("input", filterCategoryHandler);
allTaskBtn.addEventListener('click', showAllCategory);
achievedTaskBtn.addEventListener('click', showAchievedCategory);
standbyTaskBtn.addEventListener('click', showStandByCategory);
// clear.addEventListener("click", function(){
// location.reload();
// });
button.addEventListener(`click`, (e) => {
e.preventDefault();
const input = document.querySelector(`.input`);
createHtmlTask(input.value);
localStorage.setItem(input.value, 0);
todoCounter();
input.value = ``;
});
//FONCTIONS
function getAllStorage() {
const archive = []
const keys = Object.keys(localStorage)
for (let i = 0; i < keys.length; i++) {
if (keys[i] !== "achievedCount" && keys[i] !== "standbyCount"){
archive.push({taskName: keys[i], state: localStorage.getItem(keys[i])})
}
}
return archive;
}
function createHtmlTask (taskName) {
const listTask = document.createElement("listTask")
listTask.classList.add("task")
const newTask = document.createElement(`li`);
newTask.classList.add(`newTask`)
newTask.innerText = taskName;
listTask.appendChild(newTask);
const confirmButton = document.createElement("button")
confirmButton.innerHTML = '<i class="fas fa-check"></i>'
confirmButton.classList.add('check-btn')
listTask.appendChild(confirmButton)
const deleteButton = document.createElement("button")
deleteButton.innerHTML = '<i class="fas fa-trash"></i>'
deleteButton.classList.add('delete-btn')
listTask.appendChild(deleteButton)
todoList.appendChild(listTask)
};
function checkDelete(event) {
const item = event.target
const task = item.parentElement
if (item.classList.contains("check-btn")) {
task.classList.add("achieved");
todoCounter();
} else if (item.classList.contains("delete-btn")) {
task.classList.add("transighost");
task.addEventListener("transitionend", function () {
task.remove();
todoCounter()
});
}
}
// Fonction de traitement de l'évenement lors du clic sur le select
function filterCategoryHandler(e) {
// Appel de la fonction de filtrage avec la valeur de l'évenement
filterCategory(e.target.value)
}
function showAllCategory(){
filterCategory("all")
}
function showAchievedCategory(){
filterCategory("achieved")
}
function showStandByCategory(){
filterCategory("standby")
}
function filterCategory(value) {
const tasks = todoList.querySelectorAll(".task");
tasks.forEach(function (task) {
switch (value) {
case "all":
task.style.display = "flex";
break;
case "achieved":
if (task.classList.contains("achieved")) {
task.style.display = "flex";
} else {
task.style.display = "none";
}
break;
case "standby":
if (!task.classList.contains("achieved")) {
task.style.display = "flex";
} else {
task.style.display = "none";
}
break;
}
});
}
// MAJ les compteurs dans localStorage
const clearButton = document.getElementById("clear-button");
let achievedCount = 0;
let standbyCount = 0;
function todoCounter() {
const counterAchieved = document.getElementById("counter-achieved");
const counterStandby = document.getElementById("counter-standby");
const tasks = todoList.querySelectorAll(".task");
achievedCount = 0;
standbyCount = 0;
tasks.forEach((task) => {
if (task.classList.contains("achieved")) {
achievedCount++;
} else {
standbyCount++;
}
});
localStorage.setItem("achievedCount", achievedCount);
localStorage.setItem("standbyCount", standbyCount);
counterAchieved.textContent = achievedCount;
counterStandby.textContent = standbyCount;
}
// Ajout du bouton Clear pour refresh les compteurs
clearButton.addEventListener("click", () => {
localStorage.clear();
console.log("toto");
location.reload()
console.log("toto")
});
// chargement de la page
document.addEventListener("DOMContentLoaded", () => {
const counterAchieved = document.getElementById("counter-achieved");
const counterStandby = document.getElementById("counter-standby");
const counters = getCounter();
const data = getAllStorage();
data.forEach((Element) => {
createHtmlTask(Element.taskName)
})
counterAchieved.textContent = counters.achievedCount;
counterStandby.textContent = counters.standbyCount;
});
// pour récupérer les compteurs du localStorage
function getCounter() {
const achievedCount = localStorage.getItem("achievedCount") || 0;
const standbyCount = localStorage.getItem("standbyCount") || 0;
return { achievedCount: parseInt(achievedCount), standbyCount: parseInt(standbyCount) };
}
// ajout de la date
const dateElement = document.getElementById("date");
const options = {weekday : "long", month:"long", day:"numeric"};
const today = new Date();
dateElement.innerHTML = today.toLocaleDateString("fr-FR", options);