-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
194 lines (166 loc) · 6.37 KB
/
script.js
File metadata and controls
194 lines (166 loc) · 6.37 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const STORAGE_KEY = 'taskflow-todos';
const THEME_KEY = 'taskflow-theme';
let todos = [];
let currentFilter = 'all';
let currentStatus = 'all';
let searchQuery = '';
let selectedPriority = 'low';
function loadTheme() {
const savedTheme = localStorage.getItem(THEME_KEY);
if (savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.body.classList.add('dark');
}
}
function toggleTheme() {
document.body.classList.toggle('dark');
localStorage.setItem(THEME_KEY, document.body.classList.contains('dark') ? 'dark' : 'light');
}
function loadTodos() {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
} catch { return []; }
}
function saveTodos() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
}
function generateId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function formatDate(dateString) {
if (!dateString) return '';
return new Date(dateString).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
}
function isOverdue(dateString) {
if (!dateString) return false;
const today = new Date();
today.setHours(0, 0, 0, 0);
return new Date(dateString) < today;
}
function getCategoryEmoji(cat) {
const emojis = { work: '💼', personal: '🏠', shopping: '🛒', health: '💪' };
return emojis[cat] || '📌';
}
function addTodo(text, dueDate, category, priority) {
todos.unshift({
id: generateId(),
text,
completed: false,
dueDate: dueDate || null,
category: category || null,
priority: priority || 'low',
createdAt: Date.now()
});
saveTodos();
render();
}
function toggleTodo(id) {
todos = todos.map(t => t.id === id ? { ...t, completed: !t.completed } : t);
saveTodos();
render();
}
function deleteTodo(id) {
todos = todos.filter(t => t.id !== id);
saveTodos();
render();
}
function createTodoElement(todo) {
const li = document.createElement('li');
li.className = `todo-item priority-${todo.priority}${todo.completed ? ' completed' : ''}`;
let metaHtml = '';
if (todo.category) {
metaHtml += `<span class="meta-badge category">${getCategoryEmoji(todo.category)} ${todo.category}</span>`;
}
if (todo.dueDate) {
const overdueClass = !todo.completed && isOverdue(todo.dueDate) ? ' overdue' : '';
metaHtml += `<span class="meta-badge date${overdueClass}">📅 ${formatDate(todo.dueDate)}</span>`;
}
li.innerHTML = `
<button class="check-btn" aria-label="${todo.completed ? 'Mark incomplete' : 'Mark complete'}"></button>
<div class="todo-content">
<span class="todo-text">${escapeHtml(todo.text)}</span>
${metaHtml ? `<div class="todo-meta">${metaHtml}</div>` : ''}
</div>
<button class="delete-btn" aria-label="Delete">×</button>
`;
li.querySelector('.check-btn').onclick = () => toggleTodo(todo.id);
li.querySelector('.delete-btn').onclick = () => deleteTodo(todo.id);
return li;
}
function getFilteredTodos() {
return todos.filter(todo => {
const statusMatch = currentStatus === 'all' || (currentStatus === 'active' ? !todo.completed : todo.completed);
const categoryMatch = currentFilter === 'all' || todo.category === currentFilter;
const searchMatch = !searchQuery.trim() || todo.text.toLowerCase().includes(searchQuery.toLowerCase());
return statusMatch && categoryMatch && searchMatch;
});
}
function updateCounts() {
document.getElementById('allCount').textContent = todos.length;
document.getElementById('activeCount').textContent = todos.filter(t => !t.completed).length;
document.getElementById('completedCount').textContent = todos.filter(t => t.completed).length;
}
function render() {
const list = document.getElementById('todoList');
const filtered = getFilteredTodos();
const completedCount = todos.filter(t => t.completed).length;
const totalCount = todos.length;
const percent = totalCount > 0 ? Math.round((completedCount / totalCount) * 100) : 0;
document.getElementById('progressText').textContent = `${completedCount} of ${totalCount} completed`;
document.getElementById('progressPercent').textContent = `${percent}%`;
document.getElementById('progressFill').style.width = `${percent}%`;
updateCounts();
list.innerHTML = '';
if (todos.length === 0) {
list.innerHTML = `<li class="empty-state"><div class="empty-icon">📝</div><p>No tasks yet. Add one above!</p></li>`;
} else if (filtered.length === 0) {
list.innerHTML = '<li class="no-results">No matching tasks found</li>';
} else {
filtered.forEach(todo => list.appendChild(createTodoElement(todo)));
}
}
// Event Listeners
document.getElementById('todoForm').addEventListener('submit', e => {
e.preventDefault();
const input = document.getElementById('todoInput');
const text = input.value.trim();
if (text) {
addTodo(text, document.getElementById('dueDate').value, document.getElementById('category').value, selectedPriority);
input.value = '';
document.getElementById('dueDate').value = '';
document.getElementById('category').value = '';
}
});
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
document.getElementById('searchInput').addEventListener('input', e => { searchQuery = e.target.value; render(); });
document.querySelectorAll('.priority-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.priority-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
selectedPriority = btn.dataset.priority;
});
});
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentStatus = btn.dataset.status;
render();
});
});
document.querySelectorAll('.chip').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.chip').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentFilter = btn.dataset.filter;
render();
});
});
// Init
loadTheme();
todos = loadTodos();
render();