-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
280 lines (240 loc) · 9.69 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Selecting elements
const addTask = document.getElementById("add-task")
const taskIcon = document.getElementById("task-icon")
const closeIcon = document.getElementById("close-icon")
const menuIcon = document.getElementById("menu-icon")
const sideBarMobile = document.getElementById("sidebar-mobile")
const emptyTask = document.getElementById("empty-tasks")
const taskInput = document.getElementById('task-input')
const taskList = document.getElementById('task-list')
const addButton = document.getElementById('add-btn')
const form = document.getElementById('form')
const completedTasksBox = document.getElementById('completed-tasks')
const deleteBtn = document.getElementById("delete")
const cancelBtn = document.getElementById("cancel")
const popup = document.getElementById("popup-modal")
const popupText = document.getElementById("popup-text")
menuIcon.addEventListener('click', (e) => {
sideBarMobile.style.visibility = "visible"
})
closeIcon.addEventListener('click', (e) => {
sideBarMobile.style.visibility = "hidden"
})
// Event listener for input focus
taskInput.addEventListener('focus', (e) => {
console.log("Input focused")
if (taskInput.placeholder === "Add a task" && taskIcon.classList.contains('fa-plus')) {
addTask.style.outline = "2px solid rgba(108, 124, 189, 0.41)"
taskInput.placeholder = "Try typing 'Pay utility bills by 6pm Friday'"
taskIcon.className = 'far fa-circle'
addButton.style.visibility = "visible"
}
})
// Event listener for input blur
taskInput.addEventListener('blur', (e) => {
console.log("Input blurred")
addTask.style.outline = "none"
taskInput.placeholder = "Add a task"
taskIcon.className = 'fa-solid fa-plus'
if (editingSignal === -1) {
// If not editing, hide the button after a short delay
setTimeout(() => {
if (!taskInput.value.trim()) {
addButton.style.visibility = "hidden";
}
}, 200); // Delay to allow the click event to fire
}
})
// Array to collect all task object literals
let tasks = []
let editingSignal = -1 // by default, the user is not editing but when the user starts editing it changes the (-1) to the index number of what is being edited
// Handling form data
form.addEventListener('submit', addNewTask)
addButton.addEventListener('click', addNewTask)
function addNewTask(event) {
event.preventDefault()
console.log("Form submitted")
let taskText = taskInput.value.trim()
console.log("Task input value:", taskText)
if (taskText.length === 0) {
alert("Enter a task")
} else if(editingSignal >= 0){
tasks = tasks.map(function(taskObject, index){
if(editingSignal === index){
return{
text : taskText,
date : new Date().toLocaleDateString(),
completed : false
}
}else{
return{
text : taskObject.text,
date : taskObject.date,
completed : taskObject.completed
}
}
})
editingSignal = -1 // Reset the editing signal after update
addButton.innerText = "Add" // Change button text back to 'Add'
}else{
const task = {
text: taskText,
date: new Date().toLocaleString(),
completed: false
};
tasks.push(task)
console.log("Task added:", task)
}
localStorage.setItem("addedTasks", JSON.stringify(tasks))
form.reset()
fetchItems()
// Hide the button after adding a task
addButton.style.visibility = "hidden"
}
// Fetching data from local storage
function fetchItems() {
if (localStorage.getItem("addedTasks")) {
tasks = JSON.parse(localStorage.getItem("addedTasks"))
}
printItemsOnUI()
}
fetchItems()
// Print data from local storage to UI
function printItemsOnUI() {
if (tasks.length === 0) {
taskList.style.display = "none"
emptyTask.style.display = "flex"
} else {
taskList.style.display = "flex"
emptyTask.style.display = "none"
}
taskList.innerHTML = ''
tasks.forEach((taskItem, index) => {
let taskElement = document.createElement('div')
taskElement.classList.add('task')
taskElement.setAttribute(`id`, `${index}`)
let taskContent = document.createElement('div')
taskContent.classList.add('task-content')
let taskState = document.createElement(`div`)
taskState.classList.add('task-state')
let textGroup = document.createElement(`div`)
textGroup.classList.add(`text-group`)
let unCheckedIcon = document.createElement("i")
unCheckedIcon.classList.add("fa-regular", "fa-circle")
unCheckedIcon.setAttribute("data-action", "check") //adds a trigger on the element so it'll be possible to set actions for the element when it is interacted with the user
let checkedIcon = document.createElement("i")
checkedIcon.classList.add("fa-solid", "fa-circle-check")
checkedIcon.setAttribute("data-action", "check")
let tasksTitle = document.createElement(`h3`)
tasksTitle.innerText = `${taskItem.text}`
tasksTitle.setAttribute("data-action", "check")
let tasksDate = document.createElement(`p`)
tasksDate.innerText = `${taskItem.date}`
tasksDate.setAttribute("data-action", "date")
const actions = document.createElement('div')
actions.classList.add('actions')
const editIcon = document.createElement(`i`)
editIcon.classList.add(`fas`, `fa-pen-to-square`)
editIcon.setAttribute("data-action", "edit")
const deleteIcon = document.createElement('i')
deleteIcon.classList.add('fas', 'fa-trash-can')
deleteIcon.setAttribute("data-action", "delete")
// Append
if(!taskItem.completed){
taskState.append(unCheckedIcon)
textGroup.append(tasksTitle)
textGroup.append(tasksDate)
taskContent.append(taskState)
taskContent.append(textGroup)
actions.append(editIcon)
actions.append(deleteIcon)
taskElement.append(taskContent)
taskElement.append(actions)
taskList.append(taskElement)
}else {
taskState.append(checkedIcon)
textGroup.append(tasksTitle)
textGroup.append(tasksDate)
taskContent.append(taskState)
taskContent.append(textGroup)
actions.append(editIcon)
actions.append(deleteIcon)
taskElement.append(taskContent)
taskElement.append(actions)
taskList.append(taskElement)
tasksTitle.style.textDecoration = "line-through"
}
})
}
taskList.addEventListener(`click`, targetTaskItem)
function targetTaskItem(e){ //let the browser know the element that the user clicked on
let userTarget = e.target
let greatGrandParentElement = userTarget.closest(".task") //<div class="task">(The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node that matches the specified CSS selector.)
if(!greatGrandParentElement)return
let taskID = Number(greatGrandParentElement.id) //stores the index of any item that the user interacts with
let clickedAction = userTarget.dataset.action
if(clickedAction === "check"){
checkTaskItem(taskID)
}else if(clickedAction === "edit"){
editTaskItem(taskID)
}else if(clickedAction === "delete"){
console.log(taskID)
// Show confirmation dialog before deleting task
showDeleteConfirmation(taskID)
}
}
function checkTaskItem(ID){
tasks = tasks.map(function(taskObject, index){
if(index === ID){
return{
text : taskObject.text,
date : taskObject.date,
completed : !taskObject.completed
}
}else{
return{
text : taskObject.text,
date : taskObject.date,
completed : taskObject.completed
}
}
})
printItemsOnUI()
}
//Shows the popup for the delete action
function showDeleteConfirmation(taskID) {
const taskToDelete = tasks[taskID]
popup.style.visibility = "visible"; // Show popup
popupText.innerText = `"${taskToDelete.text}" will be permanently deleted.`
deleteBtn.addEventListener("click", function() {
deleteTaskItem(taskID); // Call deleteTaskItem here
popup.style.visibility = "hidden"
},{once: true})
cancelBtn.addEventListener("click", function() {
popup.style.visibility = "hidden"; // Hide popup
},{once: true});
}
// Delete tasks from array and update UI and local storage
function deleteTaskItem(ID){
// Create a new array that excludes the task with the matching taskID
tasks = tasks.filter(function(task, index) { //filter is like the foreach and map method
return index !== ID;
});
// Update local storage with the new array of tasks
localStorage.setItem("addedTasks", JSON.stringify(tasks));
// Refresh the task list by calling printItemsOnUI
printItemsOnUI();
}
// Edit tasks from array and update UI and local storage
function editTaskItem(ID){
// Set the input field with the task text to be edited
taskInput.value = tasks[ID].text
// Change the editing signal to the current task's ID
editingSignal = ID
// Focus on the input field
taskInput.focus()
// Change the add button text to "Update"
addButton.innerText = "Update"
// Ensure the add button is visible
addButton.style.visibility = "visible"
}