-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
108 lines (97 loc) · 2.86 KB
/
scripts.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
//*********** features ***********
// add task to tasks container
// add task to localStorage
// delete task from tasks container
// delete task from localStorage
// update task status (complete or not ) in UI
// update task status (complete or not ) in localStorage
// get all tasks from localStorage
// display all tasks in UI
let todoInput = document.querySelector(".todo__value");
let addButton = document.querySelector(".add__button");
let tasksList = document.querySelector(".tasks__container");
let arrayOfTasks = getAllTasksFromLocalStorage();
todoInput.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
addButton.click();
}
});
function createTask() {
if (todoInput.value !== "") {
const task = {
id: Date.now(),
title: todoInput.value,
isCompleted: false,
};
arrayOfTasks.push(task);
displayTasksFrom(arrayOfTasks);
addTasksToLocalStorage(arrayOfTasks);
todoInput.value = "";
}
}
function displayTasksFrom(arrayOfTasks) {
tasksList.innerHTML = "";
arrayOfTasks.forEach((task) => {
// create task div
let div = document.createElement("div");
div.className = "task";
div.setAttribute("data-id", task.id);
div.appendChild(document.createTextNode(task.title));
//check if task is done
if (task.isCompleted) {
div.classList = "task done";
}
// create delete button
let span = document.createElement("span");
span.className = "del";
span.appendChild(document.createTextNode("delete"));
//append del to task div
div.appendChild(span);
//add task div to tasks List
tasksList.appendChild(div);
});
}
tasksList.addEventListener("click", (e) => {
//delete button
if (e.target.classList.contains("del")) {
//remove task from local Storage
deleteTaskWith(e.target.parentElement.getAttribute("data-id"));
//remove task from ui
e.target.parentElement.remove();
}
//toggle complete
if (e.target.classList.contains("task")) {
// toggle complete in local storage
toggleStatusTashWith(e.target.getAttribute("data-id"));
// complete effect in ui
e.target.classList.toggle("done");
}
});
function getAllTasksFromLocalStorage() {
let data = window.localStorage.getItem("tasks");
if (data) {
let array = JSON.parse(data);
displayTasksFrom(array);
return array;
}
return [];
}
function addTasksToLocalStorage(arrayOfTasks) {
window.localStorage.setItem("tasks", JSON.stringify(arrayOfTasks));
}
function deleteTaskWith(taskId) {
arrayOfTasks = arrayOfTasks.filter((task) => task.id != taskId);
addTasksToLocalStorage(arrayOfTasks);
}
function toggleStatusTashWith(taskId) {
arrayOfTasks.forEach((task) => {
if (task.id == taskId) {
task.isCompleted = task.isCompleted ? false : true;
}
});
addTasksToLocalStorage(arrayOfTasks);
}
/*
Coded By Nawfel Sekrafi
*/