-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
270 lines (217 loc) · 7.47 KB
/
index.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
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
const app = document.querySelector('#app');
const createElement = (elName, elClass, elContent, attrArr) => {
const element = document.createElement(elName);
if (elClass) {
if (Array.isArray(elClass)) {
element.classList.add(...elClass);
} else {
element.classList.add(elClass);
}
}
if (elContent) {
element.textContent = elContent;
}
if (attrArr) {
attrArr.forEach((attrObj) => {
for (let attrName in attrObj) {
element.setAttribute(attrName, attrObj[attrName]);
}
});
}
return element;
};
const createTodoTemplate = () => {
// _todo body
const todoBody = createElement('div', 'todo');
// _todo body-template-container
const todoBodyTemplate = createElement('div', 'todo__template');
// _todo heading
const todoHeading = createElement('h1', 'todo__heading', 'todo list');
// _todo form
const form = createElement('form', 'todo__form');
// _todo-form-input
const formInput = createElement('input', 'todo__form-input', '', [{ placeholder: 'Add new todo...' }, { type: 'text' }]);
// _todo-form-button
const formButton = createElement('button', 'todo__form-button', 'submit');
// append elements to form
form.append(...[formInput, formButton]);
// append elements to todo template and template to main container
todoBodyTemplate.append(...[todoHeading, form]);
todoBody.append(todoBodyTemplate);
// call function for processing form
processForm(formInput, formButton);
return todoBody;
};
let todosArray = [];
const processForm = (input, button) => {
let todoName = '';
input.addEventListener('change', (e) => {
input.classList.remove('todo__form-input--error');
todoName = e.target.value.trim();
});
button.addEventListener('click', (e) => {
e.preventDefault();
if (!todoName) {
input.classList.add('todo__form-input--error');
}
if (todoName) {
input.classList.remove('todo__form-input--error');
const todoId = todosArray.length + 1;
const todoObj = {
name: todoName,
active: true,
id: todoId,
};
if (!todoInfoBar.children.length) {
createInfoBarElements();
}
todosArray.push(todoObj);
renderTodos(todosArray);
}
input.value = '';
todoName = '';
});
};
const createTodoInfoBarContainer = () => {
// _todo info bar
const todoInfoBar = createElement('div', 'todo__info-bar');
return todoInfoBar;
};
const createInfoBarElements = () => {
// _todos counter at info bar
const todoItemCounter = createElement('p', 'todo__item-counter', 'Active todos: 1');
// remove all todo btn
const removeAllButton = createElement('button', 'todo__remove-all', 'Remove all');
removeAllButton.addEventListener('click', removeAllTodo);
// append 'counter' and 'remove todo button' to info bar
todoInfoBar.append(...[todoItemCounter, removeAllButton]);
};
const createTodosContainer = () => {
const todosContaienr = createElement('div', 'todo__items-container');
return todosContaienr;
};
const todoBody = createTodoTemplate();
const todoContainer = createTodosContainer();
const todoInfoBar = createTodoInfoBarContainer();
function removeAllTodo() {
todosArray = [];
[...todoInfoBar.children].forEach((element) => element.remove());
renderTodos(todosArray);
}
const changeTodoCounter = (num) => {
const counterItem = document.querySelector('.todo__item-counter');
if (counterItem) {
counterItem.textContent = `Active todos: ${num}`;
}
};
const countActiveTodos = (todosArr) => {
const activeTodos = todosArr.filter((todo) => todo.active);
return activeTodos.length;
};
const renderTodos = (todos) => {
[...todoContainer.children].forEach((todo) => todo.remove());
todos.forEach((todo) => createTodoElement(todo));
};
const createTodoElement = (todo) => {
//render todo
const todoWrapper = createElement('div', 'todo__element-wrapper', '', [{ id: todo.id }]);
// create input and its attrs
const todoItem = createElement('input', 'todo__element', '', [{ name: todo.name, disabled: true }]);
todoItem.value = todo.name;
if (!todo.active) {
todoItem.classList.add('todo__element--done');
}
// add toggle todo status listener for todo wrapper
todoWrapper.addEventListener('click', (e) => toggleTodoStatus(e.target, todo.id, todoItem.disabled));
// create edit button
const editButton = createElement('span', ['todo__edit', 'todo__action-element']);
editButton.innerHTML = '✎';
let isEditing = false;
// listener for edit button
editButton.addEventListener('click', () => {
// _!isEditing means input is editable
if (todoItem.value !== '') {
isEditing = !isEditing;
}
editTodo(editButton, todoItem, isEditing, todo.id);
// flip isEditing state if esc was pressed
todoItem.addEventListener('keydown', (e) => {
if (e.key === 'Escape' || e.key === 'Enter') {
isEditing = false;
}
});
});
// create delete button
const deleteButton = createElement('span', ['todo__delete', 'todo__action-element'], 'x');
// listener for delete button
deleteButton.addEventListener('click', deleteTodo);
// append todo item+action button to wrapper, append wrappers to container
todoWrapper.append(...[todoItem, editButton, deleteButton]);
todoContainer.append(todoWrapper);
changeTodoCounter(countActiveTodos(todosArray));
};
todoBody.append(...[todoInfoBar, todoContainer]);
const toggleTodoStatus = (target, todoId, isDisabled) => {
const isTodo = [...target.classList].includes('todo__element');
if (isTodo && isDisabled) {
todosArray.forEach((todo) => {
if (todo.id === todoId) {
todo.active = !todo.active;
target.classList.toggle('todo__element--done');
}
});
}
renderTodos(todosArray);
changeTodoCounter(countActiveTodos(todosArray));
};
const editTodo = (button, todoItem, isEditing, todoId) => {
const confirmTodoChanges = () => {
todoItem.disabled = true;
button.innerHTML = '✎';
todoItem.classList.remove('todo__element--err');
todoItem.name = todoItem.value;
todosArray.forEach((todo) => {
if (todo.id === todoId) {
todo.name = todoItem.value;
}
});
renderTodos(todosArray);
};
let initialTodoValue = todoItem.name;
todoItem.addEventListener('keydown', (e) => {
// if escape was pressed set initial value to input.value and to arr with edited todo
// works only if input value is not empty
if (e.key === 'Escape' && todoItem.value !== '') {
todoItem.disabled = true;
button.innerHTML = '✎';
todoItem.value = initialTodoValue;
todoItem.name = initialTodoValue;
todoItem.classList.remove('todo__element--err');
}
if (e.key === 'Enter' && todoItem.value !== '') {
confirmTodoChanges();
}
});
// make todo editable
todoItem.disabled = false;
button.innerHTML = '✔';
// check if it's editing state and value is not empty save edited todo and disable input
if (!isEditing && todoItem.value !== '') {
confirmTodoChanges();
}
// if input is empty make its border red
if (todoItem.value === '') {
todoItem.classList.add('todo__element--err');
}
};
const deleteTodo = ({ target }) => {
const todoId = +target.parentElement.id;
todosArray = todosArray.filter((todo) => todo.id !== todoId);
renderTodos(todosArray);
changeTodoCounter(countActiveTodos(todosArray));
// if array with todos is empty - delete container with them and info bar
if (todosArray.length === 0) {
removeAllTodo();
}
};
app.append(todoBody);