forked from heyamykate/vanillaJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (99 loc) · 3.53 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
117
118
119
120
121
122
123
124
125
126
127
128
var taskInput = document.getElementById("new-task"); //new-task
var addButton = document.getElementsByTagName("button")[0]; //first button
var incompleteTasksHolder = document.getElementById("incomplete-tasks"); //ul #incomplete-tasks
var completedTasksHolder = document.getElementById("completed-tasks"); //ul #completed-tasks
//Add a new task
//New Task List Item
var createNewTaskElement = function(taskString) {
//create list item
var listItem = document.createElement("li");
//input (checkbox)
var checkBox = document.createElement("input"); //checkbox
//label
var label = document.createElement("label");
//input (text)
var editInput = document.createElement("input"); //text
//Create button.edit
var editButton = document.createElement("button");
//Create button.delete
var deleteButton = document.createElement("button");
checkBox.type = "checkbox";
editInput.type = "text";
editButton.innerText = "Edit";
editButton.className = "edit";
deleteButton.innerText = "Delete";
deleteButton.className = "delete";
label.innerText = taskString;
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
return listItem;
};
var addTask = function() {
console.log("Add task...")
var listItem = createNewTaskElement(taskInput.value);
//append listItem to incompleteTasksHolder
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, tasksCompleted);
//clear input value after task is added
taskInput.value = "";
};
//Edit an existing task
var editTask = function() {
console.log("Edit task...");
var listItem = this.parentNode;
var editInput = listItem.querySelector("input[type=text]");
var label = listItem.querySelector("label");
var containsClass = listItem.classList.contains("editMode");
if(containsClass) {
label.innerText = editInput.value;
} else {
editInput.value = label.innerText;
}
//Toggle .editMode on the parent li
listItem.classList.toggle("editMode");
};
//Delete an existing task
var deleteTask = function() {
console.log("Delete task....");
var listItem = this.parentNode;
var ul = listItem.parentNode;
ul.removeChild(listItem);
};
//Mark a task as complete
var tasksCompleted = function() {
console.log("complete task....")
//Append the task li to the ul #completed-tasks
var listItem = this.parentNode;
completedTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, tasksIncomplete);
};
//Mark a task as incomplete
var tasksIncomplete = function() {
console.log("Task incomplete...")
//Append the task li to the ul #incomplete-tasks
var listItem = this.parentNode;
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, tasksCompleted);
};
var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
//Select li children
var checkBox = taskListItem.querySelector("input[type=checkbox]");
var editButton = taskListItem.querySelector("button.edit");
var deleteButton = taskListItem.querySelector("button.delete");
//Bind the editTask to edit button
editButton.onclick = editTask;
//Bind deleteTask to delete button
deleteButton.onclick = deleteTask;
//Bind checkBoxEventHandler to checkbox
checkBox.onchange = checkBoxEventHandler;
};
addButton.onclick = addTask;
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
bindTaskEvents(incompleteTasksHolder.children[i], tasksCompleted);
};
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
bindTaskEvents(completedTasksHolder.children[i], tasksIncomplete);
}