-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (128 loc) · 4.78 KB
/
script.js
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
document.addEventListener("DOMContentLoaded", () => {
const taskForm = document.getElementById("taskForm"); // Form for adding tasks
const taskInput = document.getElementById("taskInput"); // Input field for task text
const taskCategory = document.getElementById("taskCategory"); // Dropdown menu for categories
const customCategory = document.getElementById("customCategory"); // Input field for custom categories
const taskPriority = document.getElementById("taskPriority"); // Dropdown menu for task priority
const tasksList = document.getElementById("tasksList"); // Container for displaying tasks
const filterCategory = document.getElementById("filterCategory"); // Dropdown menu for category filter
const filterStatus = document.getElementById("filterStatus"); // Dropdown menu for status filter
const applyFilters = document.getElementById("applyFilters"); // Button to apply filters
let tasks = []; // Store tasks locally
// Load tasks from Local Storage on page load
const loadTasks = () => {
const savedTasks = JSON.parse(localStorage.getItem("tasks")) || [];
tasks = savedTasks; // Update local tasks array
renderTasks(tasks); // Render tasks on load
};
// Save tasks to Local Storage
const saveTasksToLocalStorage = () => {
localStorage.setItem("tasks", JSON.stringify(tasks));
};
// Function to add a task
const addTask = (
taskText,
category,
priority,
completed = false,
saveToStorage = true
) => {
if (!taskText.trim()) {
alert("Task cannot be empty!");
return;
}
const task = {
text: taskText,
category,
priority,
completed,
};
tasks.push(task);
if (saveToStorage) saveTasksToLocalStorage();
renderTasks(tasks);
};
// Render Tasks
const renderTasks = (tasksToRender) => {
tasksList.innerHTML = ""; // Clear task list
tasksToRender.forEach((task, index) => {
const taskContainer = document.createElement("div");
taskContainer.classList.add("task");
// Apply styling based on priority
switch (task.priority) {
case "low":
taskContainer.style.backgroundColor = "#dff0d8";
break;
case "medium":
taskContainer.style.backgroundColor = "#fcf8e3";
break;
case "high":
taskContainer.style.backgroundColor = "#f2dede";
break;
}
const taskTextElement = document.createElement("span");
taskTextElement.textContent = `${task.text} [${task.category}] - Priority: ${task.priority}`;
taskTextElement.style.flexGrow = "1";
if (task.completed) {
taskTextElement.style.textDecoration = "line-through";
}
const toggleBtn = document.createElement("button");
toggleBtn.textContent = task.completed ? "Undo" : "Complete";
toggleBtn.classList.add("toggle-btn");
const removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.classList.add("remove-btn");
// Append elements
taskContainer.appendChild(taskTextElement);
taskContainer.appendChild(toggleBtn);
taskContainer.appendChild(removeBtn);
tasksList.appendChild(taskContainer);
// Toggle task completion
toggleBtn.addEventListener("click", () => {
task.completed = !task.completed;
saveTasksToLocalStorage();
renderTasks(tasks);
});
// Remove task with confirmation
removeBtn.addEventListener("click", () => {
const confirmed = confirm("Are you sure you want to delete this task?");
if (confirmed) {
tasks.splice(index, 1);
saveTasksToLocalStorage();
renderTasks(tasks);
}
});
});
};
// Apply Filters
applyFilters.addEventListener("click", () => {
let filteredTasks = tasks;
const categoryFilter = filterCategory.value;
const statusFilter = filterStatus.value;
if (categoryFilter !== "all") {
filteredTasks = filteredTasks.filter(
(task) => task.category === categoryFilter
);
}
if (statusFilter === "completed") {
filteredTasks = filteredTasks.filter((task) => task.completed);
} else if (statusFilter === "incomplete") {
filteredTasks = filteredTasks.filter((task) => !task.completed);
}
renderTasks(filteredTasks);
});
// Event listener for form submission
taskForm.addEventListener("submit", (e) => {
e.preventDefault();
const taskText = taskInput.value.trim();
let category = taskCategory.value;
const priority = taskPriority.value;
if (category === "Custom" && customCategory.value.trim()) {
category = customCategory.value.trim();
}
addTask(taskText, category, priority);
taskInput.value = "";
customCategory.value = "";
});
// Load tasks on page load
loadTasks();
});