forked from Groverio/To-Do-List
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
245 lines (210 loc) · 8.11 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
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
let todoList = JSON.parse(localStorage.getItem('todoList')) || [];
let todoListhtml = '';
console.log(todoList);
let currentSortMethod = 'date'; // Default sort method
let currentSortOrder = 'asc'; // Default sort order for priority
let currentCategorySortOrder = 'asc'; // Default sort order for category
let isEditing = false;
let editIndex = null;
let filterMethod = 'all';
// Display the remaining characters count out of 120
document.querySelector('.js-name-input').addEventListener('input', (e) => {
let input = e.target.value;
if (input.length === 120) {
alert('max character limits exceeded');
}
});
function addTodo() {
const inputNameElement = document.querySelector('.js-name-input');
const inputDateElement = document.querySelector('.js-date-input');
const inputTimeElement = document.querySelector('.js-time-input');
const inputCategoryElement = document.querySelector('.js-category-input');
const inputPriorityElement = document.querySelector('.js-priority-input');
let name = inputNameElement.value;
let date = inputDateElement.value;
let time = inputTimeElement.value;
let category = inputCategoryElement.value;
let priority = inputPriorityElement.value;
// Validation checks
if (!name || !date || !time || !category || !priority) {
alert(
'Please fill in all fields: task, date, time, category, and priority.'
);
return;
}
if (isEditing) {
// Update the existing todo
todoList[editIndex] = {
name,
date,
time,
category,
priority,
completed: false,
}; // Ensure completed is set
isEditing = false; // Reset edit mode
editIndex = null;
// Change the button back to 'Add'
const addButton = document.querySelector('.js-add-button');
addButton.innerHTML = 'Add';
} else {
// Add a new todo
todoList.push({ name, date, time, category, priority, completed: false }); // Ensure completed is set
}
// Save to localStorage
localStorage.setItem('todoList', JSON.stringify(todoList));
// Clear the inputs
inputNameElement.value = '';
inputDateElement.value = '';
inputTimeElement.value = '';
inputCategoryElement.value = '';
inputPriorityElement.value = '';
setDefaultDateTime();
// Update the displayed list
updateTodoList();
}
function deleteTodo(index) {
// Remove the specific todo from the list
todoList.splice(index, 1);
localStorage.setItem('todoList', JSON.stringify(todoList));
updateTodoList();
}
function editTodo(index) {
let inputNameElement = document.querySelector('.js-name-input');
let inputDateElement = document.querySelector('.js-date-input');
let inputTimeElement = document.querySelector('.js-time-input');
let inputCategoryElement = document.querySelector('.js-category-input');
let inputPriorityElement = document.querySelector('.js-priority-input');
// Fill the input fields with the current values
inputNameElement.value = todoList[index].name;
inputDateElement.value = todoList[index].date;
inputTimeElement.value = todoList[index].time;
inputCategoryElement.value = todoList[index].category;
inputPriorityElement.value = todoList[index].priority;
// Set editing mode and the index of the todo being edited
isEditing = true;
editIndex = index;
// Change the add button to 'Update'
const addButton = document.querySelector('.js-add-button');
addButton.innerHTML = 'Update';
}
function updateTodoList() {
// Sort todoList based on the current sort method
let filteredTodos = todoList;
// Apply filtering based on the selected filter method
if (filterMethod === 'pending') {
filteredTodos = todoList.filter((todo) => !todo.completed);
} else if (filterMethod === 'completed') {
filteredTodos = todoList.filter((todo) => todo.completed);
}
filteredTodos.sort((a, b) => {
if (currentSortMethod === 'date') {
const dateA = new Date(a.date + ' ' + a.time);
const dateB = new Date(b.date + ' ' + b.time);
return dateA - dateB;
} else if (currentSortMethod === 'category') {
return currentCategorySortOrder === 'asc'
? a.category.localeCompare(b.category)
: b.category.localeCompare(a.category);
} else if (currentSortMethod === 'priority') {
const priorityOrder = { high: 0, medium: 1, low: 2 };
return currentSortOrder === 'asc'
? priorityOrder[a.priority] - priorityOrder[b.priority]
: priorityOrder[b.priority] - priorityOrder[a.priority];
}
});
const addElement = document.querySelector('.js-add-html');
todoListhtml = '';
for (let i = 0; i < filteredTodos.length; i++) {
const todo = filteredTodos[i];
todoListhtml += `
<div class="small-container ${todo.completed ? 'completed' : ''}">
<input type="checkbox" class="js-complete-checkbox" data-index="${i}" ${todo.completed ? 'checked' : ''} onchange="toggleComplete(${todoList.indexOf(todo)})">
<div class="task-info">
<span class="task-name">${todo.name}</span>
<span class="category-tag">${todo.category}</span>
<span class="priority-tag priority-${todo.priority}">${todo.priority}</span>
</div>
</div>
<div class="small-container">${todo.date}</div>
<div class="small-container">${todo.time}</div>
<button class="js-delete-button" data-index="${i}">
<i class="fa-solid fa-trash"></i>
</button>
<button class="js-edit-button" data-index="${i}">
<i class="fa-solid fa-pen"></i>
</button>`;
}
// Show or hide the task container based on the presence of tasks
if (todoList.length === 0) {
addElement.style.display = 'none'; // Hide if no tasks
} else {
addElement.style.display = 'grid'; // Show if tasks exist
addElement.innerHTML = todoListhtml;
}
// Add event listeners for delete and edit buttons
document.querySelectorAll('.js-delete-button').forEach((button) => {
button.addEventListener('click', (event) => {
const index = event.currentTarget.getAttribute('data-index');
deleteTodo(index);
});
});
document.querySelectorAll('.js-edit-button').forEach((button) => {
button.addEventListener('click', (event) => {
const index = event.currentTarget.getAttribute('data-index');
editTodo(index);
});
});
}
function setDefaultDateTime() {
const inputDateElement = document.querySelector('.js-date-input');
const inputTimeElement = document.querySelector('.js-time-input');
const now = new Date();
const date = now.toISOString().split('T')[0];
const time = now.toTimeString().split(' ')[0].slice(0, 5);
inputDateElement.value = date;
inputDateElement.min = date; // Set the min attribute to today's date
inputTimeElement.value = time;
}
function sortTodos(sortBy) {
if (sortBy === 'priority') {
currentSortOrder = currentSortOrder === 'asc' ? 'desc' : 'asc';
} else if (sortBy === 'category') {
currentCategorySortOrder =
currentCategorySortOrder === 'asc' ? 'desc' : 'asc';
}
currentSortMethod = sortBy;
updateTodoList();
}
function filterTodos() {
const filterElement = document.querySelector('.js-filter-input');
filterMethod = filterElement.value;
updateTodoList();
}
// eslint-disable-next-line no-unused-vars
function toggleComplete(index) {
todoList[index].completed = !todoList[index].completed;
localStorage.setItem('todoList', JSON.stringify(todoList));
updateTodoList();
}
// Initialize the todo list and set default date and time on page load
document.addEventListener('DOMContentLoaded', () => {
updateTodoList();
setDefaultDateTime();
// Set focus on the name input field
const inputNameElement = document.querySelector('.js-name-input');
inputNameElement.focus();
// Add event listeners to buttons
document.querySelector('.js-add-button').addEventListener('click', addTodo);
// Add event listeners for sorting buttons
document
.querySelector('.sort-button-category')
.addEventListener('click', () => sortTodos('category'));
document
.querySelector('.sort-button-priority')
.addEventListener('click', () => sortTodos('priority'));
// Add event listener for filter button
document
.querySelector('.js-filter-input')
.addEventListener('change', filterTodos);
});