-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
393 lines (332 loc) · 11.9 KB
/
Copy pathscript.js
File metadata and controls
393 lines (332 loc) · 11.9 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
const initialTasks = [
{ id: 1, name: "Design homepage wireframe", desc: "Create low-fi wireframes for the new landing page.", priority: "high", status: "in-progress", date: "2026-03-10" },
{ id: 2, name: "Set up CI/CD pipeline", desc: "Configure GitHub Actions for auto deploy.", priority: "medium", status: "done", date: "2026-03-05" },
{ id: 3, name: "Write API documentation", desc: "Document all REST endpoints with examples.", priority: "low", status: "todo", date: "2026-03-15" },
{ id: 4, name: "Fix login redirect bug", desc: "Users are redirected to 404 after OAuth login.", priority: "high", status: "in-progress", date: "2026-03-08" },
{ id: 5, name: "Implement Local Storage for Tasks", desc: "Added browser localStorage to persist tasks so they remain saved after page refresh, including add, delete, and status updates.", priority: "medium", status: "done", date: "2026-03-04" },
{ id: 6, name: "Database migration", desc: "Migrate user table to new schema.", priority: "high", status: "todo", date: "2026-03-09" },
{ id: 7, name: "Code review: auth module", desc: "Review PR #42 for security issues.", priority: "medium", status: "in-progress", date: "2026-03-11" },
{ id: 8, name: "Onboard new developer", desc: "Pair programming session + codebase walkthrough.", priority: "low", status: "todo", date: "2026-03-14" }
];
let tasks = JSON.parse(localStorage.getItem("tasks")) || [...initialTasks];
let nextId = tasks.length + 1;
let activeTab = "all";
let currentSort = "date";
const taskGrid = document.getElementById("task-grid");
const emptyState = document.getElementById("empty-state");
const sidebarToggle = document.getElementById("sidebar-toggle");
const mobileMenu = document.getElementById("mobile-menu");
const addTaskBtn = document.getElementById("add-task-btn");
const modal = document.getElementById("modal");
const modalClose = document.getElementById("modal-close");
const modalCancel = document.getElementById("modal-cancel");
const taskForm = document.getElementById("task-form");
const sortBtn = document.getElementById("sort-btn");
const sortDropdown = document.getElementById("sort-dropdown");
const searchInput = document.getElementById("search-input");
const tabButtons = document.querySelectorAll(".tab-btn");
const accordionTriggers = document.querySelectorAll(".accordion-trigger");
function saveTasks() {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
if (sidebarToggle && mobileMenu) {
sidebarToggle.addEventListener("click", function (e) {
e.stopPropagation();
mobileMenu.classList.toggle("hidden");
});
}
if (sortBtn && sortDropdown) {
sortBtn.addEventListener("click", function (e) {
e.stopPropagation();
sortDropdown.classList.toggle("hidden");
});
}
document.addEventListener("click", function (e) {
if (mobileMenu && sidebarToggle) {
if (!mobileMenu.contains(e.target) && !sidebarToggle.contains(e.target)) {
mobileMenu.classList.add("hidden");
}
}
if (sortDropdown && sortBtn) {
if (!sortDropdown.contains(e.target) && !sortBtn.contains(e.target)) {
sortDropdown.classList.add("hidden");
}
}
});
function openModal() {
if (modal) {
modal.classList.remove("hidden");
document.body.classList.add("overflow-hidden");
}
}
function closeModal() {
if (modal) {
modal.classList.add("hidden");
document.body.classList.remove("overflow-hidden");
}
if (taskForm) {
taskForm.reset();
}
}
if (addTaskBtn) addTaskBtn.addEventListener("click", openModal);
if (modalClose) modalClose.addEventListener("click", closeModal);
if (modalCancel) modalCancel.addEventListener("click", closeModal);
if (modal) {
modal.addEventListener("click", function (e) {
if (e.target === modal) {
closeModal();
}
});
}
document.addEventListener("keydown", function (e) {
if (e.key === "Escape") {
closeModal();
if (mobileMenu) mobileMenu.classList.add("hidden");
if (sortDropdown) sortDropdown.classList.add("hidden");
}
});
tabButtons.forEach(function (btn) {
btn.addEventListener("click", function () {
activeTab = btn.dataset.tab;
tabButtons.forEach(function (item) {
item.classList.remove("border-slate-900", "text-slate-900");
item.classList.add("border-transparent", "text-slate-500");
item.setAttribute("aria-selected", "false");
});
btn.classList.remove("border-transparent", "text-slate-500");
btn.classList.add("border-slate-900", "text-slate-900");
btn.setAttribute("aria-selected", "true");
renderTasks();
});
});
document.querySelectorAll(".sort-option").forEach(function (option) {
option.addEventListener("click", function () {
currentSort = option.dataset.sort;
if (sortDropdown) sortDropdown.classList.add("hidden");
renderTasks();
});
});
accordionTriggers.forEach(function (trigger) {
trigger.addEventListener("click", function () {
const content = trigger.nextElementSibling;
const icon = trigger.querySelector(".accordion-chevron");
document.querySelectorAll(".accordion-content").forEach(function (item) {
if (item !== content) {
item.classList.add("hidden");
}
});
document.querySelectorAll(".accordion-chevron").forEach(function (item) {
if (item !== icon) {
item.classList.remove("rotate-180");
}
});
if (content.classList.contains("hidden")) {
content.classList.remove("hidden");
if (icon) icon.classList.add("rotate-180");
} else {
content.classList.add("hidden");
if (icon) icon.classList.remove("rotate-180");
}
});
});
if (taskForm) {
taskForm.addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("task-name").value.trim();
const desc = document.getElementById("task-desc").value.trim();
const priority = document.getElementById("task-priority").value;
const status = document.getElementById("task-status").value;
const date = document.getElementById("task-date").value;
if (name === "") return;
const newTask = {
id: nextId,
name: name,
desc: desc === "" ? "No description added." : desc,
priority: priority,
status: status,
date: date === "" ? new Date().toISOString().split("T")[0] : date
};
nextId++;
tasks.push(newTask);
saveTasks();
updateStats();
renderTasks();
closeModal();
});
}
if (searchInput) {
searchInput.addEventListener("input", function () {
renderTasks();
});
}
function deleteTask(id) {
tasks = tasks.filter(function (task) {
return task.id !== id;
});
saveTasks();
updateStats();
renderTasks();
}
function changeStatus(id) {
const task = tasks.find(function (item) {
return item.id === id;
});
if (!task) return;
if (task.status === "todo") {
task.status = "in-progress";
} else if (task.status === "in-progress") {
task.status = "done";
} else {
task.status = "todo";
}
saveTasks();
updateStats();
renderTasks();
}
function getTasks() {
let filteredTasks = [...tasks];
if (activeTab !== "all") {
filteredTasks = filteredTasks.filter(function (task) {
return task.status === activeTab;
});
}
const searchText = searchInput.value.trim().toLowerCase();
if (searchText !== "") {
filteredTasks = filteredTasks.filter(function (task) {
return (
task.name.toLowerCase().includes(searchText) ||
task.desc.toLowerCase().includes(searchText)
);
});
}
if (currentSort === "date") {
filteredTasks.sort(function (a, b) {
return new Date(a.date) - new Date(b.date);
});
}
if (currentSort === "name") {
filteredTasks.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
}
if (currentSort === "priority") {
const order = { high: 1, medium: 2, low: 3 };
filteredTasks.sort(function (a, b) {
return order[a.priority] - order[b.priority];
});
}
return filteredTasks;
}
function renderTasks() {
const filteredTasks = getTasks();
if (filteredTasks.length === 0) {
taskGrid.innerHTML = "";
emptyState.classList.remove("hidden");
return;
}
emptyState.classList.add("hidden");
taskGrid.innerHTML = filteredTasks.map(function (task) {
return createTaskCard(task);
}).join("");
const deleteButtons = document.querySelectorAll("[data-delete]");
deleteButtons.forEach(function (btn) {
btn.addEventListener("click", function () {
deleteTask(Number(btn.dataset.delete));
});
});
const statusButtons = document.querySelectorAll("[data-status]");
statusButtons.forEach(function (btn) {
btn.addEventListener("click", function () {
changeStatus(Number(btn.dataset.status));
});
});
}
function createTaskCard(task) {
let priorityClass = "";
let statusClass = "";
let statusText = "";
let actionText = "";
if (task.priority === "high") {
priorityClass = "bg-red-100 text-red-700";
} else if (task.priority === "medium") {
priorityClass = "bg-yellow-100 text-yellow-700";
} else {
priorityClass = "bg-green-100 text-green-700";
}
if (task.status === "todo") {
statusClass = "bg-slate-100 text-slate-700";
statusText = "To Do";
actionText = "Mark In Progress";
} else if (task.status === "in-progress") {
statusClass = "bg-blue-100 text-blue-700";
statusText = "In Progress";
actionText = "Mark Completed";
} else {
statusClass = "bg-emerald-100 text-emerald-700";
statusText = "Completed";
actionText = "Undo";
}
return `
<div class="group rounded-2xl border border-slate-200 bg-white p-5 shadow-sm hover:shadow-md transition">
<div class="flex items-start justify-between gap-3">
<div>
<h3 class="text-base font-semibold ${task.status === "done" ? "line-through text-slate-400" : "text-slate-900"}">${task.name}</h3>
<p class="mt-2 text-sm text-slate-500">${task.desc}</p>
</div>
<button data-delete="${task.id}" class="invisible group-hover:visible rounded-lg p-2 text-slate-400 hover:bg-slate-100 hover:text-red-500">
🗑️
</button>
</div>
<div class="mt-4 flex flex-wrap gap-2">
<span class="rounded-full px-3 py-1 text-xs font-semibold ${priorityClass}">
${capitalize(task.priority)}
</span>
<span class="rounded-full px-3 py-1 text-xs font-semibold ${statusClass}">
${statusText}
</span>
</div>
<div class="mt-5 flex items-center justify-between border-t border-slate-100 pt-4 gap-3">
<p class="text-sm text-slate-500">${formatDate(task.date)}</p>
<button data-status="${task.id}" class="rounded-xl border border-slate-200 px-3 py-2 text-xs font-semibold hover:bg-slate-50">
${actionText}
</button>
</div>
</div>
`;
}
function updateStats() {
const total = tasks.length;
const inProgress = tasks.filter(function (task) {
return task.status === "in-progress";
}).length;
const completed = tasks.filter(function (task) {
return task.status === "done";
}).length;
const overdue = tasks.filter(function (task) {
const today = new Date();
today.setHours(0, 0, 0, 0);
const dueDate = new Date(task.date);
return dueDate < today && task.status !== "done";
}).length;
const totalEl = document.getElementById("stat-total");
const progressEl = document.getElementById("stat-progress");
const doneEl = document.getElementById("stat-done");
const overdueEl = document.getElementById("stat-overdue");
if (totalEl) totalEl.textContent = total;
if (progressEl) progressEl.textContent = inProgress;
if (doneEl) doneEl.textContent = completed;
if (overdueEl) overdueEl.textContent = overdue;
}
function formatDate(dateString) {
const date = new Date(dateString + "T00:00:00");
return date.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric"
});
}
function capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
updateStats();
renderTasks();