-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (110 loc) · 2.79 KB
/
script.js
File metadata and controls
129 lines (110 loc) · 2.79 KB
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
const classNames = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}
class Todo{
constructor(no, title){
this.id = no
this.parentNode = document.createElement("li");
this.jobItem = this.createJobBox(title);
this.textbox = this.createTextBox();
this.checkbox = this.createCheckbox();
this.button = this.createButton();
this.createTodo();
}
createTodo(){
this.jobItem.appendChild(this.checkbox);
this.jobItem.appendChild(this.button);
this.jobItem.appendChild(this.createBreak());
this.jobItem.appendChild(this.textbox);
this.parentNode.appendChild(this.jobItem);
}
createJobBox(title){
const jobItem = document.createElement("div");
jobItem.className = classNames.TODO_ITEM;
jobItem.innerHTML = "Todo No. " + this.id + ": " + title
return jobItem
}
createTextBox(){
let text = document.createElement("TextArea");
text.style.width = "100%";
return text
}
createBreak(){
let lineBreak = document.createElement("br");
return lineBreak
}
createButton(){
let button = document.createElement("button");
button.className = classNames.TODO_DELETE
button.innerHTML = "Remove"
button.addEventListener("click", () => removeTodo(this.id))
return button
}
createCheckbox(){
const checkbox = document.createElement("input");
const inputType = document.createAttribute("type")
inputType.value = "checkbox"
checkbox.setAttributeNode(inputType);
checkbox.className = classNames.TODO_CHECKBOX
checkbox.innerHTML = "Done"
checkbox.addEventListener("change", update)
return checkbox
}
isChecked(){
return this.checkbox.checked
}
}
class TodoList{
constructor(){
this.list = []
this.currentID = 1
this.domList = document.getElementById('todo-list')
this.domCount = document.getElementById('item-count')
this.domUncheckCount = document.getElementById('unchecked-count')
}
createTodo() {
const todoTitle = prompt("What are you going todo?")
const todo = new Todo(this.currentID++, todoTitle);
this.domList.appendChild(todo.parentNode);
this.list.push(todo)
this.update();
}
removeTodo(number){
let remove = null
this.list.forEach(
function(todo){
if(todo.id === number){
remove = todo
remove.parentNode.innerHTML = ""
}
}
)
let index = this.list.indexOf(remove);
this.list.splice(index, 1);
this.update()
}
update(){
this.domCount.innerHTML = this.list.length;
this.getUncheckedCount();
}
getUncheckedCount(){
let count = 0
this.list.forEach(
function(todo){if(!todo.isChecked()){count++}}
)
this.domUncheckCount.innerHTML = count
}
}
const todoList = new TodoList();
function newTodo(){
todoList.createTodo();
}
function removeTodo(number){
todoList.removeTodo(number);
}
function update(){
todoList.update();
}