2019-12-26 12:31:47 +01:00
|
|
|
/* Variables séléction DOM */
|
|
|
|
const addTaskForm = document.getElementById('addTask');
|
|
|
|
const clear = document.querySelector(".clear");
|
|
|
|
const list = document.getElementById("list");
|
|
|
|
const input = document.getElementById("input");
|
|
|
|
const btnAddTask = document.getElementById('btnAddTask');
|
|
|
|
|
|
|
|
/* Variables */
|
|
|
|
const tasksListArray = JSON.parse(localStorage.getItem('tasks')) || [];
|
|
|
|
let id = tasksListArray.length;
|
|
|
|
// Classes CSS
|
|
|
|
const check = "fa-check-circle";
|
|
|
|
const unCheck = "fa-circle";
|
|
|
|
const lineThrough = "lineThrough";
|
|
|
|
|
|
|
|
/* Créer la tâche dans le DOM */
|
|
|
|
function addTask(task, id, done, trash) {
|
|
|
|
if(trash) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-27 00:08:41 +01:00
|
|
|
const item = `<li class="item row" id=${id}>
|
|
|
|
<div class="col-2">
|
|
|
|
<i class="fa ${(done) ? check : unCheck} co" id="complete"></i>
|
|
|
|
</div>
|
|
|
|
<div class="col-8">
|
|
|
|
<p class="text text-center ${(done) ? lineThrough : ""}">${task}</p>
|
|
|
|
</div>
|
|
|
|
<div class="col-2">
|
|
|
|
<i class="fas fa-trash-alt de" id="removeTask"></i>
|
|
|
|
</div>
|
2019-12-26 12:31:47 +01:00
|
|
|
</li>
|
|
|
|
`;
|
|
|
|
list.insertAdjacentHTML("beforeend", item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ajoute un élément à la liste */
|
|
|
|
function addTaskElement(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const taskValue = document.getElementById('taskToAdd').value;
|
|
|
|
addTask(taskValue, id, false, false);
|
|
|
|
tasksListArray.push({
|
|
|
|
name : taskValue,
|
|
|
|
id : id,
|
|
|
|
done : false,
|
|
|
|
trash : false
|
|
|
|
});
|
|
|
|
localStorage.setItem('tasks', JSON.stringify(tasksListArray));
|
|
|
|
id++;
|
|
|
|
addTaskForm.reset();
|
|
|
|
}
|
|
|
|
addTaskForm.addEventListener('submit', (event) => {addTaskElement(event)});
|
|
|
|
btnAddTask.addEventListener('click', (event) => {addTaskElement(event)});
|
|
|
|
|
|
|
|
/* Ajoute un élément à la liste */
|
|
|
|
list.addEventListener('click', (event) => {
|
|
|
|
const element = event.target;
|
|
|
|
const elementJob = element.getAttribute('id');
|
2019-12-27 00:08:41 +01:00
|
|
|
const elementId = element.parentNode.parentNode.getAttribute('id');
|
2019-12-26 12:31:47 +01:00
|
|
|
|
|
|
|
/* Tâche compléter */
|
|
|
|
if(elementJob == "complete") {
|
|
|
|
element.classList.toggle(check);
|
|
|
|
element.classList.toggle(unCheck);
|
2019-12-27 00:08:41 +01:00
|
|
|
element.parentNode.parentNode.querySelector(".text").classList.toggle(lineThrough);
|
2019-12-26 12:31:47 +01:00
|
|
|
tasksListArray[elementId].done = tasksListArray[elementId].done ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Supprime une tâche */
|
|
|
|
else if(elementJob == "removeTask") {
|
2019-12-27 00:08:41 +01:00
|
|
|
element.parentNode.parentNode.remove();
|
2019-12-26 12:31:47 +01:00
|
|
|
tasksListArray[elementId].trash = true;
|
|
|
|
}
|
|
|
|
localStorage.setItem("tasks", JSON.stringify(tasksListArray));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Affiche les tâches enregistrées */
|
|
|
|
tasksListArray.forEach((item) => {
|
|
|
|
addTask(item.name, item.id, item.done, item.trash);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Supprime le localStorage */
|
|
|
|
clear.addEventListener("click", function(){
|
|
|
|
localStorage.clear();
|
|
|
|
location.reload();
|
|
|
|
});
|