-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (107 loc) · 3.77 KB
/
script.js
File metadata and controls
126 lines (107 loc) · 3.77 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
class ToDoList {
constructor() {
this.tasks = [];
this.nextId = 1;
this.init();
}
init() {
this.taskInput = document.getElementById('task-input');
this.addTaskBtn = document.getElementById('add-task-btn');
this.tasksContainer = document.getElementById('tasks-container');
this.emptyState = document.getElementById('empty-state');
this.totalCountEl = document.getElementById('total-count');
this.completedCountEl = document.getElementById('completed-count');
this.progressPercentageEl = document.getElementById('progress-percentage');
this.progressFillEl = document.getElementById('progress-fill');
this.attachEventListeners();
this.renderTasks();
this.updateStats();
}
attachEventListeners() {
this.addTaskBtn.addEventListener('click', () => this.addTask());
this.taskInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') this.addTask();
});
}
addTask() {
const taskText = this.taskInput.value.trim();
if (!taskText) return;
const task = {
id: this.nextId++,
text: taskText,
completed: false,
createdAt: new Date().toLocaleDateString(),
priority: Math.random() > 0.7 ? 'high' : 'normal'
};
this.tasks.push(task);
this.taskInput.value = '';
this.renderTasks();
this.updateStats();
}
toggleTask(id) {
const task = this.tasks.find(t => t.id === id);
if (task) {
task.completed = !task.completed;
this.renderTasks();
this.updateStats();
}
}
deleteTask(id) {
this.tasks = this.tasks.filter(t => t.id !== id);
this.renderTasks();
this.updateStats();
}
renderTasks() {
if (this.tasks.length === 0) {
this.emptyState.style.display = 'block';
this.tasksContainer.innerHTML = '';
return;
}
this.emptyState.style.display = 'none';
const tasksHTML = this.tasks.map((task, index) => `
<div class="task-item ${task.completed ? 'completed' : ''}" style="animation-delay:${index*100}ms">
<div class="task-checkbox ${task.completed ? 'completed' : ''}" onclick="app.toggleTask(${task.id})">
${task.completed ? '<span class="icon-checkmark"></span>' : ''}
</div>
<div class="task-content">
<div class="task-text ${task.completed ? 'completed' : ''}">${task.text}</div>
<div class="task-meta">
<span class="task-date">${task.createdAt}</span>
${task.priority === 'high' ? '<span class="priority-badge">High Priority</span>' : ''}
</div>
</div>
<button class="delete-btn" onclick="app.deleteTask(${task.id})">
<span class="icon-trash"></span>
</button>
</div>
`).join('');
this.tasksContainer.innerHTML = tasksHTML;
}
updateStats() {
const total = this.tasks.length;
const completed = this.tasks.filter(t => t.completed).length;
const progress = total ? Math.round((completed / total) * 100) : 0;
this.totalCountEl.textContent = total;
this.completedCountEl.textContent = completed;
this.progressPercentageEl.textContent = `${progress}%`;
this.progressFillEl.style.width = `${progress}%`;
}
}
const app = new ToDoList();
const body = document.body;
const themeToggleBtn = document.getElementById("theme-toggle");
if (body.getAttribute("data-theme") === "dark") {
themeToggleBtn.textContent = "🌙 Dark Mode";
} else {
themeToggleBtn.textContent = "☀️ Light Mode";
}
themeToggleBtn.addEventListener("click", () => {
const currentTheme = body.getAttribute("data-theme");
if (currentTheme === "dark") {
body.setAttribute("data-theme", "light");
themeToggleBtn.textContent = "☀️ Light Mode";
} else {
body.setAttribute("data-theme", "dark");
themeToggleBtn.textContent = "🌙 Dark Mode";
}
});