-
Notifications
You must be signed in to change notification settings - Fork 139
/
script.js
384 lines (324 loc) · 11.9 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
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
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';
// Add icon - for add action
const addIcon = document.createElement('i');
addIcon.classList.add('fa-solid', 'fa-add');
// Check icon - for update action
const checkIcon = document.createElement('i');
checkIcon.classList.add('fa-solid', 'fa-check');
// 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');
}
});
let dateCheck = false;
let timeCheck = false;
document.querySelector('.js-date-input').addEventListener('click', (e) => {
e.preventDefault();
if (!dateCheck) {
e.target.showPicker();
dateCheck = true;
} else {
dateCheck = false;
}
});
document.querySelector('.js-date-input').addEventListener('blur', () => {
dateCheck = false;
});
document.querySelector('.js-time-input').addEventListener('click', (e) => {
e.preventDefault();
if (!timeCheck) {
e.target.showPicker();
timeCheck = true;
} else {
timeCheck = false;
}
});
document.querySelector('.js-time-input').addEventListener('blur', () => {
timeCheck = false;
});
function clearInputs() {
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');
// Clear the inputs
inputNameElement.value = '';
inputDateElement.value = '';
inputTimeElement.value = '';
inputCategoryElement.value = '';
inputPriorityElement.value = '';
setDefaultDateTime();
}
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;
}
// Check that date is not in past
if (date < inputDateElement.min) {
alert('Please select the current date or a future date.');
return;
}
// Check that time is not in past
if (time < inputTimeElement.min && date === inputDateElement.min) {
alert('Please select a future time.');
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 = '';
addButton.title = 'Add';
addButton.appendChild(addIcon);
// Hide cancel button
const cancelEditBtn = document.querySelector('.js-cancel-button');
cancelEditBtn.style.display = 'none';
} 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));
// Reset the inputs
clearInputs();
// Update the displayed list
updateTodoList();
updateTaskCounter();
}
function deleteTodo(index) {
// Remove the specific todo from the list
todoList.splice(index, 1);
localStorage.setItem('todoList', JSON.stringify(todoList));
updateTodoList();
updateTaskCounter();
}
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;
// Enable cancel option
const cancelEditBtn = document.querySelector('.js-cancel-button');
cancelEditBtn.style.display = 'block';
// Change the add button to 'Update'
const addButton = document.querySelector('.js-add-button');
addButton.innerHTML = '';
addButton.title = 'Update';
addButton.appendChild(checkIcon);
updateTaskCounter();
}
function cancelEditTodo() {
isEditing = false; // Reset edit mode
editIndex = null;
// Reset the inputs
clearInputs();
// Hide edit cancel action button on page load
const cancelEditBtn = document.querySelector('.js-cancel-button');
cancelEditBtn.style.display = 'none';
// Change the button back to 'Add'
const addButton = document.querySelector('.js-add-button');
addButton.innerHTML = '';
addButton.title = 'Add';
addButton.appendChild(addIcon);
updateTaskCounter();
}
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];
}
updateTaskCounter();
});
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;
}
console.log(window.innerWidth);
// 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);
});
});
// Call the task counter update function
updateTaskCounter();
}
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;
inputTimeElement.min = time; // Set the min attribute to current 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();
}
// this shows the sucessNotification for 4000ms
function successNotification() {
const success = document.getElementById('js-success-notification');
success.style.display = 'flex';
setTimeout(() => {
success.style.display = 'none';
}, 4000);
}
// eslint-disable-next-line no-unused-vars
function toggleComplete(index) {
todoList[index].completed = !todoList[index].completed;
if (todoList[index].completed) {
successNotification();
}
localStorage.setItem('todoList', JSON.stringify(todoList));
updateTodoList();
updateTaskCounter();
}
function updateTaskCounter() {
const totalTasks = todoList.length;
// Select the element where the task counter is displayed
const taskCounterButton = document.querySelector('.task-counter-button');
// Update the text of the task counter button
if (taskCounterButton) {
taskCounterButton.innerText = `Tasks: ${totalTasks}`;
}
}
// 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();
// Hide edit cancel action button on page load
const cancelEditBtn = document.querySelector('.js-cancel-button');
cancelEditBtn.style.display = 'none';
// Add event listeners to buttons
document.querySelector('.js-add-button').addEventListener('click', addTodo);
document
.querySelector('.js-cancel-button')
.addEventListener('click', cancelEditTodo);
// 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);
});
// Add year in the footer(CopyRight Notice)
let year = document.querySelector('.year');
year.innerText = new Date().getFullYear();