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
| let todos = []; let navId = 'all';
const $todos = document.querySelector('.todos'); const $inputTodo = document.querySelector('.input-todo'); const $completeAll = document.querySelector('.complete-all'); const $nav = document.querySelector('.nav'); const $completedTodos = document.querySelector('.completed-todos'); const $activeTodos = document.querySelector('.active-todos'); const $clearCompleted = document.querySelector('.clear-completed > .btn');
function getTodos() { todos = [ { id: 1, content: 'HTML', completed: false }, { id: 2, content: 'CSS', completed: true }, { id: 3, content: 'Javascript', completed: false } ];
todos.sort((todo1, todo2) => todo2.id - todo1.id); }
function findMaxId() { return Math.max(0, ...todos.map((todo) => todo.id)) + 1; }
function completedCount() { return todos.filter((todo) => todo.completed).length; }
function activeCount() { return todos.filter((todo) => !todo.completed).length; }
function render() {
const tempTodos = todos.filter(({ completed }) => (navId === 'all' ? true : navId === 'active' ? !completed : completed));
let html = ''; tempTodos.forEach(({ id, content, completed }) => { html += ` <li id="${id}" class="todo-item"> <input class="checkbox" type="checkbox" id="ck-${id}" ${completed ? 'checked' : ''}> <label for="ck-${id}">${content}</label> <i class="remove-todo far fa-times-circle"></i> </li>`; });
$todos.innerHTML = html;
$completedTodos.textContent = completedCount(); $activeTodos.textContent = activeCount();
}
function addTodo(content) { todos = [{ id: findMaxId(), content, completed: false }, ...todos]; console.log('[addTodo] : ', todos); }
function removeTodo(id) { todos = todos.filter((todo) => todo.id !== id); console.log('[removeTodo] : ', todos); }
function toggleTodo(id) { todos = todos.map((todo) => (todo.id === id ? { ...todo, completed: !todo.completed } : todo)); console.log('[toggleTodo] : ', todos); }
function allChangeToggle(checked) { todos = todos.map((todo) => ({ ...todo, completed: checked })); console.log('[allChangeToggle] : ', todos); }
function clearCompleted() { todos = todos.filter((todo) => !todo.completed); console.log('[clearCompleted] : ', todos); }
function changeNav(id) { [...$nav.children].forEach(($navItem) => { $navItem.classList.toggle('active', $navItem.id === id); navId = id; }); console.log('[changeNav] : ', todos); }
window.onload = () => { getTodos(); render(); };
$inputTodo.onkeydown = ({ target, keyCode }) => { if (target.value.trim() === '' || keyCode !== 13) return; addTodo(target.value); target.value = ''; render(); };
$todos.onclick = ({ target }) => { if (!target.classList.contains('remove-todo')) return; removeTodo(+target.parentNode.id); render(); };
$todos.onchange = ({ target }) => { toggleTodo(+target.parentNode.id); render(); };
$completeAll.onchange = ({ target }) => { allChangeToggle(target.checked); render(); };
$clearCompleted.onclick = () => { clearCompleted(); render(); };
$nav.onclick = ({ target }) => { if (target.classList.contains('nav')) return; changeNav(target.id); render(); };
|