-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathscript.js
More file actions
439 lines (377 loc) · 14.5 KB
/
script.js
File metadata and controls
439 lines (377 loc) · 14.5 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Redirect to login if not logged in
const currentUser = localStorage.getItem("currentUser");
if (!currentUser) {
window.location.href = "login.html";
}
// DOM Elements
const addBtn = document.getElementById("addBtn");
const activeList = document.getElementById("activeList");
const completedList = document.getElementById("completedList");
const inputField = document.getElementById("todoInput");
const categoryInput = document.getElementById("categoryInput");
const priorityInput = document.getElementById("priorityInput");
const deadlineInput = document.getElementById("deadlineInput");
const hoursInput = document.getElementById("hoursInput");
const minutesInput = document.getElementById("minutesInput");
const secondsInput = document.getElementById("secondsInput");
const searchInput = document.getElementById("searchInput");
const categoryFilter = document.getElementById("categoryFilter");
const priorityFilter = document.getElementById("priorityFilter");
const logoutBtn = document.getElementById("logoutBtn");
let activeTimers = {};
let currentTasks = []; // Single Source of Truth
// Logout functionality
logoutBtn.addEventListener("click", () => {
localStorage.removeItem("currentUser");
window.location.href = "login.html";
});
// Notifications
if ("Notification" in window && Notification.permission !== "granted") {
Notification.requestPermission();
}
function sendNotification(title, body) {
if (Notification.permission === "granted") {
new Notification(title, { body });
}
}
// Populate timer dropdowns
function populateTimeDropdowns() {
hoursInput.innerHTML = "";
minutesInput.innerHTML = "";
secondsInput.innerHTML = "";
for (let i = 0; i < 24; i++) {
const option = document.createElement("option");
option.value = i;
option.innerText = i.toString().padStart(2, "0") + "h";
hoursInput.appendChild(option);
}
for (let i = 0; i < 60; i++) {
const minOption = document.createElement("option");
minOption.value = i;
minOption.innerText = i.toString().padStart(2, "0") + "m";
minutesInput.appendChild(minOption);
const secOption = document.createElement("option");
secOption.value = i;
secOption.innerText = i.toString().padStart(2, "0") + "s";
secondsInput.appendChild(secOption);
}
}
// Save tasks per user
function saveTasks() {
const allUsersTasks = JSON.parse(localStorage.getItem("userTasks")) || {};
allUsersTasks[currentUser] = currentTasks;
localStorage.setItem("userTasks", JSON.stringify(allUsersTasks));
}
// Load tasks per user
function loadTasks() {
const allUsersTasks = JSON.parse(localStorage.getItem("userTasks")) || {};
currentTasks = allUsersTasks[currentUser] || [];
renderList(currentTasks);
}
// Render the task list
function renderList(tasksToRender) {
// Clear existing timers to prevent leaks
Object.values(activeTimers).forEach(clearInterval);
activeTimers = {};
activeList.innerHTML = "";
completedList.innerHTML = "";
tasksToRender.forEach((task) => {
const li = createTodoItem(
task.text,
task.category,
task.priority,
task.deadline,
task.duration || "0",
task.id,
task.timeLeft,
task.timerEndsAt
);
if (task.completed) {
li.classList.add("completed");
li.querySelector('input[type="checkbox"]').checked = true;
completedList.appendChild(li);
} else {
activeList.appendChild(li);
}
});
}
// Animation for moving tasks between lists
function moveWithAnimation(item, targetList) {
item.classList.add("task-exit");
requestAnimationFrame(() => {
item.classList.add("task-exit-active");
});
setTimeout(() => {
item.remove();
targetList.appendChild(item);
item.classList.remove("task-exit", "task-exit-active");
item.classList.add("task-enter");
requestAnimationFrame(() => {
item.classList.add("task-enter-active");
});
setTimeout(() => {
item.classList.remove("task-enter", "task-enter-active");
}, 250);
// Note: currentTasks is already updated before calling moveWithAnimation
saveTasks();
}, 250);
}
// Create a new todo item
function createTodoItem(text, category, priority, deadline, duration, id, timeLeft, timerEndsAt = null) {
id = id || Date.now().toString();
duration = duration || "0";
timeLeft = timeLeft || duration;
const li = document.createElement("li");
li.className = "todo-item";
li.dataset.id = id;
li.dataset.category = category;
li.dataset.priority = priority;
if (deadline) li.dataset.deadline = deadline;
li.dataset.duration = duration;
li.dataset.timeLeft = timeLeft;
li.dataset.timerEndsAt = timerEndsAt || "";
const textSpan = document.createElement("span");
textSpan.className = "todo-text";
textSpan.innerText = text;
const details = document.createElement("div");
details.className = "todo-details";
details.innerHTML = `<span class="category">${category}</span><span class="priority">${priority}</span>`;
const deadlineSpan = document.createElement("div");
deadlineSpan.className = "deadline";
if (deadline) deadlineSpan.innerText = "Due: " + new Date(deadline).toLocaleString();
// Timer section
const durationNum = parseInt(duration);
if (durationNum > 0) {
const timerContainer = document.createElement("div");
timerContainer.className = "timer-container";
const timerDisplay = document.createElement("span");
timerDisplay.className = "timer-display";
let remaining = parseInt(li.dataset.timeLeft);
function updateDisplay() {
const hours = Math.floor(remaining / 3600);
const minutes = Math.floor((remaining % 3600) / 60);
const seconds = remaining % 60;
timerDisplay.innerText =
hours.toString().padStart(2, "0") + ":" +
minutes.toString().padStart(2, "0") + ":" +
seconds.toString().padStart(2, "0");
}
updateDisplay();
const startBtn = document.createElement("button");
startBtn.innerText = "▶";
startBtn.className = "timer-btn";
const pauseBtn = document.createElement("button");
pauseBtn.innerText = "⏸";
pauseBtn.className = "timer-btn";
pauseBtn.style.display = "none";
const resetBtn = document.createElement("button");
resetBtn.innerText = "🔄";
resetBtn.className = "timer-btn";
resetBtn.style.display = "none";
timerContainer.append(startBtn, pauseBtn, resetBtn, timerDisplay);
li.appendChild(timerContainer);
function stopTimer() {
clearInterval(activeTimers[id]);
delete activeTimers[id];
}
function startTimerLogic() {
if (activeTimers[id]) return;
if (!li.dataset.timerEndsAt || parseInt(li.dataset.timerEndsAt) <= Date.now()) {
li.dataset.timerEndsAt = Date.now() + (remaining * 1000);
// Update array
const task = currentTasks.find(t => t.id === id);
if (task) task.timerEndsAt = li.dataset.timerEndsAt;
saveTasks();
}
activeTimers[id] = setInterval(() => {
const endsAt = parseInt(li.dataset.timerEndsAt);
remaining = Math.floor((endsAt - Date.now()) / 1000);
if (remaining > 0) {
li.dataset.timeLeft = remaining;
const task = currentTasks.find(t => t.id === id);
if (task) task.timeLeft = remaining;
updateDisplay();
} else {
remaining = 0;
li.dataset.timeLeft = 0;
updateDisplay();
stopTimer();
li.dataset.timerEndsAt = "";
const task = currentTasks.find(t => t.id === id);
if (task) {
task.timeLeft = 0;
task.timerEndsAt = "";
}
saveTasks();
sendNotification("Timer Finished!", text);
startBtn.style.display = "inline-block";
pauseBtn.style.display = "none";
}
}, 1000);
startBtn.style.display = "none";
pauseBtn.style.display = "inline-block";
resetBtn.style.display = "inline-block";
}
startBtn.addEventListener("click", startTimerLogic);
pauseBtn.addEventListener("click", () => {
stopTimer();
li.dataset.timerEndsAt = "";
li.dataset.timeLeft = remaining;
const task = currentTasks.find(t => t.id === id);
if (task) {
task.timerEndsAt = "";
task.timeLeft = remaining;
}
saveTasks();
startBtn.style.display = "inline-block";
pauseBtn.style.display = "none";
});
resetBtn.addEventListener("click", () => {
stopTimer();
remaining = parseInt(duration);
li.dataset.timeLeft = remaining;
li.dataset.timerEndsAt = "";
const task = currentTasks.find(t => t.id === id);
if (task) {
task.timeLeft = remaining;
task.timerEndsAt = "";
}
updateDisplay();
saveTasks();
startBtn.style.display = "inline-block";
pauseBtn.style.display = "none";
resetBtn.style.display = "none";
});
if (timerEndsAt && parseInt(timerEndsAt) > Date.now()) {
remaining = Math.floor((parseInt(timerEndsAt) - Date.now()) / 1000);
startTimerLogic();
} else if (timerEndsAt && parseInt(timerEndsAt) <= Date.now()) {
remaining = 0;
li.dataset.timeLeft = 0;
li.dataset.timerEndsAt = "";
updateDisplay();
}
}
// Buttons section
const buttons = document.createElement("div");
buttons.className = "button-container";
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.addEventListener("change", function () {
const task = currentTasks.find(t => t.id === id);
if (task) task.completed = checkbox.checked;
if (checkbox.checked) {
li.classList.add("completed");
if (activeTimers[id]) {
clearInterval(activeTimers[id]);
delete activeTimers[id];
li.dataset.timerEndsAt = "";
if (task) task.timerEndsAt = "";
}
moveWithAnimation(li, completedList);
} else {
li.classList.remove("completed");
moveWithAnimation(li, activeList);
}
});
const deleteBtn = document.createElement("button");
deleteBtn.className = "delete-btn";
deleteBtn.innerText = "Delete";
deleteBtn.addEventListener("click", () => {
if (activeTimers[id]) {
clearInterval(activeTimers[id]);
delete activeTimers[id];
}
currentTasks = currentTasks.filter(t => t.id !== id);
li.remove();
saveTasks();
});
buttons.appendChild(checkbox);
buttons.appendChild(deleteBtn);
li.appendChild(textSpan);
li.appendChild(details);
if (deadline) li.appendChild(deadlineSpan);
li.appendChild(buttons);
checkDeadline(li);
return li;
}
// Add new task
addBtn.addEventListener("click", () => {
const text = inputField.value.trim();
if (!text) return;
const hours = parseInt(hoursInput.value) || 0;
const minutes = parseInt(minutesInput.value) || 0;
const seconds = parseInt(secondsInput.value) || 0;
const duration = (hours * 3600 + minutes * 60 + seconds).toString();
const id = Date.now().toString();
const newTask = {
id,
text,
category: categoryInput.value,
priority: priorityInput.value,
deadline: deadlineInput.value,
duration,
timeLeft: duration,
timerEndsAt: null,
completed: false
};
currentTasks.push(newTask);
const li = createTodoItem(
newTask.text,
newTask.category,
newTask.priority,
newTask.deadline,
newTask.duration,
newTask.id,
newTask.timeLeft
);
activeList.appendChild(li);
inputField.value = "";
categoryInput.value = "Personal";
priorityInput.value = "Low";
deadlineInput.value = "";
hoursInput.value = "0";
minutesInput.value = "0";
secondsInput.value = "0";
saveTasks();
});
// Check deadlines
function checkDeadline(li) {
if (!li.dataset.deadline || li.classList.contains("completed")) return;
const now = new Date();
const deadline = new Date(li.dataset.deadline);
const diff = deadline - now;
li.classList.remove("near-deadline", "overdue");
if (diff <= 0) {
li.classList.add("overdue");
sendNotification("Task Overdue!", li.querySelector(".todo-text").innerText);
} else if (diff <= 3600000) {
li.classList.add("near-deadline");
sendNotification("Task Due Soon!", li.querySelector(".todo-text").innerText);
}
}
// Periodically check deadlines
setInterval(() => {
document.querySelectorAll("#activeList .todo-item").forEach(checkDeadline);
}, 30000);
// Optimized filter function using in-memory array
function combinedFilter() {
const searchTerm = searchInput.value.toLowerCase();
const selectedCategory = categoryFilter.value;
const selectedPriority = priorityFilter.value;
const filteredTasks = currentTasks.filter(task => {
const searchMatch = task.text.toLowerCase().includes(searchTerm);
const categoryMatch = selectedCategory === "All" || task.category === selectedCategory;
const priorityMatch = selectedPriority === "All" || task.priority === selectedPriority;
return searchMatch && categoryMatch && priorityMatch;
});
renderList(filteredTasks);
}
// Load tasks and attach filter listeners
document.addEventListener("DOMContentLoaded", function () {
populateTimeDropdowns();
loadTasks();
searchInput.addEventListener("input", combinedFilter);
categoryFilter.addEventListener("change", combinedFilter);
priorityFilter.addEventListener("change", combinedFilter);
});