-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (111 loc) · 4.09 KB
/
Copy pathscript.js
File metadata and controls
131 lines (111 loc) · 4.09 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
class TodoApp {
constructor() {
this.tasks = [];
this.elements = {
form: document.getElementById('todo-form'),
input: document.getElementById('task-input'),
priority: document.getElementById('priority'),
taskList: document.getElementById('task-list'),
themeSelect: document.getElementById('theme-select'),
clearAll: document.getElementById('clear-all'),
totalTasks: document.getElementById('total-tasks'),
completedTasks: document.getElementById('completed-tasks')
};
this.init();
}
init() {
this.loadState();
this.bindEvents();
this.updateStats();
}
bindEvents() {
this.elements.form.addEventListener('submit', (e) => this.handleSubmit(e));
this.elements.themeSelect.addEventListener('change', () => this.changeTheme());
this.elements.clearAll.addEventListener('click', () => this.clearAll());
}
handleSubmit(e) {
e.preventDefault();
const text = this.elements.input.value.trim();
const priority = this.elements.priority.value;
if (!text) return;
const task = {
id: Date.now(),
text,
priority,
completed: false,
createdAt: new Date().toISOString()
};
this.tasks.push(task);
this.renderTask(task);
this.saveState();
this.elements.input.value = '';
this.updateStats();
}
renderTask(task) {
const li = document.createElement('li');
li.className = `task-item ${task.completed ? 'completed' : ''}`;
li.dataset.id = task.id;
li.innerHTML = `
<span class="priority-indicator priority-${task.priority}"></span>
<span class="task-text">${task.text}</span>
<button class="toggle-btn">${task.completed ? 'Undo' : 'Done'}</button>
<button class="delete-btn">Delete</button>
`;
li.querySelector('.toggle-btn').addEventListener('click', () => this.toggleTask(task.id));
li.querySelector('.delete-btn').addEventListener('click', () => this.deleteTask(task.id));
this.elements.taskList.appendChild(li);
}
toggleTask(id) {
const task = this.tasks.find(t => t.id === id);
if (task) {
task.completed = !task.completed;
this.refreshTasks();
this.saveState();
this.updateStats();
}
}
deleteTask(id) {
this.tasks = this.tasks.filter(t => t.id !== id);
this.refreshTasks();
this.saveState();
this.updateStats();
}
clearAll() {
if (confirm('Are you sure you want to clear all tasks?')) {
this.tasks = [];
this.refreshTasks();
this.saveState();
this.updateStats();
}
}
refreshTasks() {
this.elements.taskList.innerHTML = '';
this.tasks.sort((a, b) => {
const priorityOrder = { high: 3, medium: 2, low: 1 };
return priorityOrder[b.priority] - priorityOrder[a.priority];
}).forEach(task => this.renderTask(task));
}
updateStats() {
const total = this.tasks.length;
const completed = this.tasks.filter(t => t.completed).length;
this.elements.totalTasks.textContent = total;
this.elements.completedTasks.textContent = completed;
}
changeTheme() {
const theme = this.elements.themeSelect.value;
document.body.className = theme;
localStorage.setItem('theme', theme);
}
saveState() {
localStorage.setItem('tasks', JSON.stringify(this.tasks));
localStorage.setItem('theme', this.elements.themeSelect.value);
}
loadState() {
this.tasks = JSON.parse(localStorage.getItem('tasks') || '[]');
const theme = localStorage.getItem('theme') || 'default';
this.elements.themeSelect.value = theme;
document.body.className = theme;
this.refreshTasks();
}
}
document.addEventListener('DOMContentLoaded', () => new TodoApp());