-
Notifications
You must be signed in to change notification settings - Fork 0
/
JY_Project2.js
218 lines (182 loc) · 6.96 KB
/
JY_Project2.js
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
const todoInputElem = document.querySelector('.todo-input');
const todoListElem = document.querySelector('.todo-list');
const completeAllBtnElem = document.querySelector('.complete-all-btn');
const leftItemsElem = document.querySelector('.left-items')
const showAllBtnElem = document.querySelector('.show-all-btn');
const showActiveBtnElem = document.querySelector('.show-active-btn');
const showCompletedBtnElem = document.querySelector('.show-completed-btn');
const clearCompletedBtnElem = document.querySelector('.clear-completed-btn');
let id = 0;
const setId = (newId) => {id = newId};
let isAllCompleted = false; // 전체 todos 체크 여부
const setIsAllCompleted = (bool) => { isAllCompleted = bool};
let currentShowType = 'all'; // all | active | complete
const setCurrentShowType = (newShowType) => currentShowType = newShowType
let todos = [];
const setTodos = (newTodos) => {
todos = newTodos;
}
const getAllTodos = () => {
return todos;
}
const getCompletedTodos = () => {
return todos.filter(todo => todo.isCompleted === true );
}
const getActiveTodos = () => {
return todos.filter(todo => todo.isCompleted === false);
}
const setLeftItems = () => {
const leftTodos = getActiveTodos()
leftItemsElem.innerHTML = `${leftTodos.length} items left`
}
const completeAll = () => {
completeAllBtnElem.classList.add('checked');
const newTodos = getAllTodos().map(todo => ({...todo, isCompleted: true }) )
setTodos(newTodos)
}
const incompleteAll = () => {
completeAllBtnElem.classList.remove('checked');
const newTodos = getAllTodos().map(todo => ({...todo, isCompleted: false }) );
setTodos(newTodos)
}
// 전체 todos의 check 여부 (isCompleted)
const checkIsAllCompleted = () => {
if(getAllTodos().length === getCompletedTodos().length ){
setIsAllCompleted(true);
completeAllBtnElem.classList.add('checked');
}else {
setIsAllCompleted(false);
completeAllBtnElem.classList.remove('checked');
}
}
const onClickCompleteAll = () => {
if(!getAllTodos().length) return; // todos배열의 길이가 0이면 return;
if(isAllCompleted) incompleteAll(); // isAllCompleted가 true이면 todos를 전체 미완료 처리
else completeAll(); // isAllCompleted가 false이면 todos를 전체 완료 처리
setIsAllCompleted(!isAllCompleted); // isAllCompleted 토글
paintTodos(); // 새로운 todos를 렌더링
setLeftItems()
}
const appendTodos = (text) => {
const newId = id + 1; // 기존에 i++ 로 작성했던 부분을 setId()를 통해 id값을 갱신하였다.
setId(newId)
const newTodos = getAllTodos().concat({id: newId, isCompleted: false, content: text })
// const newTodos = [...getAllTodos(), {id: newId, isCompleted: false, content: text }]
setTodos(newTodos)
setLeftItems()
checkIsAllCompleted();
paintTodos();
}
const deleteTodo = (todoId) => {
const newTodos = getAllTodos().filter(todo => todo.id !== todoId );
setTodos(newTodos);
setLeftItems()
paintTodos()
}
const completeTodo = (todoId) => {
const newTodos = getAllTodos().map(todo => todo.id === todoId ? {...todo, isCompleted: !todo.isCompleted} : todo )
setTodos(newTodos);
paintTodos();
setLeftItems()
checkIsAllCompleted();
}
const updateTodo = (text, todoId) => {
const currentTodos = getAllTodos();
const newTodos = currentTodos.map(todo => todo.id === todoId ? ({...todo, content: text}) : todo);
setTodos(newTodos);
paintTodos();
}
const onDbclickTodo = (e, todoId) => {
const todoElem = e.target;
const inputText = e.target.innerText;
const todoItemElem = todoElem.parentNode;
const inputElem = document.createElement('input');
inputElem.value = inputText;
inputElem.classList.add('edit-input');
inputElem.addEventListener('keypress', (e)=>{
if(e.key === 'Enter') {
updateTodo(e.target.value, todoId);
document.body.removeEventListener('click', onClickBody );
}
})
const onClickBody = (e) => {
if(e.target !== inputElem) {
todoItemElem.removeChild(inputElem);
document.body.removeEventListener('click', onClickBody );
}
}
document.body.addEventListener('click', onClickBody)
todoItemElem.appendChild(inputElem);
}
const clearCompletedTodos = () => {
const newTodos = getActiveTodos()
setTodos(newTodos)
paintTodos();
}
const paintTodo = (todo) => {
const todoItemElem = document.createElement('li');
todoItemElem.classList.add('todo-item');
todoItemElem.setAttribute('data-id', todo.id );
const checkboxElem = document.createElement('div');
checkboxElem.classList.add('checkbox');
checkboxElem.addEventListener('click', () => completeTodo(todo.id))
const todoElem = document.createElement('div');
todoElem.classList.add('todo');
todoElem.addEventListener('dblclick', (event) => onDbclickTodo(event, todo.id))
todoElem.innerText = todo.content;
const delBtnElem = document.createElement('button');
delBtnElem.classList.add('delBtn');
delBtnElem.addEventListener('click', () => deleteTodo(todo.id))
delBtnElem.innerHTML = 'X';
if(todo.isCompleted) {
todoItemElem.classList.add('checked');
checkboxElem.innerText = '✔';
}
todoItemElem.appendChild(checkboxElem);
todoItemElem.appendChild(todoElem);
todoItemElem.appendChild(delBtnElem);
todoListElem.appendChild(todoItemElem);
}
const paintTodos = () => {
todoListElem.innerHTML = null;
switch (currentShowType) {
case 'all':
const allTodos = getAllTodos();
allTodos.forEach(todo => { paintTodo(todo);});
break;
case 'active':
const activeTodos = getActiveTodos();
activeTodos.forEach(todo => { paintTodo(todo);});
break;
case 'completed':
const completedTodos = getCompletedTodos();
completedTodos.forEach(todo => { paintTodo(todo);});
break;
default:
break;
}
}
const onClickShowTodosType = (e) => {
const currentBtnElem = e.target;
const newShowType = currentBtnElem.dataset.type;
if ( currentShowType === newShowType ) return;
const preBtnElem = document.querySelector(`.show-${currentShowType}-btn`);
preBtnElem.classList.remove('selected');
currentBtnElem.classList.add('selected')
setCurrentShowType(newShowType)
paintTodos();
}
const init = () => {
todoInputElem.addEventListener('keypress', (e) =>{
if( e.key === 'Enter' ){
appendTodos(e.target.value); todoInputElem.value ='';
}
})
completeAllBtnElem.addEventListener('click', onClickCompleteAll);
showAllBtnElem.addEventListener('click', onClickShowTodosType);
showActiveBtnElem.addEventListener('click',onClickShowTodosType);
showCompletedBtnElem.addEventListener('click',onClickShowTodosType);
clearCompletedBtnElem.addEventListener('click', clearCompletedTodos);
setLeftItems()
}
init()