-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
217 lines (186 loc) · 5.23 KB
/
app.js
File metadata and controls
217 lines (186 loc) · 5.23 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
// DOM 요소 선택
const todoInput = document.getElementById('todoInput');
const addBtn = document.getElementById('addBtn');
const todoList = document.getElementById('todoList');
const todoCount = document.getElementById('todoCount');
const clearCompleted = document.getElementById('clearCompleted');
const filterBtns = document.querySelectorAll('.filter-btn');
const currentDate = document.getElementById('currentDate');
// 상태 관리
let todos = [];
let currentFilter = 'all';
// 로컬스토리지 키
const STORAGE_KEY = 'todos';
// 초기화
function init() {
displayCurrentDate();
loadTodos();
renderTodos();
updateCount();
}
// 현재 날짜 표시
function displayCurrentDate() {
const now = new Date();
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long'
};
currentDate.textContent = now.toLocaleDateString('ko-KR', options);
}
// 로컬스토리지에서 할 일 불러오기
function loadTodos() {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
try {
todos = JSON.parse(stored);
} catch (e) {
console.error('할 일 불러오기 실패:', e);
todos = [];
}
}
}
// 로컬스토리지에 할 일 저장
function saveTodos() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
} catch (e) {
console.error('할 일 저장 실패:', e);
alert('저장 공간이 부족합니다. 완료된 항목을 삭제해주세요.');
}
}
// 할 일 추가
function addTodo() {
const text = todoInput.value.trim();
if (text === '') {
alert('할 일을 입력해주세요.');
return;
}
const newTodo = {
id: Date.now(),
text: text,
completed: false,
createdAt: new Date().toISOString()
};
todos.unshift(newTodo);
saveTodos();
renderTodos();
updateCount();
todoInput.value = '';
todoInput.focus();
}
// 할 일 토글 (완료/미완료)
function toggleTodo(id) {
const todo = todos.find(t => t.id === id);
if (todo) {
todo.completed = !todo.completed;
saveTodos();
renderTodos();
updateCount();
}
}
// 할 일 삭제
function deleteTodo(id) {
todos = todos.filter(t => t.id !== id);
saveTodos();
renderTodos();
updateCount();
}
// 완료된 할 일 모두 삭제
function clearCompletedTodos() {
const completedCount = todos.filter(t => t.completed).length;
if (completedCount === 0) {
alert('완료된 항목이 없습니다.');
return;
}
if (confirm(`${completedCount}개의 완료된 항목을 삭제하시겠습니까?`)) {
todos = todos.filter(t => !t.completed);
saveTodos();
renderTodos();
updateCount();
}
}
// 필터링된 할 일 가져오기
function getFilteredTodos() {
switch (currentFilter) {
case 'active':
return todos.filter(t => !t.completed);
case 'completed':
return todos.filter(t => t.completed);
default:
return todos;
}
}
// 할 일 렌더링
function renderTodos() {
const filteredTodos = getFilteredTodos();
if (filteredTodos.length === 0) {
todoList.innerHTML = `
<div class="empty-state">
<p>${getEmptyMessage()}</p>
</div>
`;
return;
}
todoList.innerHTML = filteredTodos.map(todo => `
<li class="todo-item ${todo.completed ? 'completed' : ''}" data-id="${todo.id}">
<input
type="checkbox"
class="todo-checkbox"
${todo.completed ? 'checked' : ''}
onchange="toggleTodo(${todo.id})"
>
<span class="todo-text">${escapeHtml(todo.text)}</span>
<button class="delete-btn" onclick="deleteTodo(${todo.id})">삭제</button>
</li>
`).join('');
}
// 빈 상태 메시지
function getEmptyMessage() {
switch (currentFilter) {
case 'active':
return '진행중인 할 일이 없습니다.';
case 'completed':
return '완료된 할 일이 없습니다.';
default:
return '할 일을 추가해보세요!';
}
}
// HTML 이스케이프 (XSS 방지)
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 할 일 개수 업데이트
function updateCount() {
const activeCount = todos.filter(t => !t.completed).length;
todoCount.textContent = `${activeCount}개의 할 일`;
}
// 필터 변경
function setFilter(filter) {
currentFilter = filter;
filterBtns.forEach(btn => {
btn.classList.remove('active');
if (btn.dataset.filter === filter) {
btn.classList.add('active');
}
});
renderTodos();
}
// 이벤트 리스너
addBtn.addEventListener('click', addTodo);
todoInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addTodo();
}
});
clearCompleted.addEventListener('click', clearCompletedTodos);
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
setFilter(btn.dataset.filter);
});
});
// 앱 초기화
init();