-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
117 lines (93 loc) · 3.23 KB
/
app.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
+function () {
let todos = [];
const addButton = document.getElementById("addButton");
const todoText = document.getElementById("todoText");
const todoList = document.getElementById("todoList");
addButton.onclick = addItem;
todoText.onkeypress = function (e) {
if (e.key === 'Enter') {
addItem()
}
};
function addItem() {
todos.push({text: todoText.value, done: false, editMode: false});
todoText.value = "";
refreshTodos();
}
function createTodoItem(todo, index) {
let item = document.createElement("li");
let text = document.createElement("span");
let btnContainer = createButtons(index);
if (todo.done) {
item.setAttribute("class", "item-done");
}
text.innerText = todo.text;
item.appendChild(text);
item.appendChild(btnContainer);
return item;
}
function createEditableTodoItem(todo, index) {
let item = document.createElement("li");
let textBox = document.createElement("input");
let btnContainer = createEditableModeButtons(index, textBox);
textBox.setAttribute("type", "text");
textBox.value = todo.text;
item.appendChild(textBox);
item.appendChild(btnContainer);
return item
}
function createEditableModeButtons(itemIndex, editableTextBox) {
let btnContainer = document.createElement("div");
btnContainer.appendChild(createButton("×", cancelEditMode));
btnContainer.appendChild(createButton("✓", save));
return btnContainer;
function cancelEditMode() {
todos[itemIndex].editMode = false;
refreshTodos();
}
function save() {
todos[itemIndex].editMode = false;
todos[itemIndex].text = editableTextBox.value;
refreshTodos();
}
}
function createButtons(itemIndex) {
let btnContainer = document.createElement("div");
btnContainer.appendChild(createButton("✎", setEditMode));
btnContainer.appendChild(createButton("✓", setDone));
btnContainer.appendChild(createButton("🗑", removeTodo));
return btnContainer;
function removeTodo(e) {
e.preventDefault();
todos.splice(itemIndex, 1);
refreshTodos();
}
function setDone() {
todos[itemIndex].done = !todos[itemIndex].done;
refreshTodos();
}
function setEditMode() {
todos[itemIndex].editMode = true;
refreshTodos();
}
}
function createButton(text, func) {
let btn = document.createElement("a");
btn.innerHTML = text;
btn.setAttribute("href", "#");
btn.onclick = func;
return btn;
}
function refreshTodos() {
while (todoList.firstChild) {
todoList.removeChild(todoList.firstChild)
}
todos.forEach((todo, index) => {
if (todo.editMode) {
todoList.appendChild(createEditableTodoItem(todo, index))
} else {
todoList.appendChild(createTodoItem(todo, index))
}
})
}
}();