-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
155 lines (119 loc) · 4.41 KB
/
list.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
// Add a new task.
let taskInput = document.getElementById("new-task");
// first button
let addButton = document.getElementsByTagName("button")[0];
// ul of #incomplete-tasks
let incompleteTaskHolder = document.getElementById("incomplete-tasks");
// completed-tasks
let completedTasksHolder = document.getElementById("completed-tasks");
/*---- Part 1 ----*/
// function to create new task item
let createNewTaskElement = function (taskString) {
let listItem = document.createElement("li");
// input (checkbox)
let checkBox = document.createElement("input"); // checkbox
// label
let label = document.createElement("label"); // label
// input (text)
let editInput = document.createElement("input"); // text
// button.edit
let editButton = document.createElement("button"); // edit button
// button.delete
let deleteButton = document.createElement("button"); // delete button
label.innerText = taskString;
// Each elements, needs appending
checkBox.type = "checkbox";
editInput.type = "text";
// innerText encodes special characters, HTML does not.
editButton.innerText = "Edit";
editButton.className = "edit";
deleteButton.innerText = "Delete";
deleteButton.className = "delete";
// and appending.
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
return listItem;
}
/*---- Part 2 ----*/
let addTask = function () {
console.log("Add Task...");
let listItem = createNewTaskElement(taskInput.value);
if (taskInput.value == "") {
return;
}
// Append listItem to incompleteTaskHolder
incompleteTaskHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
taskInput.value = "";
}
/*---- Part 3 ----*/
let editTask = function () {
console.log("Edit Task...");
console.log("Change 'edit' to 'save'");
let listItem = this.parentNode;
let editInput = listItem.querySelector('input[type=text]');
let label = listItem.querySelector("label");
let containsClass = listItem.classList.contains("editMode");
// If class of the parent is .editmode
if (containsClass) {
label.innerText = editInput.value;
} else {
editInput.value = label.innerText;
}
listItem.classList.toggle("editMode");
}
/*---- Part 4 ----*/
let deleteTask = function () {
console.log("Delete Task...");
let listItem = this.parentNode;
let ul = listItem.parentNode;
// Remove the parent list item from the ul.
ul.removeChild(listItem);
}
/*---- Part 5 ----*/
let taskCompleted = function () {
console.log("Complete Task...");
// Append the task list item to the #completed-tasks
let listItem = this.parentNode;
completedTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
}
/*---- Part 6 ----*/
let taskIncomplete = function () {
console.log("Incomplete Task...");
// Mark task as incomplete.
let listItem = this.parentNode;
incompleteTaskHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
/*---- Part 7 ----*/
addButton.onclick = addTask;
addButton.addEventListener("click", addTask);
let bindTaskEvents = function (taskListItem, checkBoxEventHandler) {
console.log("bind list item events");
// select ListItems children
let checkBox = taskListItem.querySelector("input[type=checkbox]");
let editButton = taskListItem.querySelector("button.edit");
let deleteButton = taskListItem.querySelector("button.delete");
// Bind editTask to edit button.
editButton.onclick = editTask;
// Bind deleteTask to delete button.
deleteButton.onclick = deleteTask;
// Bind taskCompleted to checkBoxEventHandler.
checkBox.onchange = checkBoxEventHandler;
}
/*---- Part 8 ----*/
// cycle over incompleteTaskHolder ul list items
// for each list item
for (let i = 0; i < incompleteTaskHolder.children.length; i++) {
// bind events to list items children(tasksCompleted)
bindTaskEvents(incompleteTaskHolder.children[i], taskCompleted);
}
// cycle over completedTasksHolder ul list items
for (let i = 0; i < completedTasksHolder.children.length; i++) {
// bind events to list items children(tasksIncompleted)
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}