-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
864 lines (792 loc) · 25.9 KB
/
Copy pathscript.js
File metadata and controls
864 lines (792 loc) · 25.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// ---------- Model ----------
const STORAGE_KEY = "assignmentPlanner.state";
const NOTIFY_WINDOW_MS = 24 * 60 * 60 * 1000; // schedule only if within next 24h
/**
* @typedef {Object} Assignment
* @property {string} id
* @property {string} title
* @property {string} course
* @property {string} dueDate // YYYY-MM-DD
* @property {string} dueTime // HH:MM or ""
* @property {"High"|"Medium"|"Low"} priority
* @property {number} estimateHours
* @property {"todo"|"doing"|"done"} status
* @property {string} notes
* @property {number} remindAheadMinutes // -1 for none
* @property {number|null} notifiedAt // timestamp when notification fired
* @property {number|null} scheduledAt // computed timestamp for reminder
* @property {number} createdAt
* @property {number} updatedAt
*/
const state = {
/** @type {Assignment[]} */
items: [],
filters: {
search: "",
course: "",
priority: "",
status: "",
hideDone: false,
sort: "dueAsc",
},
};
// timers per id (so we can cancel if edited/done)
const scheduledTimers = new Map();
// ---------- Elements ----------
const yearSpan = document.getElementById("year");
yearSpan.textContent = String(new Date().getFullYear());
const toastEl = document.getElementById("toast");
function toast(msg, isError = false) {
toastEl.textContent = msg;
toastEl.style.borderColor = isError ? "#5a1d1d" : "#232733";
toastEl.classList.add("show");
clearTimeout(toast._t);
toast._t = setTimeout(() => toastEl.classList.remove("show"), 2000);
}
// const rowsContainer = document.getElementById("rowsContainer");
const rowsTbody = document.getElementById("rowsTbody");
// Form inputs
const titleInput = document.getElementById("titleInput");
const courseInput = document.getElementById("courseInput");
const dueDateInput = document.getElementById("dueDateInput");
const dueTimeInput = document.getElementById("dueTimeInput");
const priorityInput = document.getElementById("priorityInput");
const estimateInput = document.getElementById("estimateInput");
const notesInput = document.getElementById("notesInput");
const reminderAheadInput = document.getElementById("reminderAheadInput");
const statusInput = document.getElementById("statusInput");
const createForm = document.getElementById("createForm");
const resetFormBtn = document.getElementById("resetFormBtn");
// Filters
const searchInput = document.getElementById("searchInput");
const filterCourse = document.getElementById("filterCourse");
const filterPriority = document.getElementById("filterPriority");
const filterStatus = document.getElementById("filterStatus");
const hideDoneCheck = document.getElementById("hideDoneCheck");
const sortSelect = document.getElementById("sortSelect");
const clearFiltersBtn = document.getElementById("clearFiltersBtn");
// Header actions
const notifyPermissionBtn = document.getElementById("notifyPermissionBtn");
const exportCsvBtn = document.getElementById("exportCsvBtn");
const importCsvInput = document.getElementById("importCsvInput");
const printBtn = document.getElementById("printBtn");
const clearAllBtn = document.getElementById("clearAllBtn");
// KPIs
const kpiTotal = document.getElementById("kpiTotal");
const kpiTodo = document.getElementById("kpiTodo");
const kpiDoing = document.getElementById("kpiDoing");
const kpiDone = document.getElementById("kpiDone");
const kpiOverdue = document.getElementById("kpiOverdue");
// Edit dialog elements
const editDialog = document.getElementById("editDialog");
const editId = document.getElementById("editId");
const editTitle = document.getElementById("editTitle");
const editCourse = document.getElementById("editCourse");
const editDate = document.getElementById("editDate");
const editTime = document.getElementById("editTime");
const editPriority = document.getElementById("editPriority");
const editEstimate = document.getElementById("editEstimate");
const editStatus = document.getElementById("editStatus");
const editNotes = document.getElementById("editNotes");
const editReminder = document.getElementById("editReminder");
const saveBtn = document.getElementById("saveBtn");
const deleteBtn = document.getElementById("deleteBtn");
// ---------- Utilities ----------
const byId = (id) => state.items.find((a) => a.id === id);
function uid(prefix = "id") {
return `${prefix}_${Math.random()
.toString(36)
.slice(2, 8)}_${Date.now().toString(36)}`;
}
function nowTs() {
return Date.now();
}
function dueTimestamp(assignment) {
const date = assignment.dueDate;
let time = assignment.dueTime || "23:59";
const [hh, mm] = time.split(":").map(Number);
const dt = new Date(date);
if (Number.isFinite(hh) && Number.isFinite(mm)) {
dt.setHours(hh, mm, 0, 0);
}
return dt.getTime();
}
function scheduledReminderTs(assignment) {
if (assignment.remindAheadMinutes < 0) return null;
return dueTimestamp(assignment) - assignment.remindAheadMinutes * 60 * 1000;
}
function isOverdue(assignment) {
return assignment.status !== "done" && dueTimestamp(assignment) < nowTs();
}
function isDueSoon(assignment) {
const ts = dueTimestamp(assignment);
const msLeft = ts - nowTs();
return (
assignment.status !== "done" &&
msLeft > 0 &&
msLeft <= 6 * 60 * 60 * 1000
); // 6h
}
function saveState() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}
function loadState() {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return;
try {
const parsed = JSON.parse(raw);
if (parsed?.items) state.items = parsed.items;
if (parsed?.filters)
state.filters = { ...state.filters, ...parsed.filters };
} catch {}
}
// ---------- Notifications ----------
function updateNotifyButton() {
if (!("Notification" in window)) {
notifyPermissionBtn.textContent = "🔔 Not supported";
notifyPermissionBtn.disabled = true;
return;
}
if (Notification.permission === "granted") {
notifyPermissionBtn.textContent = "🔔 Reminders On";
notifyPermissionBtn.classList.remove("btn");
notifyPermissionBtn.classList.add("btn-primary");
} else if (Notification.permission === "denied") {
notifyPermissionBtn.textContent = "🔕 Reminders Blocked";
notifyPermissionBtn.disabled = true;
} else {
notifyPermissionBtn.textContent = "🔔 Enable Reminders";
}
}
function fireNotification(assignment) {
if (!("Notification" in window) || Notification.permission !== "granted")
return;
const due = new Date(dueTimestamp(assignment));
const dueLabel = due.toLocaleString([], {
dateStyle: "medium",
timeStyle: assignment.dueTime ? "short" : undefined,
});
new Notification(`Due soon: ${assignment.title}`, {
body: `${assignment.course || "Course"} · ${dueLabel}`,
tag: `assignment-${assignment.id}`,
});
assignment.notifiedAt = nowTs();
saveState();
}
function cancelTimerFor(id) {
const t = scheduledTimers.get(id);
if (t) {
clearTimeout(t);
scheduledTimers.delete(id);
}
}
function scheduleReminder(assignment) {
cancelTimerFor(assignment.id);
const ts = scheduledReminderTs(assignment);
assignment.scheduledAt = ts;
if (!ts) return; // none
const delay = ts - nowTs();
if (delay <= 0) return; // past
// only schedule in current tab if within next 24h
if (delay <= NOTIFY_WINDOW_MS) {
const handle = setTimeout(() => {
scheduledTimers.delete(assignment.id);
// skip if marked done meanwhile
const fresh = byId(assignment.id);
if (!fresh || fresh.status === "done") return;
fireNotification(fresh);
}, delay);
scheduledTimers.set(assignment.id, handle);
}
}
// reschedule everything on boot (for near-term)
function rescheduleAll() {
scheduledTimers.forEach((h) => clearTimeout(h));
scheduledTimers.clear();
state.items.forEach((a) => {
if (a.remindAheadMinutes >= 0 && a.status !== "done")
scheduleReminder(a);
});
}
// ---------- CSV helpers ----------
const CSV_HEADERS = [
"title",
"course",
"dueDate",
"dueTime",
"priority",
"estimateHours",
"status",
"notes",
"remindAheadMinutes",
];
// escape for CSV
function csvEscape(value) {
const s = String(value ?? "");
if (s.includes('"') || s.includes(",") || s.includes("\n")) {
return `"${s.replace(/"/g, '""')}"`;
}
return s;
}
function toCSV(items) {
const head = CSV_HEADERS.join(",");
const lines = items.map((a) =>
CSV_HEADERS.map((h) => csvEscape(a[h])).join(",")
);
return [head, ...lines].join("\n");
}
// Parse one CSV line with quotes support
function parseCsvLine(line) {
const out = [];
let current = "";
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const ch = line[i];
if (inQuotes) {
if (ch === '"') {
if (line[i + 1] === '"') {
current += '"';
i++;
} else {
inQuotes = false;
}
} else {
current += ch;
}
} else {
if (ch === '"') {
inQuotes = true;
} else if (ch === ",") {
out.push(current);
current = "";
} else {
current += ch;
}
}
}
out.push(current);
return out;
}
function parseCSV(text) {
const lines = text.split(/\r?\n/).filter((l) => l.trim().length);
if (!lines.length) return [];
const header = parseCsvLine(lines[0]).map((h) => h.trim());
const idxMap = CSV_HEADERS.map((h) => header.indexOf(h));
const items = [];
for (let i = 1; i < lines.length; i++) {
const cols = parseCsvLine(lines[i]);
const rec = {};
CSV_HEADERS.forEach((h, j) => {
const idx = idxMap[j];
rec[h] = idx >= 0 ? cols[idx] : "";
});
const a = /** @type {Assignment} */ ({
id: uid("asg"),
title: rec.title,
course: rec.course,
dueDate: rec.dueDate,
dueTime: rec.dueTime,
priority: ["High", "Medium", "Low"].includes(rec.priority)
? rec.priority
: "Medium",
estimateHours: Number(rec.estimateHours) || 0,
status: ["todo", "doing", "done"].includes(rec.status)
? rec.status
: "todo",
notes: rec.notes || "",
remindAheadMinutes: Number(rec.remindAheadMinutes ?? -1),
notifiedAt: null,
scheduledAt: null,
createdAt: nowTs(),
updatedAt: nowTs(),
});
items.push(a);
}
return items;
}
// ---------- Rendering ----------
function renderKPIs() {
const total = state.items.length;
const todo = state.items.filter((a) => a.status === "todo").length;
const doing = state.items.filter((a) => a.status === "doing").length;
const done = state.items.filter((a) => a.status === "done").length;
const overdue = state.items.filter((a) => isOverdue(a)).length;
kpiTotal.textContent = total;
kpiTodo.textContent = todo;
kpiDoing.textContent = doing;
kpiDone.textContent = done;
kpiOverdue.textContent = overdue;
}
function uniqueCourses() {
const set = new Set(state.items.map((a) => a.course).filter(Boolean));
return Array.from(set).sort((a, b) => a.localeCompare(b));
}
function hydrateCourseFilter() {
const current = filterCourse.value;
filterCourse.innerHTML = `<option value="">All courses</option>`;
uniqueCourses().forEach((c) => {
const opt = document.createElement("option");
opt.value = c;
opt.textContent = c;
filterCourse.appendChild(opt);
});
filterCourse.value = current;
}
function courseTag(course) {
return course
? `<span class="badge">${course}</span>`
: `<span class="badge">—</span>`;
}
function priorityBadge(p) {
const klass = p === "High" ? "high" : p === "Low" ? "low" : "medium";
return `<span class="badge ${klass}">${p}</span>`;
}
function dueBadges(a) {
let b = "";
if (isOverdue(a)) b += ` <span class="badge overdue">Overdue</span>`;
else if (isDueSoon(a)) b += ` <span class="badge soon">Due soon</span>`;
return b;
}
function statusPill(s) {
return `<span class="status-pill ${s}">${
s === "todo" ? "To-do" : s === "doing" ? "Doing" : "Done"
}</span>`;
}
function reminderLabel(a) {
if (a.remindAheadMinutes < 0) return "None";
const mins = a.remindAheadMinutes;
if (mins === 0) return "At due";
if (mins === 15) return "15m before";
if (mins === 30) return "30m before";
if (mins === 60) return "1h before";
if (mins === 1440) return "1d before";
return `${mins}m before`;
}
function dueLabel(a) {
const ts = dueTimestamp(a);
const d = new Date(ts);
const datePart = d.toLocaleDateString([], {
weekday: "short",
month: "short",
day: "numeric",
});
const timePart = a.dueTime
? " " + d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })
: "";
return `${datePart}${timePart}${dueBadges(a)}`;
}
function matchesFilters(a) {
const f = state.filters;
if (f.search) {
const needle = f.search.toLowerCase();
const hay = `${a.title} ${a.notes}`.toLowerCase();
if (!hay.includes(needle)) return false;
}
if (f.course && a.course !== f.course) return false;
if (f.priority && a.priority !== f.priority) return false;
if (f.status && a.status !== f.status) return false;
if (f.hideDone && a.status === "done") return false;
return true;
}
function sortItems(items) {
const s = state.filters.sort;
const byDue = (x, y) => dueTimestamp(x) - dueTimestamp(y);
const prioRank = (p) => ({ High: 3, Medium: 2, Low: 1 }[p] || 0);
return items.sort((a, b) => {
switch (s) {
case "priorityDesc":
return (
prioRank(b.priority) - prioRank(a.priority) || byDue(a, b)
);
case "courseAsc":
return (
(a.course || "").localeCompare(b.course || "") ||
byDue(a, b)
);
case "titleAsc":
return a.title.localeCompare(b.title) || byDue(a, b);
case "statusAsc":
return a.status.localeCompare(b.status) || byDue(a, b);
case "dueAsc":
default:
return byDue(a, b) || a.title.localeCompare(b.title);
}
});
}
function renderList() {
const filtered = sortItems(state.items.filter(matchesFilters));
rowsTbody.innerHTML = "";
if (!filtered.length) {
const tr = document.createElement("tr");
const td = document.createElement("td");
td.colSpan = 8;
td.textContent = "No assignments match the filters.";
tr.appendChild(td);
rowsTbody.appendChild(tr);
renderKPIs();
hydrateCourseFilter();
return;
}
for (const a of filtered) {
const tr = document.createElement("tr");
tr.dataset.id = a.id;
tr.innerHTML = `
<td>
<div class="row-title">
<div>${a.title || "Untitled"}</div>
${a.notes ? `<small>${a.notes}</small>` : ""}
</div>
</td>
<td>${
a.course
? `<span class="badge">${a.course}</span>`
: '<span class="badge">—</span>'
}</td>
<td>${dueLabel(a)}</td>
<td>${priorityBadge(a.priority)}</td>
<td>${a.estimateHours ? `${a.estimateHours}h` : "—"}</td>
<td style="white-space: nowrap;">${statusPill(a.status)}</td>
<td>${reminderLabel(a)}</td>
<td>
<div class="actions-inline">
<button class="row-btn" data-action="toggle-done" style="white-space: nowrap;">${
a.status === "done" ? "↩︎ Undo" : "✔︎ Done"
}</button>
<button class="row-btn" data-action="ics">.ics</button>
<button class="row-btn" data-action="edit">Edit</button>
<button class="row-btn" data-action="delete">Delete</button>
</div>
</td>
`;
// Row click -> edit (ignore button clicks)
tr.addEventListener("click", (ev) => {
if (ev.target.closest("button")) return;
openEdit(a.id);
});
tr.querySelectorAll("button").forEach((btn) => {
btn.addEventListener("click", (ev) => {
ev.stopPropagation();
const action = btn.dataset.action;
if (action === "edit") openEdit(a.id);
if (action === "delete") confirmDelete(a.id);
if (action === "toggle-done") toggleDone(a.id);
if (action === "ics") exportICS(a.id);
});
});
rowsTbody.appendChild(tr);
}
renderKPIs();
hydrateCourseFilter();
}
// ---------- CRUD ----------
function addFromForm() {
const title = titleInput.value.trim();
const dueDate = dueDateInput.value;
if (!title || !dueDate) {
toast("Please fill Title and Due Date.", true);
return;
}
const assignment = /** @type {Assignment} */ ({
id: uid("asg"),
title,
course: courseInput.value.trim(),
dueDate,
dueTime: dueTimeInput.value,
priority: priorityInput.value,
estimateHours: Number(estimateInput.value) || 0,
status: statusInput.value,
notes: notesInput.value.trim(),
remindAheadMinutes: Number(reminderAheadInput.value),
notifiedAt: null,
scheduledAt: null,
createdAt: nowTs(),
updatedAt: nowTs(),
});
state.items.push(assignment);
saveState();
scheduleReminder(assignment);
renderList();
toast("Added assignment.");
resetCreateForm(); // keep UX clean
}
function resetCreateForm() {
createForm.reset();
priorityInput.value = "Medium";
estimateInput.value = "1";
reminderAheadInput.value = "30";
statusInput.value = "todo";
}
function isFormDirty() {
if (titleInput.value.trim()) return true;
if (courseInput.value.trim()) return true;
if (dueDateInput.value) return true;
if (dueTimeInput.value) return true;
if (notesInput.value.trim()) return true;
if (priorityInput.value !== "Medium") return true;
if (estimateInput.value !== "1") return true;
if (reminderAheadInput.value !== "30") return true;
if (statusInput.value !== "todo") return true;
return false;
}
function toggleDone(id) {
const a = byId(id);
if (!a) return;
if (a.status === "done") {
a.status = "todo";
} else {
a.status = "done";
cancelTimerFor(id);
}
a.updatedAt = nowTs();
saveState();
renderList();
}
function confirmDelete(id) {
const a = byId(id);
if (!a) return;
const d = new Date(dueTimestamp(a));
const label = `${a.title} — ${d.toLocaleDateString()}${
a.dueTime ? " " + a.dueTime : ""
}`;
const ok = confirm(
`Delete this assignment?\n\n${label}\n\nThis cannot be undone.`
);
if (!ok) return;
cancelTimerFor(id);
state.items = state.items.filter((x) => x.id !== id);
saveState();
renderList();
toast("Deleted.");
}
function clearAll() {
if (!state.items.length) {
toast("Nothing to clear.");
return;
}
const ok = confirm(
`Clear all ${state.items.length} assignments? This cannot be undone.`
);
if (!ok) return;
state.items.forEach((a) => cancelTimerFor(a.id));
state.items = [];
saveState();
renderList();
toast("All cleared.");
}
// ---------- Edit dialog ----------
function openEdit(id) {
const a = byId(id);
if (!a) return;
editId.value = a.id;
editTitle.value = a.title;
editCourse.value = a.course || "";
editDate.value = a.dueDate;
editTime.value = a.dueTime || "";
editPriority.value = a.priority;
editEstimate.value = String(a.estimateHours || 0);
editStatus.value = a.status;
editNotes.value = a.notes || "";
editReminder.value = String(a.remindAheadMinutes);
editDialog.showModal();
}
saveBtn.addEventListener("click", (ev) => {
ev.preventDefault();
const id = editId.value;
const a = byId(id);
if (!a) {
editDialog.close();
return;
}
a.title = editTitle.value.trim() || a.title;
a.course = editCourse.value.trim();
a.dueDate = editDate.value || a.dueDate;
a.dueTime = editTime.value;
a.priority = editPriority.value;
a.estimateHours = Number(editEstimate.value) || 0;
a.status = editStatus.value;
a.notes = editNotes.value.trim();
a.remindAheadMinutes = Number(editReminder.value);
a.updatedAt = nowTs();
// reschedule if needed
if (a.status === "done") cancelTimerFor(a.id);
else scheduleReminder(a);
saveState();
renderList();
editDialog.close();
toast("Updated.");
});
deleteBtn.addEventListener("click", (ev) => {
ev.preventDefault();
const id = editId.value;
const a = byId(id);
if (!a) {
editDialog.close();
return;
}
const d = new Date(dueTimestamp(a));
const summary = `${a.title} — ${d.toLocaleDateString()}${
a.dueTime ? " " + a.dueTime : ""
}`;
const ok = confirm(
`Delete this assignment?\n\n${summary}\n\nThis cannot be undone.`
);
if (!ok) return;
editDialog.close();
confirmDelete(id);
});
// ---------- ICS Export ----------
function exportICS(id) {
const a = byId(id);
if (!a) return;
const dtStart = new Date(dueTimestamp(a));
const dtEnd = new Date(
dtStart.getTime() +
Math.max(30, Math.round((a.estimateHours || 1) * 60)) * 60000
);
const fmt = (d) =>
d
.toISOString()
.replace(/[-:]/g, "")
.replace(/\.\d{3}Z$/, "Z");
const ics = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Assignment Planner//EN
BEGIN:VEVENT
UID:${a.id}@assignment-planner
DTSTAMP:${fmt(new Date())}
DTSTART:${fmt(dtStart)}
DTEND:${fmt(dtEnd)}
SUMMARY:${a.title.replace(/\n/g, " ")}
DESCRIPTION:${(a.course ? a.course + " — " : "") + (a.notes || "")}
PRIORITY:${a.priority === "High" ? 1 : a.priority === "Medium" ? 5 : 9}
END:VEVENT
END:VCALENDAR`;
const blob = new Blob([ics], { type: "text/calendar" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `assignment_${a.title
.replace(/[^a-z0-9]+/gi, "-")
.toLowerCase()}.ics`;
link.click();
URL.revokeObjectURL(url);
}
// ---------- Events ----------
createForm.addEventListener("submit", (e) => {
e.preventDefault();
addFromForm();
});
resetFormBtn.addEventListener("click", () => {
if (isFormDirty()) {
const ok = confirm(
"Reset the form? This will clear all entered values."
);
if (!ok) return;
}
resetCreateForm();
toast("Form reset.");
});
searchInput.addEventListener("input", () => {
state.filters.search = searchInput.value.trim();
saveState();
renderList();
});
filterCourse.addEventListener("change", () => {
state.filters.course = filterCourse.value;
saveState();
renderList();
});
filterPriority.addEventListener("change", () => {
state.filters.priority = filterPriority.value;
saveState();
renderList();
});
filterStatus.addEventListener("change", () => {
state.filters.status = filterStatus.value;
saveState();
renderList();
});
hideDoneCheck.addEventListener("change", () => {
state.filters.hideDone = hideDoneCheck.checked;
saveState();
renderList();
});
sortSelect.addEventListener("change", () => {
state.filters.sort = sortSelect.value;
saveState();
renderList();
});
clearFiltersBtn.addEventListener("click", () => {
state.filters = {
search: "",
course: "",
priority: "",
status: "",
hideDone: false,
sort: "dueAsc",
};
searchInput.value = "";
filterCourse.value = "";
filterPriority.value = "";
filterStatus.value = "";
hideDoneCheck.checked = false;
sortSelect.value = "dueAsc";
saveState();
renderList();
});
notifyPermissionBtn.addEventListener("click", async () => {
if (!("Notification" in window)) return;
const res = await Notification.requestPermission();
updateNotifyButton();
if (res === "granted") {
rescheduleAll();
toast("Reminders enabled.");
}
});
exportCsvBtn.addEventListener("click", () => {
const csv = toCSV(state.items);
const blob = new Blob([csv], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "assignments.csv";
a.click();
URL.revokeObjectURL(url);
});
importCsvInput.addEventListener("change", async (e) => {
const file = e.target.files?.[0];
if (!file) return;
try {
const text = await file.text();
const items = parseCSV(text);
if (!items.length) {
toast("No rows found in CSV.", true);
return;
}
state.items.push(...items);
saveState();
renderList();
rescheduleAll();
toast(`Imported ${items.length} item(s).`);
} catch {
toast("Failed to import CSV.", true);
} finally {
importCsvInput.value = "";
}
});
printBtn.addEventListener("click", () => window.print());
clearAllBtn.addEventListener("click", clearAll);
// ---------- Boot ----------
(function init() {
loadState();
updateNotifyButton();
// reflect saved filters
searchInput.value = state.filters.search;
filterCourse.value = state.filters.course;
filterPriority.value = state.filters.priority;
filterStatus.value = state.filters.status;
hideDoneCheck.checked = state.filters.hideDone;
sortSelect.value = state.filters.sort;
renderList();
rescheduleAll();
})();