-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-crud.js
159 lines (136 loc) · 5.05 KB
/
script-crud.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
// Selecting DOM elements
const btnAddTask = document.querySelector(".app__button--add-task");
const addTaskForm = document.querySelector(".app__form-add-task");
const textarea = document.querySelector(".app__form-textarea");
const tasksList = document.querySelector(".app__section-task-list");
const btnCancel = document.querySelector(".app__form-footer__button--cancel");
const taskDescriptionParagraph = document.querySelector(
".app__section-active-task-description"
);
// Selecting buttons for removing completed tasks and all tasks
const btnRemoveCompleted = document.querySelector("#btn-remove-completed");
const btnRemoveAll = document.querySelector("#btn-remove-all");
// Initializing tasks from localStorage or an empty array
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
let selectedTask = null;
let selectedTaskListItem = null;
// Function to update tasks in localStorage
function updateTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
// Function to create task elements dynamically
function createTaskElement(task) {
const li = document.createElement("li");
li.classList.add("app__section-task-list-item");
const svg = document.createElement("svg");
svg.innerHTML = `
<svg class="app__section-task-icon-status" width="24" height="24" viewBox="0 0 24 24" fill="none"
xmlns="http://www.w3.org/2000/svg">
<circle cx="12" cy="12" r="12" fill="#FFF"></circle>
<path d="M9 16.1719L19.5938 5.57812L21 6.98438L9 18.9844L3.42188 13.4062L4.82812 12L9 16.1719Z"
fill="#01080E"></path>
</svg>
`;
const paragraph = document.createElement("p");
paragraph.textContent = task.description;
paragraph.classList.add("app__section-task-list-item-description");
const button = document.createElement("button");
button.classList.add("app__button-edit");
// Click event to edit task description
button.onclick = () => {
const newDescription = prompt("Qual é o novo nome da tarefa?");
if (newDescription) {
paragraph.textContent = newDescription;
task.description = newDescription;
updateTasks();
}
};
const buttonImage = document.createElement("img");
buttonImage.setAttribute("src", "/images/edit.png");
button.append(buttonImage);
li.append(svg);
li.append(paragraph);
li.append(button);
// Check if task is completed and set appropriate styling
if (task.completed) {
li.classList.add("app__section-task-list-item-complete");
button.setAttribute("disabled", "disabled");
} else {
// Click event to select task and show description
li.onclick = () => {
document
.querySelectorAll(".app__section-task-list-item-active")
.forEach((element) => {
element.classList.remove("app__section-task-list-item-active");
});
if (selectedTask == task) {
taskDescriptionParagraph.textContent = "";
selectedTask = null;
selectedTaskListItem = null;
return;
}
selectedTask = task;
selectedTaskListItem = li;
taskDescriptionParagraph.textContent = task.description;
li.classList.add("app__section-task-list-item-active");
};
}
return li;
}
// Click event to toggle task form visibility
btnAddTask.addEventListener("click", () => {
addTaskForm.classList.toggle("hidden");
});
// Form submission event to add new task
addTaskForm.addEventListener("submit", (event) => {
event.preventDefault();
const task = {
description: textarea.value,
};
tasks.push(task);
const taskElement = createTaskElement(task);
tasksList.append(taskElement);
updateTasks();
textarea.value = "";
addTaskForm.classList.add("hidden");
});
// Loop through tasks and append to the tasks list
tasks.forEach((task) => {
const taskElement = createTaskElement(task);
tasksList.append(taskElement);
});
// Function to clear the task form
const clearForm = () => {
textarea.value = "";
addTaskForm.classList.add("hidden");
};
// Click event to clear the task form
btnCancel.addEventListener("click", clearForm);
// Event listener for when focus on a task finishes
document.addEventListener("FocusFinished", () => {
if (selectedTask && selectedTaskListItem) {
selectedTaskListItem.classList.remove("app__section-task-list-item-active");
selectedTaskListItem.classList.add("app__section-task-list-item-complete");
selectedTaskListItem
.querySelector("button")
.setAttribute("disabled", "disabled");
selectedTask.completed = true;
updateTasks();
}
});
// Function to remove tasks (completed or all)
const removeTasks = (onlyCompleted) => {
let selector = ".app__section-task-list-item";
if (onlyCompleted) {
selector = ".app__section-task-list-item-complete";
}
document.querySelectorAll(selector).forEach((element) => {
element.remove();
});
tasks = onlyCompleted ? tasks.filter((task) => !task.completed) : [];
updateTasks();
};
// Click event to remove completed tasks
btnRemoveCompleted.onclick = () => removeTasks(true);
// Click event to remove all tasks
btnRemoveAll.onclick = () => removeTasks(false);