-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (92 loc) · 3.49 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
var addButton = document.getElementById('add');
var inputTask = document.getElementById('new-task');
var unfinishedTasks = document.getElementById('unfinished-tasks');
var finishedTasks = document.getElementById('finished-tasks');
function createNewElement(task, finished) {
var listItem = document.createElement('li');
var checkbox = document.createElement('button');
if(finished){
checkbox.className = "material-icons checkbox";
checkbox.innerHTML = "<i class='material-icons'>check_box</i>";
}else {
checkbox.className = "material-icons checkbox";
checkbox.innerHTML = "<i class='material-icons'>check_box_outline_blank</i>";
}
var label = document.createElement('label');
label.innerText = task;
var input = document.createElement('input');
input.type = "text";
var editButton = document.createElement('button');
editButton.className = "material-icons edit";
editButton.innerHTML = "<i class='material-icons'>edit</i>";
var deleteButton = document.createElement('button');
deleteButton.className = "material-icons delete";
deleteButton.innerHTML = "<i class='material-icons'>delete</i>";
listItem.appendChild(checkbox);
listItem.appendChild(label);
listItem.appendChild(input);
listItem.appendChild(deleteButton);
listItem.appendChild(editButton);
return listItem;
}
function addTask() {
if (inputTask.value) {
var listItem = createNewElement(inputTask.value, false);
unfinishedTasks.appendChild(listItem);
bindTaskEvents(listItem, finishTask)
inputTask.value = "";
}
save();
}
addButton.onclick = addTask;
function deleteTask() {
var listItem = this.parentNode;
var ul = listItem.parentNode;
ul.removeChild(listItem);
save();
}
function editTask() {
console.log(2);
var editButton = this;
var listItem = this.parentNode;
var label = listItem.querySelector('label');
var input = listItem.querySelector('input[type=text]');
var containsClass = listItem.classList.contains('editMode');
if (containsClass) {
label.innerText = input.value;
editButton.className = "material-icons edit";
editButton.innerHTML = "<i class='material-icons'>edit</i>";
save();
} else {
input.value = label.innerText;
editButton.className = "material-icons save";
editButton.innerHTML = "<i class='material-icons'>save</i>";
}
listItem.classList.toggle('editMode');
}
function finishTask() {
var listItem = this.parentNode;
var checkbox = listItem.querySelector('button.checkbox');
checkbox.className = "material-icons checkbox";
checkbox.innerHTML = "<i class='material-icons'>check_box</i>";
finishedTasks.appendChild(listItem);
bindTaskEvents(listItem, unfinishTask);
save();
}
function unfinishTask() {
var listItem = this.parentNode;
var checkbox = listItem.querySelector('button.checkbox');
checkbox.className = "material-icons checkbox";
checkbox.innerHTML = "<i class='material-icons'>check_box_outline_blank</i>";
unfinishedTasks.appendChild(listItem);
bindTaskEvents(listItem, finishTask)
save();
}
function bindTaskEvents(listItem, checkboxEvent) {
var checkbox = listItem.querySelector('button.checkbox');
var editButton = listItem.querySelector('button.edit');
var deleteButton = listItem.querySelector('button.delete');
checkbox.onclick = checkboxEvent;
editButton.onclick = editTask;
deleteButton.onclick = deleteTask;
}