-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
141 lines (116 loc) · 3.37 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
// SELECT ELEMENTS
const form = document.getElementById('todoform');
const todoInput = document.getElementById('newtodo');
const todosListEl = document.getElementById('todos-list');
const notificationEl = document.querySelector('.notification');
// VARS
let todos = JSON.parse(localStorage.getItem('todos')) || [];
let EditTodoId = -1;
// 1st render
renderTodos();
// FORM SUBMIT
form.addEventListener('submit', function (event) {
event.preventDefault();
saveTodo();
renderTodos();
localStorage.setItem('todos', JSON.stringify(todos));
});
// SAVE TODO
function saveTodo() {
const todoValue = todoInput.value;
// check if the todo is empty
const isEmpty = todoValue === '';
// check for duplicate todos
const isDuplicate = todos.some((todo) => todo.value.toUpperCase() === todoValue.toUpperCase());
if (isEmpty) {
showNotification("Todo's input is empty");
} else if (isDuplicate) {
showNotification('Todo already exists!');
} else {
if (EditTodoId >= 0) {
todos = todos.map((todo, index) => ({
...todo,
value: index === EditTodoId ? todoValue : todo.value,
}));
EditTodoId = -1;
} else {
todos.push({
value: todoValue,
checked: false,
color: '#' + Math.floor(Math.random() * 16777215).toString(16),
});
}
todoInput.value = '';
}
}
// RENDER TODOS
function renderTodos() {
if (todos.length === 0) {
todosListEl.innerHTML = '<center>Nothing to do!</center>';
return;
}
// CLEAR ELEMENT BEFORE A RE-RENDER
todosListEl.innerHTML = '';
// RENDER TODOS
todos.forEach((todo, index) => {
todosListEl.innerHTML += `
<div class="todo" id=${index}>
<i
class="bi ${todo.checked ? 'bi-check-circle-fill' : 'bi-circle'}"
style="color : ${todo.color}"
data-action="check"
></i>
<p class="${todo.checked ? 'checked' : ''}" data-action="check">${todo.value}</p>
<i class="bi bi-pencil-square" data-action="edit"></i>
<i class="bi bi-trash" data-action="delete"></i>
</div>
`;
});
}
// CLICK EVENT LISTENER FOR ALL THE TODOS
todosListEl.addEventListener('click', (event) => {
const target = event.target;
const parentElement = target.parentNode;
if (parentElement.className !== 'todo') return;
// t o d o id
const todo = parentElement;
const todoId = Number(todo.id);
// target action
const action = target.dataset.action;
action === 'check' && checkTodo(todoId);
action === 'edit' && editTodo(todoId);
action === 'delete' && deleteTodo(todoId);
});
// CHECK A TODO
function checkTodo(todoId) {
todos = todos.map((todo, index) => ({
...todo,
checked: index === todoId ? !todo.checked : todo.checked,
}));
renderTodos();
localStorage.setItem('todos', JSON.stringify(todos));
}
// EDIT A TODO
function editTodo(todoId) {
todoInput.value = todos[todoId].value;
EditTodoId = todoId;
}
// DELETE TODO
function deleteTodo(todoId) {
todos = todos.filter((todo, index) => index !== todoId);
EditTodoId = -1;
// re-render
renderTodos();
localStorage.setItem('todos', JSON.stringify(todos));
}
// SHOW A NOTIFICATION
function showNotification(msg) {
// change the message
notificationEl.innerHTML = msg;
// notification enter
notificationEl.classList.add('notif-enter');
// notification leave
setTimeout(() => {
notificationEl.classList.remove('notif-enter');
}, 2000);
}