-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
178 lines (148 loc) · 5.24 KB
/
main.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
const selectAll = document.getElementById('select-all')
const todoActions = document.querySelectorAll('.todo-table-action')
todoChecksFunc();
function todoChecksFunc(){
const todoChecks = document.querySelectorAll('.todo-check')
selectAll.onchange = function(){
todoChecks.forEach(item => {
item.checked = selectAll.checked;
})
todoActions[0].classList.toggle('active', !selectAll.checked);
todoActions[1].classList.toggle('active', selectAll.checked);
}
todoChecks.forEach(item=> {
item.onchange = function() {
const listCheck = Array.from(todoChecks).map(i=> {
return i.checked
})
todoActions[0].classList.toggle('active', !listCheck.includes(true))
todoActions[1].classList.toggle('active', listCheck.includes(true))
selectAll.checked = !listCheck.includes(false)
}
})
}
function resetCheck() {
selectAll.checked = false
todoActions[0].classList.toggle('active', !selectAll.checked)
todoActions[1].classList.toggle('active', selectAll.checked)
}
!localStorage.getItem('todo') && localStorage.setItem('todo', JSON.stringify([]));
const formAction = document.querySelector('.todo-form');
const todoBody = document.querySelector('.todo-table-body');
renderTodo()
formAction.onsubmit = function(e){
e.preventDefault();
const todo = JSON.parse(localStorage.getItem('todo'));
if(e.target.todo.value){
const newTodo = {
id : getMaxId() + 1,
todo : e.target.todo.value,
completed: false
}
localStorage.setItem('todo', JSON.stringify([newTodo, ...todo]));
formAction.reset()
renderTodo()
}
}
function getMaxId(){
const todo = JSON.parse(localStorage.getItem('todo'));
const listId = todo.map(item => item.id);
return todo.length > 0 ? Math.max(...listId) : 0;
}
function renderTodo(){
todoBody.innerHTML = "";
const todo = JSON.parse(localStorage.getItem('todo'));
if(todo.length > 0){
todo.forEach(item => {
const template = `
<tr class="${item.completed ? "completed" : ''}">
<td>
<input type="checkbox" class="todo-check" id="${item.id}">
</td>
<td>
<div class="todo-item">
<input type="text" class="todo-text" ondblclick="changeCompleted(this, ${item.id})" value="${item.todo}" readonly />
<div class="todo-item-action">
<span onclick="editTodo(this, ${item.id})"><i class="ri-edit-line"></i></span>
<span onclick="deleteTodo(${item.id})"><i class="ri-delete-bin-line"></i></span>
</div>
</div>
</td>
</tr>
`;
todoBody.insertAdjacentHTML('beforeend', template);
});
}else{
const template = `
<tr>
<td colspan="2">No todo</td>
</tr>
`;
todoBody.insertAdjacentHTML('beforeend', template)
}
todoChecksFunc()
}
function editTodo(el, id){
const todoText = el.closest('.todo-item').querySelector('.todo-text');
todoText.removeAttribute('readonly');
todoText.focus()
todoText.onblur = function(){
todoText.setAttribute('readonly', '');
updateTodo(id, todoText.value);
}
todoText.onkeydown = function(e){
if(e.key == 'Enter'){
e.preventDefault();
todoText.onblur();
}
}
}
function updateTodo(id, val){
if(val){
const todo = JSON.parse(localStorage.getItem('todo'));
const todoIndex = todo.findIndex(i => i.id == id);
todo[todoIndex].todo = val;
localStorage.setItem('todo', JSON.stringify(todo));
}
renderTodo();
}
function deleteTodo(id){
const todo = JSON.parse(localStorage.getItem('todo'));
const newTodo = todo.filter(item => item.id != id);
localStorage.setItem('todo', JSON.stringify(newTodo));
renderTodo();
}
function deleteManyItems(){
let todo = JSON.parse(localStorage.getItem('todo'));
const todoChecks = document.querySelectorAll('.todo-check');
todoChecks.forEach(item => {
if(item.checked){
todo = todo.filter(i => i.id != item.id);
}
});
localStorage.setItem('todo', JSON.stringify(todo));
renderTodo();
resetCheck();
}
function changeCompleted(el, id) {
if(el.getAttribute('readonly') == '') {
const todo = JSON.parse(localStorage.getItem('todo'))
const todoIndex = todo.findIndex(i=> i.id == id)
todo[todoIndex].completed = !todo[todoIndex].completed
localStorage.setItem('todo', JSON.stringify(todo))
renderTodo()
}
}
function changeCompletedMany(status) {
let todo = JSON.parse(localStorage.getItem('todo'));
const todoChecks = document.querySelectorAll('.todo-check');
todoChecks.forEach(item => {
if(item.checked){
const todoIndex = todo.findIndex(i => i.id == item.id);
todo[todoIndex].completed = status;
}
})
localStorage.setItem('todo', JSON.stringify(todo))
renderTodo();
resetCheck();
}