Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (108 loc) · 4.26 KB
/
Copy pathscript.js
File metadata and controls
128 lines (108 loc) · 4.26 KB
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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('todo-form');
const input = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
const filterBtns = document.querySelectorAll('.filter-btn');
let todos = JSON.parse(localStorage.getItem('todos')) || [];
let currentFilter = 'all';
// Check SVG icon for completed tasks
const checkIcon = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>`;
// Trash SVG icon for delete button
const trashIcon = `<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"></path><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>`;
function saveTodos() {
localStorage.setItem('todos', JSON.stringify(todos));
}
function renderTodos() {
todoList.innerHTML = '';
let filteredTodos = todos;
if (currentFilter === 'active') {
filteredTodos = todos.filter(t => !t.completed);
} else if (currentFilter === 'completed') {
filteredTodos = todos.filter(t => t.completed);
}
if (filteredTodos.length === 0) {
todoList.innerHTML = `
<div class="empty-state">
<p>${todos.length === 0 ? "You have no tasks yet." : "No tasks found in this view."}</p>
</div>
`;
return;
}
filteredTodos.forEach(todo => {
const li = document.createElement('li');
li.className = `todo-item ${todo.completed ? 'completed' : ''}`;
li.dataset.id = todo.id;
li.innerHTML = `
<div class="checkbox">
${checkIcon}
</div>
<span class="todo-text">${escapeHTML(todo.text)}</span>
<button class="delete-btn" aria-label="Delete task">
${trashIcon}
</button>
`;
// Toggle completion
const checkbox = li.querySelector('.checkbox');
checkbox.addEventListener('click', () => toggleTodo(todo.id));
// Delete task
const deleteBtn = li.querySelector('.delete-btn');
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation();
deleteTodo(todo.id, li);
});
todoList.appendChild(li);
});
}
function addTodo(text) {
const newTodo = {
id: Date.now().toString(),
text,
completed: false
};
todos.unshift(newTodo);
saveTodos();
renderTodos();
}
function toggleTodo(id) {
todos = todos.map(t =>
t.id === id ? { ...t, completed: !t.completed } : t
);
saveTodos();
renderTodos();
}
function deleteTodo(id, element) {
// Add fade out animation before deleting
element.style.animation = 'fadeOut 0.3s ease forwards';
setTimeout(() => {
todos = todos.filter(t => t.id !== id);
saveTodos();
renderTodos();
}, 300);
}
// Set filter
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
filterBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentFilter = btn.dataset.filter;
renderTodos();
});
});
// Handle form submit
form.addEventListener('submit', (e) => {
e.preventDefault();
const text = input.value.trim();
if (text) {
addTodo(text);
input.value = '';
}
});
// Utility to prevent HTML injection securely
function escapeHTML(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
// Initial render
renderTodos();
});