-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
172 lines (147 loc) · 4.57 KB
/
scripts.js
File metadata and controls
172 lines (147 loc) · 4.57 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
const form = document.getElementById("form");
const taskContainer = document.getElementById("taskContainer");
function toggleForm(edit = false) {
if (!edit) document.getElementById("editIndex").value = "";
form.style.display = form.style.display === "none" ? "block" : "none";
}
function saveTasks(tasks) {
localStorage.setItem("tasks", JSON.stringify(tasks));
}
function getTasks() {
return JSON.parse(localStorage.getItem("tasks")) || [];
}
function calculateStatus(task) {
if (task.subtaskStatus && task.subtaskStatus.every((v) => v)) return "green";
const now = new Date();
const due = new Date(task.dueDate);
const diff = (due - now) / (1000 * 60 * 60 * 24);
if (diff < 0) return "red";
if (diff <= 1) return "orange";
return "cyan";
}
function renderTasks() {
const tasks = getTasks();
taskContainer.innerHTML = "";
tasks
.slice()
.reverse()
.forEach((task, indexReversed) => {
const index = tasks.length - 1 - indexReversed;
const status = calculateStatus(task);
const priorityClass = {
High: "priority-high",
Medium: "priority-medium",
Low: "priority-low",
}[task.priority];
const card = document.createElement("div");
card.className = `task-card ${status}`;
card.innerHTML = `
<h3 class="task-title">${task.title}</h3>
<p class="task-desc">${task.description}</p>
<div class="task-meta">
<span class="${priorityClass}">Priority: ${task.priority}</span>
<span>Due: ${task.dueDate}</span>
</div>
<ul style="margin-top: 0.75rem; padding-left: 1rem; list-style: none;">
${task.subtasks
.map(
(s, i) => `
<li style="margin-bottom: 0.5rem;">
<label style="display: flex; align-items: center; gap: 0.5rem;">
<input type="checkbox" ${
task.subtaskStatus[i] ? "checked" : ""
} onclick="toggleSubtask(${index}, ${i})" />
<span style="${
task.subtaskStatus[i]
? "text-decoration: line-through; color: gray;"
: "color: #222"
}">${s.trim()}</span>
</label>
</li>
`
)
.join("")}
</ul>
<div class="task-actions" style="margin-top: 1rem;">
<button onclick="editTask(${index})" class="edit-btn">Edit</button>
<button onclick="deleteTask(${index})" class="delete-btn">Delete</button>
</div>
`;
taskContainer.appendChild(card);
});
}
function submitTask(event) {
event.preventDefault();
const title = document.getElementById("title").value.trim();
const dueDate = document.getElementById("dueDate").value.trim();
if (!title) {
alert("Task title is required.");
return;
}
if (!dueDate) {
alert("Due date is required.");
return;
}
const description = document.getElementById("description").value;
const priority = document.getElementById("priority").value;
const subtasks = document
.getElementById("subtasks")
.value.split(",")
.map((s) => s.trim());
const index = document.getElementById("editIndex").value;
const tasks = getTasks();
if (index !== "") {
tasks[index] = {
...tasks[index],
title,
description,
priority,
subtasks,
subtaskStatus: subtasks.map(
(_, i) => tasks[index].subtaskStatus[i] || false
),
dueDate,
};
} else {
tasks.push({
title,
description,
priority,
subtasks,
subtaskStatus: subtasks.map(() => false),
dueDate,
});
}
saveTasks(tasks);
renderTasks();
document.getElementById("form").reset();
document.getElementById("editIndex").value = "";
document.getElementById("form").style.display = "none";
}
function editTask(index) {
const tasks = getTasks();
const task = tasks[index];
document.getElementById("title").value = task.title;
document.getElementById("description").value = task.description;
document.getElementById("priority").value = task.priority;
document.getElementById("subtasks").value = task.subtasks.join(", ");
document.getElementById("dueDate").value = task.dueDate;
document.getElementById("editIndex").value = index;
toggleForm(true);
}
function deleteTask(index) {
const tasks = getTasks();
tasks.splice(index, 1);
saveTasks(tasks);
renderTasks();
}
function toggleSubtask(taskIndex, subIndex) {
const tasks = getTasks();
tasks[taskIndex].subtaskStatus[subIndex] =
!tasks[taskIndex].subtaskStatus[subIndex];
saveTasks(tasks);
renderTasks();
}
window.onload = () => {
renderTasks();
};