-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
342 lines (229 loc) · 8.12 KB
/
Copy pathscript.js
File metadata and controls
342 lines (229 loc) · 8.12 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
// ------------------------------------------------------
// Utility: Escape HTML (Prevent XSS)
// ------------------------------------------------------
function escapeHTML(text) {
const div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
// ------------------------------------------------------
// DOM Elements
// ------------------------------------------------------
const taskInput = document.getElementById("taskInput");
const energyTag = document.getElementById("energyTag");
const addTaskBtn = document.getElementById("addTaskBtn");
const taskList = document.getElementById("taskList");
const progressBar = document.getElementById("progress");
const privacyToggle = document.getElementById("privacyToggle");
// ------------------------------------------------------
// State
// ------------------------------------------------------
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
let currentFilter = "all";
let incognitoMode = false;
// ------------------------------------------------------
// Event Listeners
// ------------------------------------------------------
addTaskBtn.addEventListener("click", addTask);
taskInput.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
event.preventDefault();
addTask();
}
});
// ------------------------------------------------------
// Function: Add Task
// ------------------------------------------------------
function addTask() {
const text = escapeHTML(taskInput.value.trim());
const energy = energyTag.value;
if (text === "") {
showToast("Task cannot be empty ❌");
return;
}
if (text.length > 100) {
showToast("Max 100 characters allowed ⚠️");
return;
}
const task = {
id: Date.now(),
text,
energy,
completed: false
};
tasks.push(task);
saveTasks();
taskInput.value = "";
renderTasks();
// Notifications
sendNotification("New task added!");
showToast("Task added ✅");
}
// ------------------------------------------------------
// Function: Render Tasks
// ------------------------------------------------------
function renderTasks() {
taskList.innerHTML = "";
let filteredTasks = tasks;
if (currentFilter === "active") {
filteredTasks = tasks.filter(t => !t.completed);
}
if (currentFilter === "completed") {
filteredTasks = tasks.filter(t => t.completed);
}
if (currentFilter === "quick") {
filteredTasks = tasks.filter(t => t.energy === "quick");
}
if (currentFilter === "deep") {
filteredTasks = tasks.filter(t => t.energy === "deep");
}
// Empty state
if (filteredTasks.length === 0) {
const li = document.createElement("li");
li.className = "empty-state";
li.innerHTML = `
<span>No tasks yet ✏️</span>
<small>Add something to get started</small>
`;
taskList.appendChild(li);
updateProgress();
updateDashboard();
return;
}
filteredTasks.forEach(task => {
const li = document.createElement("li");
li.className = task.completed ? "completed" : "";
li.innerHTML = `
<span>${task.text} (${task.energy})</span>
<div class="task-actions">
<button onclick="toggleTask(${task.id}, this)">✔</button>
<button onclick="deleteTask(${task.id})">🗑</button>
</div>
`;
taskList.appendChild(li);
});
updateProgress();
updateDashboard();
}
// ------------------------------------------------------
// Function: Toggle Task
// ------------------------------------------------------
function toggleTask(id, btn) {
const li = btn.closest("li");
li.classList.add("burn");
setTimeout(() => {
tasks = tasks.map(task => {
if (task.id === id) task.completed = !task.completed;
return task;
});
saveTasks();
renderTasks();
showToast("Task completed 🎉");
sendNotification("Task completed!");
if (tasks.length > 0 && tasks.every(t => t.completed)) {
showToast("All tasks completed 🚀");
sendNotification("All tasks completed 🎉");
}
}, 400);
}
// ------------------------------------------------------
// Function: Delete Task
// ------------------------------------------------------
function deleteTask(id) {
tasks = tasks.filter(t => t.id !== id);
saveTasks();
renderTasks();
showToast("Task deleted 🗑");
}
// ------------------------------------------------------
// Save Tasks
// ------------------------------------------------------
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
// ------------------------------------------------------
// Progress Bar
// ------------------------------------------------------
function updateProgress() {
const progressText = document.getElementById("progressText");
const total = tasks.length;
const completed = tasks.filter(t => t.completed).length;
const percent = total === 0 ? 0 : Math.round((completed / total) * 100);
progressBar.style.width = percent + "%";
if (progressText) {
progressText.textContent = percent + "% complete";
}
}
// ------------------------------------------------------
// Dashboard
// ------------------------------------------------------
function updateDashboard() {
const total = tasks.length;
const completed = tasks.filter(t => t.completed).length;
const quick = tasks.filter(t => t.energy === "quick").length;
const deep = tasks.filter(t => t.energy === "deep").length;
document.getElementById("totalTasks").textContent = total;
document.getElementById("completedTasks").textContent = completed;
document.getElementById("quickTasks").textContent = quick;
document.getElementById("deepTasks").textContent = deep;
let insight = "Start adding tasks";
if (quick > deep) insight = "Focus more on deep work 🔥";
if (deep > quick) insight = "Great deep work focus 💪";
if (completed === total && total > 0) insight = "All tasks done 🎉";
document.getElementById("insightText").textContent = insight;
}
// ------------------------------------------------------
// Browser Notifications
// ------------------------------------------------------
function requestNotificationPermission() {
if ("Notification" in window) {
Notification.requestPermission();
}
}
function sendNotification(message) {
if (!("Notification" in window)) return;
if (Notification.permission === "granted") {
new Notification("Task Manager", { body: message });
}
}
// ------------------------------------------------------
// Toast Notifications (IN-APP 🔥)
// ------------------------------------------------------
function showToast(message) {
const toast = document.createElement("div");
toast.className = "toast";
toast.innerText = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.classList.add("show");
}, 50);
setTimeout(() => {
toast.classList.remove("show");
setTimeout(() => toast.remove(), 300);
}, 2500);
}
// ------------------------------------------------------
// Filters
// ------------------------------------------------------
document.querySelectorAll(".filters button").forEach(btn => {
btn.addEventListener("click", () => {
currentFilter = btn.dataset.filter;
renderTasks();
});
});
// ------------------------------------------------------
// Incognito Mode
// ------------------------------------------------------
privacyToggle.addEventListener("click", () => {
incognitoMode = !incognitoMode;
taskList.classList.toggle("incognito");
privacyToggle.innerText = incognitoMode
? "Disable Incognito"
: "Incognito Mode";
});
// ------------------------------------------------------
// Init
// ------------------------------------------------------
renderTasks();
updateDashboard();
requestNotificationPermission();