-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (82 loc) · 2.81 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
const theme = document.getElementById('theme');
const newItemInput = document.getElementById('addItem');
const todoList = document.querySelector('.content ul');
const itemsLeft = document.querySelector('.items-left p');
itemsLeft.innerText = document.querySelectorAll('.list-item input[type="checkbox"]').length;
theme.addEventListener('click', () => {
document.querySelector('body').classList = [theme.checked ? 'theme-light' : 'theme-dark'];
});
document.querySelector('.add-new-item p').addEventListener('click', () => {
if (newItemInput.value.length > 0) {
createNewTodoItem(newItemInput.value);
newItemInput.value = '';
}
});
newItemInput.addEventListener('keypress', (e) => {
if (e.charCode === 13 && newItemInput.value.length > 0) {
createNewTodoItem(newItemInput.value);
newItemInput.value = '';
}
});
function createNewTodoItem(text) {
const elem = document.createElement('li');
elem.classList.add('flex-row');
elem.innerHTML = `
<label class="list-item">
<input type="checkbox" name="todoItem">
<p class="checkmark"></p>
<p class="text">${text}</p>
</label>
<p class="remove"></p>
`;
if (document.querySelector('.filter input[type="radio"]:checked').id === 'completed') {
elem.classList.add('hidden');
}
todoList.append(elem);
updateItemsCount(1);
}
function updateItemsCount(number) {
itemsLeft.innerText = +itemsLeft.innerText + number;
}
// remove todo item
function removeTodoItem(elem) {
elem.remove();
updateItemsCount(-1);
}
todoList.addEventListener('click',(event) => {
if (event.target.classList.contains('remove')) {
removeTodoItem(event.target.parentElement);
}
});
// clear comleted items
document.querySelector('.clear').addEventListener('click', () => {
document.querySelectorAll('.list-item input[type="checkbox"]:checked').forEach(item => {
removeTodoItem(item.closest('li'));
});
});
// filter todo list items
document.querySelectorAll('.filter input').forEach(radio => {
radio.addEventListener('change', (e) => {
filterTodoItems(e.target.id);
});
});
function filterTodoItems(id) {
const allItems = todoList.querySelectorAll('li');
switch(id) {
case 'all':
allItems.forEach(item => {
item.classList.remove('hidden');
})
break;
case 'active':
allItems.forEach(item => {
item.querySelector('input').checked ? item.classList.add('hidden') : item.classList.remove('hidden');;
})
break;
default:
allItems.forEach(item => {
!item.querySelector('input').checked ? item.classList.add('hidden') : item.classList.remove('hidden');;
})
break;
}
}