-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (187 loc) · 6.13 KB
/
script.js
File metadata and controls
225 lines (187 loc) · 6.13 KB
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
// TASK FORM LOGIC
const inputForm = document.querySelector("#task-input");
const taskPushButton = document.querySelector(".add-task");
const taskContainer = document.querySelector(".task-list-content");
// MODAL LOGIC
const quickEditModal = document.querySelector(".quick-edit-modal");
const closeIcon = quickEditModal?.querySelector(".bi-x-octagon");
const editForm = quickEditModal?.querySelector("#edit-task-form");
const editInput = quickEditModal?.querySelector("#edit-task-input");
let currentlyEditingId = null;
// Load saved tasks on page load
window.addEventListener("load", () => {
const savedTasks = JSON.parse(localStorage.getItem("tasks") || "[]");
savedTasks.forEach((task) => createTaskElement(task.id, task.text));
});
// Add new task
taskPushButton.addEventListener("click", (e) => {
e.preventDefault();
const text = inputForm.value.trim();
if (!text) {
inputForm.classList.add("error");
alert("Please enter a task");
return;
}
inputForm.classList.remove("error");
const id = Date.now().toString();
createTaskElement(id, text);
saveTaskToStorage(id, text);
inputForm.value = "";
});
// Handle task container clicks (edit/delete)
taskContainer.addEventListener("click", (e) => {
const parentTask = e.target.closest(".task");
if (!parentTask) return;
// Delete task
if (e.target.closest(".delete-icon")) {
const list = parentTask.querySelector(".list");
if (list?.id) {
deleteTaskFromStorage(list.id);
parentTask.remove();
}
return;
}
// Edit task
if (e.target.closest(".edit-icon")) {
const list = parentTask.querySelector(".list");
if (!list?.id) return;
currentlyEditingId = list.id;
if (editInput && quickEditModal) {
editInput.value = list.querySelector(".task-text").textContent.trim();
quickEditModal.style.display = "block";
}
}
});
// Close modal
closeIcon?.addEventListener("click", () => {
if (quickEditModal) {
quickEditModal.style.display = "none";
currentlyEditingId = null;
}
});
// Save edited task
editForm?.addEventListener("submit", (e) => {
e.preventDefault();
if (!currentlyEditingId || !editInput) return;
const newText = editInput.value.trim();
if (!newText) {
alert("Task cannot be empty");
return;
}
const list = document.getElementById(currentlyEditingId);
if (list) {
const taskText = list.querySelector(".task-text");
if (taskText) {
taskText.textContent = newText;
updateTaskInStorage(currentlyEditingId, newText);
}
}
if (quickEditModal) {
quickEditModal.style.display = "none";
currentlyEditingId = null;
}
});
// Create task DOM element
function createTaskElement(id, text) {
const wrapper = document.createElement("div");
wrapper.classList.add("task");
wrapper.innerHTML = `
<div class="list" id="${id}">
<span class="task-text">${escapeHtml(text)}</span>
<div class="actions">
<i class="bi bi-pencil-square edit-icon"></i>
<i class="bi bi-trash delete-icon"></i>
</div>
</div>
`;
taskContainer.appendChild(wrapper);
}
// Storage helpers
function saveTaskToStorage(id, text) {
const tasks = JSON.parse(localStorage.getItem("tasks") || "[]");
tasks.push({ id, text });
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function updateTaskInStorage(id, newText) {
const tasks = JSON.parse(localStorage.getItem("tasks") || "[]");
const updatedTasks = tasks.map((task) =>
task.id === id ? { ...task, text: newText } : task
);
localStorage.setItem("tasks", JSON.stringify(updatedTasks));
}
function deleteTaskFromStorage(id) {
const tasks = JSON.parse(localStorage.getItem("tasks") || "[]");
const filteredTasks = tasks.filter((task) => task.id !== id);
localStorage.setItem("tasks", JSON.stringify(filteredTasks));
}
// Escape HTML to prevent injection
function escapeHtml(str) {
const htmlEntities = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
return str.replace(/[&<>"']/g, (match) => htmlEntities[match]);
}
// Toggle task list visibility
document.querySelector(".task-list-toggle").addEventListener("click", () => {
document.querySelector(".task-list-container").classList.toggle("active");
});
// Background Particle Animation
const container = document.getElementById("particles-container");
let totalParticles = 75;
if (window.innerWidth <= 576) {
totalParticles = 30;
}
// all particles
for (let i = 0; i < totalParticles; i++) {
const p = makeParticle();
container.appendChild(p);
animate(p);
}
// Single particle element
function makeParticle() {
const p = document.createElement("div");
p.className = "particle";
// Random size 3–8px
const size = rand(3, 8);
p.style.width = `${size}px`;
p.style.height = `${size}px`;
// Random bright neon color
const colors = ["#00C9FF", "#92FE9D", "#FF6A88", "#FFD93D", "#845EC2"];
p.style.background = colors[Math.floor(rand(0, colors.length))];
// Glow effect
p.style.borderRadius = "50%";
p.style.boxShadow = `0 0 10px ${p.style.background}, 0 0 20px ${p.style.background}`;
setRandomPosition(p);
return p;
}
// particle to a random position + hide
function setRandomPosition(p) {
p.style.left = `${rand(0, 100)}%`;
p.style.top = `${rand(0, 100)}%`;
p.style.opacity = "0";
}
// Particle movement
function animate(p) {
setRandomPosition(p);
const duration = rand(6, 12); // faster moves
const delay = rand(0, 3);
setTimeout(() => {
p.style.transition = `all ${duration}s ease-in-out`;
p.style.opacity = rand(0.4, 1);
// Wider movement range
const moveX = parseFloat(p.style.left) + rand(-30, 30);
const moveY = parseFloat(p.style.top) - rand(10, 40);
p.style.left = `${moveX}%`;
p.style.top = `${moveY}%`;
// repeat after finished
setTimeout(() => animate(p), duration * 1000);
}, delay * 1000);
}
// random float between min and max
function rand(min, max) {
return Math.random() * (max - min) + min;
}