|
| 1 | +(() => { |
| 2 | + const STORAGE_KEY = 'todo-list-tasks-v1'; |
| 3 | + |
| 4 | + const form = document.getElementById('todo-form'); |
| 5 | + const input = document.getElementById('todo-input'); |
| 6 | + const listEl = document.getElementById('todo-list'); |
| 7 | + const itemsLeft = document.getElementById('items-left'); |
| 8 | + const filters = document.querySelectorAll('.filter'); |
| 9 | + const clearCompletedBtn = document.getElementById('clear-completed'); |
| 10 | + |
| 11 | + let tasks = []; |
| 12 | + let filter = 'all'; |
| 13 | + |
| 14 | + function save() { |
| 15 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks)); |
| 16 | + } |
| 17 | + |
| 18 | + function load() { |
| 19 | + try { |
| 20 | + const raw = localStorage.getItem(STORAGE_KEY); |
| 21 | + tasks = raw ? JSON.parse(raw) : []; |
| 22 | + } catch (e) { |
| 23 | + tasks = []; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + function uid() { |
| 28 | + return Date.now().toString(36) + Math.random().toString(36).slice(2, 8); |
| 29 | + } |
| 30 | + |
| 31 | + function render() { |
| 32 | + listEl.innerHTML = ''; |
| 33 | + const visible = tasks.filter(t => { |
| 34 | + if (filter === 'active') return !t.completed; |
| 35 | + if (filter === 'completed') return t.completed; |
| 36 | + return true; |
| 37 | + }); |
| 38 | + |
| 39 | + visible.forEach(task => listEl.appendChild(createTaskEl(task))); |
| 40 | + itemsLeft.textContent = tasks.filter(t => !t.completed).length; |
| 41 | + } |
| 42 | + |
| 43 | + function createTaskEl(task) { |
| 44 | + const li = document.createElement('li'); |
| 45 | + li.className = 'todo-item'; |
| 46 | + li.dataset.id = task.id; |
| 47 | + |
| 48 | + const left = document.createElement('div'); |
| 49 | + left.className = 'left'; |
| 50 | + |
| 51 | + const chk = document.createElement('input'); |
| 52 | + chk.type = 'checkbox'; |
| 53 | + chk.checked = !!task.completed; |
| 54 | + chk.addEventListener('change', () => { |
| 55 | + task.completed = chk.checked; |
| 56 | + save(); |
| 57 | + render(); |
| 58 | + }); |
| 59 | + |
| 60 | + const span = document.createElement('span'); |
| 61 | + span.className = 'task-text' + (task.completed ? ' completed' : ''); |
| 62 | + span.textContent = task.text; |
| 63 | + span.title = 'Double click to edit'; |
| 64 | + span.addEventListener('dblclick', () => enterEditMode(li, task, span)); |
| 65 | + |
| 66 | + left.appendChild(chk); |
| 67 | + left.appendChild(span); |
| 68 | + |
| 69 | + const actions = document.createElement('div'); |
| 70 | + actions.className = 'actions'; |
| 71 | + |
| 72 | + const editBtn = document.createElement('button'); |
| 73 | + editBtn.type = 'button'; |
| 74 | + editBtn.textContent = 'Edit'; |
| 75 | + editBtn.addEventListener('click', () => enterEditMode(li, task, span)); |
| 76 | + |
| 77 | + const delBtn = document.createElement('button'); |
| 78 | + delBtn.type = 'button'; |
| 79 | + delBtn.textContent = 'Delete'; |
| 80 | + delBtn.addEventListener('click', () => { |
| 81 | + tasks = tasks.filter(t => t.id !== task.id); |
| 82 | + save(); |
| 83 | + render(); |
| 84 | + }); |
| 85 | + |
| 86 | + actions.appendChild(editBtn); |
| 87 | + actions.appendChild(delBtn); |
| 88 | + |
| 89 | + li.appendChild(left); |
| 90 | + li.appendChild(actions); |
| 91 | + return li; |
| 92 | + } |
| 93 | + |
| 94 | + function enterEditMode(li, task, span) { |
| 95 | + const input = document.createElement('input'); |
| 96 | + input.type = 'text'; |
| 97 | + input.className = 'edit-input'; |
| 98 | + input.value = task.text; |
| 99 | + span.replaceWith(input); |
| 100 | + input.focus(); |
| 101 | + input.select(); |
| 102 | + |
| 103 | + function finish(saveText) { |
| 104 | + if (saveText) { |
| 105 | + const text = input.value.trim(); |
| 106 | + if (text) task.text = text; |
| 107 | + } |
| 108 | + save(); |
| 109 | + render(); |
| 110 | + } |
| 111 | + |
| 112 | + input.addEventListener('blur', () => finish(true)); |
| 113 | + input.addEventListener('keydown', (e) => { |
| 114 | + if (e.key === 'Enter') { |
| 115 | + e.preventDefault(); |
| 116 | + input.blur(); |
| 117 | + } else if (e.key === 'Escape') { |
| 118 | + finish(false); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + form.addEventListener('submit', (e) => { |
| 124 | + e.preventDefault(); |
| 125 | + const text = input.value.trim(); |
| 126 | + if (!text) return; |
| 127 | + const task = { id: uid(), text, completed: false }; |
| 128 | + tasks.unshift(task); |
| 129 | + save(); |
| 130 | + input.value = ''; |
| 131 | + render(); |
| 132 | + }); |
| 133 | + |
| 134 | + filters.forEach(btn => btn.addEventListener('click', () => { |
| 135 | + filters.forEach(b => b.classList.remove('active')); |
| 136 | + btn.classList.add('active'); |
| 137 | + filter = btn.dataset.filter; |
| 138 | + render(); |
| 139 | + })); |
| 140 | + |
| 141 | + clearCompletedBtn.addEventListener('click', () => { |
| 142 | + tasks = tasks.filter(t => !t.completed); |
| 143 | + save(); |
| 144 | + render(); |
| 145 | + }); |
| 146 | + |
| 147 | + // initial load |
| 148 | + load(); |
| 149 | + render(); |
| 150 | + |
| 151 | + // expose for debugging (optional) |
| 152 | + window.__todo = { get tasks() { return tasks; }, save }; |
| 153 | + |
| 154 | +})(); |
0 commit comments