-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
94 lines (75 loc) · 2.2 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
/* eslint-disable default-case */
const list = document.getElementById('todos-list');
const addBtn = document.getElementById('todo-add-btn');
const addInput = document.getElementById('todo-input');
function createTodo() {
const text = addInput.value;
if (text === '') {
return;
}
const li = document.createElement('li');
const checkbox = document.createElement('input');
checkbox.classList.add('checkbox');
checkbox.type = 'checkbox';
const paragraph = document.createElement('p');
paragraph.classList.add('paragraph');
paragraph.textContent = text;
const remove = document.createElement('span');
remove.classList.add('remove');
remove.innerHTML = '✗';
li.appendChild(checkbox);
li.appendChild(paragraph);
li.appendChild(remove);
list.appendChild(li);
addInput.value = '';
}
function showEditInput(paregraphElement) {
const editInput = document.getElementsByName('editInput')[0];
if (editInput) {
editInput.remove();
}
const input = document.createElement('input');
input.type = 'text';
input.name = 'editInput';
input.value = paregraphElement.textContent;
input.classList.add('editInput');
paregraphElement.parentElement.appendChild(input);
input.focus();
}
function removeTodo(removeElement) {
removeElement.parentElement.remove();
}
function toggleComplete(inputElement) {
if (inputElement.checked === false) {
inputElement.parentElement.classList.remove('complete');
} else {
inputElement.parentElement.classList.add('complete');
}
}
list.addEventListener('click', (event) => {
event.stopPropagation();
switch (event.target.tagName) {
case 'p':
showEditInput();
break;
case 'SPAN':
removeTodo(event.target);
break;
}
});
list.addEventListener('change', (event) => {
if (event.target.tagName === 'INPUT' && event.target.type === 'checkbox') {
toggleComplete(event.target);
}
});
list.addEventListener('keypress', (event) => {
if (event.target.tagName === 'INPUT' && event.target.type === 'checkbox') {
toggleComplete(event.target);
}
});
addBtn.addEventListener('click', createTodo);
addInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
createTodo();
}
});