-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (101 loc) · 3.4 KB
/
index.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
'use strict';
const taskInput = document.querySelector('.task__input input');
const taskBox = document.querySelector('.task__box');
const filters = document.querySelectorAll('.filters span');
const clearAll = document.querySelector('.clear__btn')
// Getting local storage todo-list data
let toDo = JSON.parse(localStorage.getItem('todo-list'));
let editedID;
let isEditedTask = false;
filters.forEach(btn => {
btn.addEventListener('click', () => {
document.querySelector('span.active').classList.remove('active');
btn.classList.add('active');
showToDo(btn.id);
})
})
const showToDo = function (filter) {
let li = '';
if (!toDo) return;
toDo.forEach((todo, id) => {
let isCompleted = todo.status === 'completed' ? 'checked' : '';
if (filter == todo.status || filter == 'all') {
li += `
<li class="task">
<label for="${id}">
<input onclick="updateStatus(this)" type="checkbox" id="${id}" ${isCompleted}>
<p class="${isCompleted}">${todo.name}</p>
</label>
<div class="setting">
<i class="fa-solid fa-ellipsis" onclick="showMenu(this)"></i>
<ul class="task__menu">
<li onclick="editTask(${id},'${todo.name}')"><i class="fa-solid fa-pen"></i>Edit</li>
<li onclick="deleteTask(${id})"><i class="fa-solid fa-trash"></i>Delete</li>
</ul>
</div>
</li>
`;
}
})
taskBox.innerHTML = li || `<span>You don't have any task now.</span>`;
}
showToDo('all');
taskInput.addEventListener('keyup', (e) => {
const inputData = taskInput.value.trim();
if (e.key == 'Enter' && inputData) {
if (!isEditedTask) {
if (!toDo) {
toDo = [];
}
let taskInfo = {
name: inputData,
status: 'pending'
}
toDo.push(taskInfo);
} else {
isEditedTask = false;
toDo[editedID].name = inputData;
}
taskInput.value = '';
localStorage.setItem('todo-list', JSON.stringify(toDo));
showToDo('all');
}
})
const showMenu = function (selectedTask) {
let taskMenu = selectedTask.closest('div').lastElementChild;
taskMenu.classList.add('show');
document.addEventListener('click', (e) => {
if (e.target.tagName != 'I' || e.target != selectedTask) {
taskMenu.classList.remove('show');
}
})
}
const editTask = function (taskID, taskName) {
editedID = taskID;
isEditedTask = true;
taskInput.value = taskName;
}
const deleteTask = function (id) {
// Delete item from array
toDo.splice(id, 1);
localStorage.setItem('todo-list', JSON.stringify(toDo));
showToDo('all');
}
const updateStatus = function (selectedTask) {
let taskName = selectedTask.closest('label').lastElementChild;
if (selectedTask.checked) {
taskName.classList.add('checked');
// Updating the status Value
toDo[selectedTask.id].status = 'completed';
} else {
taskName.classList.remove('checked');
// Updating the status Value
toDo[selectedTask.id].status = 'pending';
}
localStorage.setItem('todo-list', JSON.stringify(toDo))
}
clearAll.addEventListener('click', () => {
toDo.splice(0, toDo.length);
localStorage.clear();
showToDo('all')
})