-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
471 lines (391 loc) · 12.5 KB
/
Copy pathscript.js
File metadata and controls
471 lines (391 loc) · 12.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* ============================================================
Smart To-Do List — script.js
Clean, modular JavaScript with full comments.
Features: CRUD, localStorage, filters, search, stats,
dark/light toggle, drag-and-drop reorder, priorities.
============================================================ */
// ==================== DOM REFERENCES ====================
const taskForm = document.getElementById("taskForm");
const taskInput = document.getElementById("taskInput");
const prioritySelect = document.getElementById("prioritySelect");
const validationMsg = document.getElementById("validationMsg");
const taskList = document.getElementById("taskList");
const searchInput = document.getElementById("searchInput");
const themeToggle = document.getElementById("themeToggle");
const emptyState = document.getElementById("emptyState");
// Stats elements
const totalCount = document.getElementById("totalCount");
const completedCount = document.getElementById("completedCount");
const pendingCount = document.getElementById("pendingCount");
// Filter buttons
const filterButtons = document.querySelectorAll("[data-filter]");
// ==================== STATE ====================
/** @type {'all'|'completed'|'pending'} Current active filter */
let currentFilter = "all";
/** @type {string} Current search query */
let searchQuery = "";
/** @type {number|null} ID of the task currently being dragged */
let draggedId = null;
// ==================== LOCAL STORAGE HELPERS ====================
/**
* Load tasks from localStorage.
* Each task: { id: number, text: string, completed: boolean, priority: 'high'|'medium'|'low' }
* @returns {Array} Array of task objects
*/
function loadTasks() {
try {
const data = localStorage.getItem("smartTodoTasks");
return data ? JSON.parse(data) : [];
} catch {
return [];
}
}
/**
* Save the tasks array to localStorage.
* @param {Array} tasks
*/
function saveTasks(tasks) {
localStorage.setItem("smartTodoTasks", JSON.stringify(tasks));
}
/**
* Load the saved theme preference, defaulting to 'dark'.
* @returns {'dark'|'light'}
*/
function loadTheme() {
return localStorage.getItem("smartTodoTheme") || "dark";
}
/**
* Save the current theme preference.
* @param {'dark'|'light'} theme
*/
function saveTheme(theme) {
localStorage.setItem("smartTodoTheme", theme);
}
// ==================== STATS ====================
/**
* Update the stats bar with current totals.
*/
function updateStats() {
const tasks = loadTasks();
const total = tasks.length;
const completed = tasks.filter((t) => t.completed).length;
const pending = total - completed;
// Animate number changes with a small scale pulse
animateNumber(totalCount, total);
animateNumber(completedCount, completed);
animateNumber(pendingCount, pending);
}
/**
* Smoothly update a stat number element.
* @param {HTMLElement} el
* @param {number} value
*/
function animateNumber(el, value) {
if (el.textContent !== String(value)) {
el.textContent = value;
el.style.transform = "scale(1.25)";
setTimeout(() => {
el.style.transition = "transform 0.3s";
el.style.transform = "scale(1)";
}, 50);
}
}
// ==================== RENDERING ====================
/**
* Render the visible task list based on the current filter and search query.
* Re-creates all <li> elements from the stored task array.
*/
function renderTasks() {
const tasks = loadTasks();
// Apply filter
let visible = tasks;
if (currentFilter === "completed") {
visible = tasks.filter((t) => t.completed);
} else if (currentFilter === "pending") {
visible = tasks.filter((t) => !t.completed);
}
// Apply search
if (searchQuery) {
const q = searchQuery.toLowerCase();
visible = visible.filter((t) => t.text.toLowerCase().includes(q));
}
// Clear existing list
taskList.innerHTML = "";
// Build each task item
visible.forEach((task) => {
const li = createTaskElement(task);
taskList.appendChild(li);
});
// Toggle empty state
emptyState.classList.toggle("visible", visible.length === 0);
// Update statistics
updateStats();
}
/**
* Create a single <li> element for a task.
* @param {Object} task - { id, text, completed, priority }
* @returns {HTMLLIElement}
*/
function createTaskElement(task) {
const li = document.createElement("li");
li.className = `task-item${task.completed ? " completed" : ""}`;
li.setAttribute("draggable", "true");
li.dataset.id = task.id;
li.innerHTML = `
<!-- Priority bar -->
<div class="task-item__priority task-item__priority--${task.priority}"></div>
<!-- Checkbox -->
<label class="task-item__check">
<input type="checkbox" ${task.completed ? "checked" : ""} aria-label="Mark complete" />
<span class="checkmark">
<svg 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"/></svg>
</span>
</label>
<!-- Task text -->
<span class="task-item__text">${escapeHTML(task.text)}</span>
<!-- Priority badge -->
<span class="task-item__badge task-item__badge--${task.priority}">${task.priority}</span>
<!-- Action buttons -->
<div class="task-item__actions">
<button class="btn-sm" title="Edit" aria-label="Edit task">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
</button>
<button class="btn-sm btn-sm--danger" title="Delete" aria-label="Delete task">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/><path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"/></svg>
</button>
</div>
`;
// --- Event Listeners ---
// Toggle complete
li.querySelector('input[type="checkbox"]').addEventListener("change", () => {
toggleComplete(task.id);
});
// Edit button
li.querySelector(".btn-sm:first-child").addEventListener("click", () => {
startEdit(li, task);
});
// Delete button
li.querySelector(".btn-sm--danger").addEventListener("click", () => {
deleteTask(task.id, li);
});
// Drag events
li.addEventListener("dragstart", handleDragStart);
li.addEventListener("dragend", handleDragEnd);
li.addEventListener("dragover", handleDragOver);
li.addEventListener("dragleave", handleDragLeave);
li.addEventListener("drop", handleDrop);
return li;
}
// ==================== CRUD OPERATIONS ====================
/**
* Add a new task from the form input.
* Validates that the input is not empty.
*/
function addTask(text, priority) {
const tasks = loadTasks();
const newTask = {
id: Date.now(), // unique timestamp ID
text: text,
completed: false,
priority: priority,
};
tasks.push(newTask);
saveTasks(tasks);
renderTasks();
}
/**
* Delete a task by its ID with a removal animation.
* @param {number} id
* @param {HTMLLIElement} li - the DOM element to animate
*/
function deleteTask(id, li) {
// Play exit animation, then remove from data
li.classList.add("removing");
li.addEventListener(
"animationend",
() => {
const tasks = loadTasks().filter((t) => t.id !== id);
saveTasks(tasks);
renderTasks();
},
{ once: true },
);
}
/**
* Toggle the completed status of a task.
* @param {number} id
*/
function toggleComplete(id) {
const tasks = loadTasks();
const task = tasks.find((t) => t.id === id);
if (task) {
task.completed = !task.completed;
saveTasks(tasks);
renderTasks();
}
}
/**
* Enter inline-edit mode for a task.
* Replaces the text span with an input field.
* @param {HTMLLIElement} li
* @param {Object} task
*/
function startEdit(li, task) {
const textSpan = li.querySelector(".task-item__text");
if (!textSpan) return;
// Create input pre-filled with current text
const input = document.createElement("input");
input.type = "text";
input.className = "task-item__edit-input";
input.value = task.text;
input.maxLength = 120;
textSpan.replaceWith(input);
input.focus();
input.select();
/** Save on Enter or blur */
const save = () => {
const newText = input.value.trim();
if (newText && newText !== task.text) {
const tasks = loadTasks();
const t = tasks.find((t) => t.id === task.id);
if (t) {
t.text = newText;
saveTasks(tasks);
}
}
renderTasks();
};
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") save();
if (e.key === "Escape") renderTasks(); // cancel
});
input.addEventListener("blur", save, { once: true });
}
// ==================== FILTERS & SEARCH ====================
/**
* Set the active filter and re-render.
* @param {'all'|'completed'|'pending'} filter
*/
function setFilter(filter) {
currentFilter = filter;
// Update active button styling
filterButtons.forEach((btn) => {
btn.classList.toggle("active", btn.dataset.filter === filter);
});
renderTasks();
}
// ==================== THEME TOGGLE ====================
/**
* Toggle between dark and light themes.
* Persists choice in localStorage.
*/
function toggleTheme() {
const html = document.documentElement;
const current = html.getAttribute("data-theme");
const next = current === "dark" ? "light" : "dark";
html.setAttribute("data-theme", next);
saveTheme(next);
}
// ==================== DRAG & DROP ====================
/**
* Handle drag start — store the dragged task's ID.
* @param {DragEvent} e
*/
function handleDragStart(e) {
draggedId = Number(this.dataset.id);
this.classList.add("dragging");
e.dataTransfer.effectAllowed = "move";
}
/** Remove dragging style when drag ends. */
function handleDragEnd() {
this.classList.remove("dragging");
draggedId = null;
// Remove all drag-over classes
document
.querySelectorAll(".drag-over")
.forEach((el) => el.classList.remove("drag-over"));
}
/** Allow drop and show visual cue. */
function handleDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
this.classList.add("drag-over");
}
/** Remove visual cue when dragging leaves. */
function handleDragLeave() {
this.classList.remove("drag-over");
}
/**
* Handle drop — reorder the tasks array and re-render.
* @param {DragEvent} e
*/
function handleDrop(e) {
e.preventDefault();
this.classList.remove("drag-over");
const droppedOnId = Number(this.dataset.id);
if (draggedId === null || draggedId === droppedOnId) return;
const tasks = loadTasks();
const fromIndex = tasks.findIndex((t) => t.id === draggedId);
const toIndex = tasks.findIndex((t) => t.id === droppedOnId);
if (fromIndex === -1 || toIndex === -1) return;
// Remove dragged item and insert at new position
const [moved] = tasks.splice(fromIndex, 1);
tasks.splice(toIndex, 0, moved);
saveTasks(tasks);
renderTasks();
}
// ==================== UTILITY ====================
/**
* Escape HTML special characters to prevent XSS.
* @param {string} str
* @returns {string}
*/
function escapeHTML(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
// ==================== EVENT BINDINGS ====================
// Form submission — add new task
taskForm.addEventListener("submit", (e) => {
e.preventDefault();
const text = taskInput.value.trim();
// Input validation
if (!text) {
validationMsg.textContent = "⚠️ Please enter a task description.";
taskInput.classList.add("shake");
taskInput.addEventListener(
"animationend",
() => taskInput.classList.remove("shake"),
{ once: true },
);
return;
}
validationMsg.textContent = "";
addTask(text, prioritySelect.value);
taskInput.value = "";
taskInput.focus();
});
// Live search
searchInput.addEventListener("input", (e) => {
searchQuery = e.target.value.trim();
renderTasks();
});
// Filter buttons
filterButtons.forEach((btn) => {
btn.addEventListener("click", () => setFilter(btn.dataset.filter));
});
// Theme toggle
themeToggle.addEventListener("click", toggleTheme);
// ==================== INITIALISATION ====================
/**
* Boot the app: apply saved theme, then render tasks.
*/
function init() {
// Apply persisted theme
document.documentElement.setAttribute("data-theme", loadTheme());
// Render stored tasks
renderTasks();
}
// Run on DOM ready
init();