-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathscript.js
More file actions
245 lines (213 loc) · 8.81 KB
/
script.js
File metadata and controls
245 lines (213 loc) · 8.81 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class TodoApp {
constructor() {
this.todos = JSON.parse(localStorage.getItem('todos')) || [];
this.filteredTodos = [...this.todos];
this.currentFilter = 'all';
this.searchQuery = '';
this.initializeElements();
this.bindEvents();
this.renderTodos();
this.updateStats();
}
initializeElements() {
this.todoInput = document.getElementById('todoInput');
this.addBtn = document.getElementById('addBtn');
this.todoList = document.getElementById('todoList');
this.searchInput = document.getElementById('searchInput');
this.prioritySelect = document.getElementById('prioritySelect');
this.categorySelect = document.getElementById('categorySelect');
this.dueDateInput = document.getElementById('dueDateInput');
this.filterBtns = document.querySelectorAll('.filter-btn');
}
bindEvents() {
this.addBtn.addEventListener('click', () => this.addTodo());
this.todoInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') this.addTodo();
});
this.searchInput.addEventListener('input', (e) => {
this.searchQuery = e.target.value.toLowerCase();
this.filterTodos();
});
this.filterBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
this.setActiveFilter(e.target.dataset.filter);
});
});
// Set default due date to tomorrow
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
this.dueDateInput.value = tomorrow.toISOString().split('T')[0];
}
setActiveFilter(filter) {
this.currentFilter = filter;
this.filterBtns.forEach(btn => {
btn.classList.toggle('active', btn.dataset.filter === filter);
});
this.filterTodos();
}
filterTodos() {
this.filteredTodos = this.todos.filter(todo => {
const matchesSearch = todo.text.toLowerCase().includes(this.searchQuery);
const matchesFilter = this.currentFilter === 'all' ||
(this.currentFilter === 'pending' && !todo.completed) ||
(this.currentFilter === 'completed' && todo.completed);
return matchesSearch && matchesFilter;
});
this.renderTodos();
}
addTodo() {
const todoText = this.todoInput.value.trim();
if (todoText === '') return;
const todo = {
id: Date.now(),
text: todoText,
completed: false,
priority: this.prioritySelect.value,
category: this.categorySelect.value,
dueDate: this.dueDateInput.value,
createdAt: new Date().toISOString()
};
this.todos.push(todo);
this.saveTodos();
this.filterTodos();
this.updateStats();
this.todoInput.value = '';
this.todoInput.focus();
}
toggleTodo(id) {
const todo = this.todos.find(t => t.id === id);
if (todo) {
todo.completed = !todo.completed;
this.saveTodos();
this.filterTodos();
this.updateStats();
}
}
editTodo(id) {
const todo = this.todos.find(t => t.id === id);
if (!todo) return;
const newText = prompt('Edit todo:', todo.text);
if (newText !== null && newText.trim() !== '') {
todo.text = newText.trim();
this.saveTodos();
this.filterTodos();
}
}
deleteTodo(id) {
if (confirm('Are you sure you want to delete this todo?')) {
this.todos = this.todos.filter(t => t.id !== id);
this.saveTodos();
this.filterTodos();
this.updateStats();
}
}
saveTodos() {
localStorage.setItem('todos', JSON.stringify(this.todos));
}
updateStats() {
const total = this.todos.length;
const completed = this.todos.filter(t => t.completed).length;
const pending = total - completed;
document.getElementById('totalCount').textContent = total;
document.getElementById('completedCount').textContent = completed;
document.getElementById('pendingCount').textContent = pending;
}
renderTodos() {
this.todoList.innerHTML = '';
if (this.filteredTodos.length === 0) {
const emptyState = this.searchQuery || this.currentFilter !== 'all'
? 'No todos match your search/filter criteria.'
: 'No todos yet. Add one above!';
this.todoList.innerHTML = `
<div class="empty-state">
<i class="fas fa-clipboard-list"></i>
<p>${emptyState}</p>
</div>
`;
return;
}
this.filteredTodos.forEach(todo => {
const li = document.createElement('li');
li.className = `todo-item ${todo.completed ? 'completed' : ''}`;
const dueDate = new Date(todo.dueDate);
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const isOverdue = !todo.completed && dueDate < today && dueDate.toDateString() !== today.toDateString();
li.innerHTML = `
<div class="todo-header">
<span class="todo-text">${this.escapeHtml(todo.text)}</span>
</div>
<div class="todo-meta">
<span class="meta-item">
<i class="fas fa-flag"></i>
<span class="priority-badge priority-${todo.priority}">${todo.priority}</span>
</span>
<span class="meta-item">
<i class="fas fa-tag"></i>
<span class="category-badge">${todo.category}</span>
</span>
<span class="meta-item ${isOverdue ? 'overdue' : ''}">
<i class="fas fa-calendar"></i>
<span>${this.formatDate(todo.dueDate)}</span>
${isOverdue ? ' <i class="fas fa-exclamation-triangle" style="color: #ef4444;"></i>' : ''}
</span>
<span class="meta-item">
<i class="fas fa-clock"></i>
<span>${this.formatRelativeTime(todo.createdAt)}</span>
</span>
</div>
<div class="todo-actions">
<button class="complete-btn" onclick="todoApp.toggleTodo(${todo.id})">
<i class="fas ${todo.completed ? 'fa-undo' : 'fa-check'}"></i>
${todo.completed ? 'Undo' : 'Complete'}
</button>
<button class="edit-btn" onclick="todoApp.editTodo(${todo.id})">
<i class="fas fa-edit"></i>
Edit
</button>
<button class="delete-btn" onclick="todoApp.deleteTodo(${todo.id})">
<i class="fas fa-trash"></i>
Delete
</button>
</div>
`;
this.todoList.appendChild(li);
});
}
formatDate(dateString) {
const date = new Date(dateString);
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
if (date.toDateString() === today.toDateString()) {
return 'Today';
} else if (date.toDateString() === tomorrow.toDateString()) {
return 'Tomorrow';
} else {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: date.getFullYear() !== today.getFullYear() ? 'numeric' : undefined
});
}
}
formatRelativeTime(dateString) {
const date = new Date(dateString);
const now = new Date();
const diffInMinutes = Math.floor((now - date) / (1000 * 60));
if (diffInMinutes < 1) return 'Just now';
if (diffInMinutes < 60) return `${diffInMinutes}m ago`;
if (diffInMinutes < 1440) return `${Math.floor(diffInMinutes / 60)}h ago`;
return `${Math.floor(diffInMinutes / 1440)}d ago`;
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
// Initialize the app when the page loads
document.addEventListener('DOMContentLoaded', () => {
window.todoApp = new TodoApp();
});