-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (144 loc) · 5.33 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
const inputAddTask = document.getElementById("input-add-task");
const btnAddTask = document.querySelector(".btn-todo");
const taskList = document.querySelector(".task-list");
const errorMessage = document.querySelector(".error-message");
const taskMessage = document.getElementById("task-message");
// Function to save tasks to local storage
const saveTasksToLocalStorage = () => {
const tasks = [];
document.querySelectorAll(".task-list li").forEach((li) => {
const taskText = li.querySelector("span").textContent;
const isDone = li.querySelector("span").classList.contains("done");
tasks.push({ text: taskText, done: isDone });
});
localStorage.setItem("tasks", JSON.stringify(tasks));
};
// Function to load tasks from local storage
const loadTasksFromLocalStorage = () => {
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
tasks.forEach((task) => {
addTaskToList(task.text, task.done);
});
};
// Function to update the task message
const updateTaskListMessage = () => {
const totalTasks = taskList.children.length;
taskMessage.style.display = totalTasks > 0 ? "block" : "none";
taskMessage.textContent = `You have ${totalTasks} task${
totalTasks === 1 ? "" : "s"
}.`;
};
// Function to add task to the list
const addTaskToList = (taskText, isDone = false) => {
const listItem = document.createElement("li");
const taskSpan = document.createElement("span");
taskSpan.textContent = taskText;
if (isDone) {
taskSpan.classList.add("done");
}
listItem.appendChild(taskSpan);
// Create a div to hold the buttons
const buttonContainer = document.createElement("div");
buttonContainer.classList.add("button-container");
// Create Done/Undo button with icon
const doneBtn = document.createElement("button");
doneBtn.classList.add("done-btn");
doneBtn.innerHTML = isDone
? '<i class="fa-solid fa-hourglass"></i>'
: '<i class="fa-solid fa-circle-check"></i>';
doneBtn.addEventListener("click", () => {
taskSpan.classList.toggle("done");
doneBtn.innerHTML = taskSpan.classList.contains("done")
? '<i class="fa-solid fa-hourglass"></i>'
: '<i class="fa-solid fa-circle-check"></i>';
saveTasksToLocalStorage();
});
buttonContainer.appendChild(doneBtn);
// Create Remove button with icon
const removeBtn = document.createElement("button");
removeBtn.classList.add("remove-btn");
removeBtn.innerHTML = '<i class="fa-solid fa-trash"></i>';
removeBtn.addEventListener("click", () => {
taskList.removeChild(listItem);
updateTaskListMessage();
saveTasksToLocalStorage();
});
buttonContainer.appendChild(removeBtn);
// Create Edit button with icon
const editBtn = document.createElement("button");
editBtn.classList.add("edit-btn");
editBtn.innerHTML = '<i class="fa-solid fa-pen-to-square"></i>';
editBtn.addEventListener("click", () => {
inputAddTask.value = taskSpan.textContent;
taskList.removeChild(listItem);
updateTaskListMessage();
saveTasksToLocalStorage();
});
buttonContainer.appendChild(editBtn);
// Append button container to the list item
listItem.appendChild(buttonContainer);
// Set flex properties
listItem.style.display = "flex";
listItem.style.justifyContent = "space-between";
listItem.style.alignItems = "center";
taskList.appendChild(listItem);
updateTaskListMessage();
};
// Event listener for adding a task
btnAddTask.addEventListener("click", () => {
const taskText = inputAddTask.value.trim();
if (taskText === "") {
errorMessage.textContent = "Please enter a task.";
return;
}
errorMessage.textContent = "";
addTaskToList(taskText);
inputAddTask.value = ""; // Clear input after adding
saveTasksToLocalStorage();
});
// Load tasks from local storage on page load
document.addEventListener("DOMContentLoaded", loadTasksFromLocalStorage);
//--------------------------
// Modal Box and Search Functionality
//--------------------------
const searchModal = document.getElementById("search-modal");
const searchIcon = document.getElementById("search-icon");
const closeModal = document.querySelector(".close");
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-btn");
const searchResults = document.getElementById("search-results");
// Open search modal
searchIcon.addEventListener("click", () => {
searchInput.value = "";
searchResults.innerHTML = "<li>No tasks found</li>";
searchModal.style.display = "block";
});
// Close search modal
closeModal.addEventListener("click", () => {
searchModal.style.display = "none";
});
// Close modal when clicking outside
window.addEventListener("click", (event) => {
if (event.target === searchModal) {
searchModal.style.display = "none";
}
});
// Search functionality
searchBtn.addEventListener("click", () => {
const query = searchInput.value.toLowerCase();
searchResults.innerHTML = "";
const tasks = Array.from(taskList.children);
let tasksFound = false;
tasks.forEach((task) => {
const taskText = task.querySelector("span").textContent.toLowerCase();
if (taskText.includes(query)) {
const resultItem = document.createElement("li");
resultItem.textContent = task.querySelector("span").textContent;
searchResults.appendChild(resultItem);
tasksFound = true;
}
});
if (!tasksFound) {
searchResults.innerHTML = "<li>No tasks found</li>";
}
});