-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (112 loc) · 3.57 KB
/
script.js
File metadata and controls
122 lines (112 loc) · 3.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
const taskList = JSON.parse(localStorage.getItem('tasks')) || [];
function addTask(task) {
taskList.push(task);
localStorage.setItem('tasks', JSON.stringify(taskList));
}
function renderTasks(category) {
const tasksHTML = taskList.filter((task) => {
if (category === 'all' || task.category === category) {
return task;
}
}).map((task, index) => {
const completed = task.completed ? 'checked' : '';
return `
<tr>
<td> ${task.name} </td>
<td> ${task.date} </td>
<td> ${task.time} </td>
<td> ${getPriorityIcon(task.priority)} </td>
<td> ${getCategoryIcon(task.category)} </td>
<td><input type='checkbox' id='complete-${index}' ${completed}></td>
<td><b><button class='delete-task' data-index='${index}'>Remove</button></b></td>
</tr>
`;
}).join('');
document.getElementById('tasks').innerHTML = `
<table style="margin-left:2rem;font-size:18px;">
<thead>
<tr>
<th>|| Task Name ||</th>
<th> Task Date ||</th>
<th> Task Time ||</th>
<th> Task Priority ||</th>
<th> Task Category ||</th>
<th> Completed ||</th>
<th></th>
</tr>
</thead>
<tbody>
${tasksHTML}
</tbody>
</table>
`;
}
function getTaskStyle(task) {
if (new Date(task.date + ' ' + task.time) <= new Date()) {
return 'font-weight: bold;';
}
}
function getPriorityIcon(priority) {
switch (priority) {
case 'low':
return '❌';
case 'medium':
return '❍';
case 'high':
return '❎';
default:
return '';
}
}
function getCategoryIcon(category) {
switch (category) {
case 'school':
return '<img src="school.png" alt="Profile Image" /><span style="color:red"> School</span>';
case 'work':
return '<img src="work.png" alt="Profile Image" /><span style="color:orange"> Work</span>';
case 'personal':
return '<img src="personal.png" alt="Profile Image" /><span style="color:blue"> Personal</span>';
default:
return '';
}
}
function updateTaskCompletion(index) {
taskList[index].completed = !taskList[index].completed;
localStorage.setItem('tasks', JSON.stringify(taskList));
renderTasks('all');
}
function deleteTask(index) {
taskList.splice(index, 1);
localStorage.setItem('tasks', JSON.stringify(taskList));
renderTasks('all');
}
document.addEventListener('click', (e) => {
if (e.target.tagName === 'INPUT' && e.target.type === 'checkbox') {
const index = e.target.id.split('-')[1];
updateTaskCompletion(index);
} else if (e.target.className === 'delete-task') {
const index = e.target.dataset.index;
deleteTask(index);
}
});
// Render initial tasks from local storage
if (taskList.length > 0) {
renderTasks('all');
}
document.getElementById('add-task-form').addEventListener('submit', (e) => {
e.preventDefault();
const taskName = document.getElementById('task-name').value;
const taskDate = document.getElementById('task-date').value;
const taskTime = document.getElementById('task-time').value;
const taskPriority = document.getElementById('task-priority').value;
const taskCategory = document.getElementById('school').checked ? 'school' : document.getElementById('work').checked ? 'work' : 'personal';
addTask({
name: taskName,
date: taskDate,
time: taskTime,
priority: taskPriority,
category: taskCategory,
completed: false
});
renderTasks('all');
});