-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
413 lines (342 loc) · 11.4 KB
/
script.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
class ToDoItem {
constructor(description, isDone, id) {
this.description = description;
this.isDone = isDone;
this.id = id;
}
}
/*****************************************************************************************************/
const addBtn = document.querySelector("#btn-add");
const rmBtn = document.querySelector("#btn-remove");
const input = document.querySelector("#todo-input");
const domFilterAll = document.querySelector("#all");
const domFilterOpen = document.querySelector("#open");
const domFilterDone = document.querySelector("#done");
const toDoList = document.querySelector("#todo-list");
const errorMsg = document.querySelector("#aside-input-error");
const filterAll = "all";
const filterOpen = "open";
const filterDone = "done";
const filterOptions = [domFilterAll, domFilterOpen, domFilterDone];
/*****************************************************************************************************/
const appState = {
filter: filterAll,
todos: [],
};
addBtn.addEventListener("click", function (event) {
event.preventDefault();
addTodoItem();
});
rmBtn.addEventListener("click", function () {
removeAllDoneTodos();
});
for (const el of filterOptions) {
el.addEventListener("change", (e) => {
appState.filter = e.target.id;
localStorage.setItem("filter", appState.filter);
render();
});
}
getDataFromLocalStorage();
render();
/*****************************************************************************************************/
/**
* add input value as new ToDoItem
*/
function addTodoItem() {
if (isInputValid(input.value)) {
const newEntry = new ToDoItem(input.value, false, generateId());
appState.todos.push(newEntry);
updateLocalStorage();
render();
}
input.value = ""; //clear input field
}
/**
* delete todo-item matching clicked button (compares todo-item id with button name)
*/
function deleteSingleEntry() {
const index = appState.todos.findIndex((e) => e.id === parseInt(this.name));
appState.todos.splice(index, 1);
updateLocalStorage();
render();
}
/**
* remove ToDoItems with property "isDone === true"
* return remaining content of todos array after splicing
*/
function removeAllDoneTodos() {
if (appState.todos.length <= 0) {
console.log("Nothing to delete");
}
appState.todos = appState.todos.filter((e) => e.isDone === false);
updateLocalStorage();
render();
}
/**
* let user edit todo-item's description
* prevent more than 1 editing element (div, input, buttons) to be visible
* check if there is already an editing element existing in todo-list ul element
* activeEditingElementBtn variable saves current active editBtn button element
*/
let activeEditingElementBtn = null;
function editEntry() {
//logic
const index = appState.todos.findIndex((e) => e.id === parseInt(this.name)); //this: editBtn
const listItemInput = document.getElementById(parseInt(this.name)); //input checkbox element (sibling)
const listElement = listItemInput.parentElement; //li element (parent)
/**
* check if activeEditingElementBtn matches currently clicked editBtn button element
*/
if (activeEditingElementBtn != this) {
/* remove existing editing element (the one which was opened before and is still visible) */
if (activeEditingElementBtn != null) {
//console.log("active: ", activeEditingElementBtn.name);
const toRemove = document.getElementById(
parseInt(activeEditingElementBtn.name)
).parentElement.nextSibling;
console.dir(toRemove);
activeEditingElementBtn.classList.remove("bg-clr-red02");
if (toRemove != null && toRemove.nodeName === "DIV") {
toRemove.remove();
}
}
/* overwrite activeEditingElementBtn to this as the current active/open/visible editing element */
activeEditingElementBtn = this;
this.classList.add("bg-clr-red02"); //editBtn extra styling
//structure
//TODO: tidy code by implementing additional functions
const container = document.createElement("div");
container.classList.add("editTextInput");
const arrowSymbol = document.createElement("p");
arrowSymbol.innerText = "↪";
arrowSymbol.classList.add("arrowSymbol");
const inputText = document.createElement("input");
inputText.setAttribute("type", "text");
inputText.setAttribute("placeholder", "Edit Todo Title here...");
inputText.value = appState.todos[index].description;
const confirmBtn = document.createElement("button");
confirmBtn.innerText = "✓";
confirmBtn.classList.add("confirmBtn");
const cancelBtn = document.createElement("button");
cancelBtn.innerText = "✕";
container.appendChild(arrowSymbol);
container.appendChild(inputText);
container.appendChild(confirmBtn);
container.appendChild(cancelBtn);
listElement.parentNode.insertBefore(container, listElement.nextSibling);
//event listeners for confirm and cancel buttons
confirmBtn.addEventListener("click", function () {
if (isInputValid(inputText.value)) {
appState.todos[index].description = inputText.value;
updateLocalStorage();
render();
}
});
//render to hide editing element
cancelBtn.addEventListener("click", function () {
render();
});
}
}
/**
* move todo-item either up or down in list
*/
function moveEntry() {
const index = appState.todos.findIndex((e) => e.id === parseInt(this.name));
let direction;
if (this.innerText === "⬆") {
direction = "up";
} else {
direction = "down";
}
//temporary
if (appState.filter === filterAll) {
switch (direction) {
case "up":
if (index > 0) {
const tempStore = appState.todos[index - 1];
appState.todos[index - 1] = appState.todos[index];
appState.todos[index] = tempStore;
updateLocalStorage();
render();
}
break;
case "down":
if (index + 1 < appState.todos.length) {
const tempStore = appState.todos[index + 1];
appState.todos[index + 1] = appState.todos[index];
appState.todos[index] = tempStore;
updateLocalStorage();
render();
}
break;
}
}
}
/**
* set ToDoItem's "isDone"-property to either true or false
*/
function toggleProgress() {
const entry = this;
const index = appState.todos.findIndex((e) => e.id === parseInt(entry.id));
appState.todos[index].isDone = entry.checked;
updateLocalStorage();
render();
}
/**
* check if text input is valid
* @param {*} inputVal input element value from user input
* @returns false if input text length is less than 5 chars and if todo item's description is already used
*/
function isInputValid(inputVal) {
if (inputVal.length < 5) {
renderErrorMsg("Input ivalid! Must consist of 5 characters at least!");
return false;
}
const foundExistingEntry = findExistingEntry();
if (foundExistingEntry) {
renderErrorMsg("Input invalid! Todo already exists!");
return false;
}
return true;
}
/**
* find already existing todo-item if present (case sensitive)
* @returns todo-item id
*/
function findExistingEntry() {
const inputVal = input.value;
const inputBothCases = appState.todos.find(
(e) => e.description.toLowerCase() === inputVal.toLowerCase()
);
if (inputBothCases != null) {
return inputBothCases.id;
}
}
/*****************************************************************************************************/
/**
* render markup of todo list content
*/
function render() {
toDoList.innerHTML = "";
const filteredTodos = filterTodos(appState.filter);
for (const todo of filteredTodos) {
createTodoItemMarkupStructure(todo);
// renderTodo(todo);
}
renderFilter();
}
/**
* @param {*} filterProp state's filter property
* @returns either array of all, done or open todos depending on state's filter property
*/
function filterTodos(filterProp) {
if (filterProp === filterOpen) {
return appState.todos.filter((e) => e.isDone === false);
} else if (filterProp === filterDone) {
return appState.todos.filter((e) => e.isDone === true);
} else {
return appState.todos;
}
}
/**
* check filter-checkbox of last chosen filter (saved in localStorage)
* needed for rendering filter option after site refreshing
*/
function renderFilter() {
switch (appState.filter) {
case "all":
domFilterAll.checked = true;
break;
case "open":
domFilterOpen.checked = true;
break;
case "done":
domFilterDone.checked = true;
break;
}
}
//TODO: tidy code by implementing additional functions
function createTodoItemMarkupStructure(todo) {
const listItem = document.createElement("li");
//listItem.todoObj = entry;
const todoEntry = document.createElement("input");
todoEntry.setAttribute("type", "checkbox");
todoEntry.setAttribute("id", todo.id);
todoEntry.checked = todo.isDone;
todoEntry.addEventListener("change", toggleProgress);
const entryLabel = document.createElement("label");
entryLabel.setAttribute("for", todoEntry.id);
entryLabel.innerText = todo.description;
listItem.appendChild(todoEntry);
listItem.appendChild(entryLabel);
toDoList.appendChild(listItem);
createUtilityButtonsForTodoItem(listItem, todoEntry.id);
}
function createUtilityButtonsForTodoItem(listItem, id) {
const btnContainer = document.createElement("div");
//edit
const editBtn = document.createElement("button");
editBtn.setAttribute("name", id);
// editBtn.setAttribute("data-id", id);
editBtn.innerText = "✏️";
editBtn.addEventListener("click", editEntry);
btnContainer.appendChild(editBtn);
//move up or down
const moveUpBtn = document.createElement("button");
moveUpBtn.setAttribute("name", id);
moveUpBtn.innerText = "⬆";
moveUpBtn.addEventListener("click", moveEntry);
btnContainer.appendChild(moveUpBtn);
const moveDownBtn = document.createElement("button");
moveDownBtn.setAttribute("name", id);
moveDownBtn.innerText = "⬇";
moveDownBtn.addEventListener("click", moveEntry);
btnContainer.appendChild(moveDownBtn);
//delete
const deleteBtn = document.createElement("button");
deleteBtn.setAttribute("name", id);
deleteBtn.innerText = "❌";
deleteBtn.addEventListener("click", deleteSingleEntry);
btnContainer.appendChild(deleteBtn);
listItem.appendChild(btnContainer);
}
/**
* tell user why their input is invalid by briefly rendering element with corresponding text
* @param {*} message corresponding text: either not enough characters or duplicate input
*/
function renderErrorMsg(message) {
errorMsg.innerText = message;
errorMsg.classList.remove("vis-hidden");
setTimeout(function () {
errorMsg.classList.add("vis-hidden");
}, 3000);
}
/*****************************************************************************************************/
/**
* get data from local storage (todo-items as array)
*/
function getDataFromLocalStorage() {
const todosFromLocalStorage = localStorage.getItem("todos");
if (todosFromLocalStorage !== null) {
//const todoData = JSON.parse(todosFromLocalStorage);
appState.todos = JSON.parse(todosFromLocalStorage);
// appState.todos = todoData;
}
if (localStorage.getItem("filter")) {
appState.filter = localStorage.getItem("filter");
}
}
/**
* load todo-items to local storage
*/
function updateLocalStorage() {
localStorage.setItem("todos", JSON.stringify(appState.todos));
}
/**
* generate id for label and input relations
*/
function generateId() {
return Date.now();
}