-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (100 loc) · 2.84 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let todoList = {
todos: [],
addTodo (todoText) {
this.todos.push({
todoText: todoText,
completed: false
})
},
changeTodo (position, newTodoText) {
this.todos[position].todoText = newTodoText
},
toggleTodo (position) {
let todoItem = this.todos[position]
todoItem.completed = !todoItem.completed
},
toggleAll () {
let totalTodos = this.todos.length
let completedTodos = 0
this.todos.forEach((todo) => {
if (todo.completed === true) {
completedTodos++
}
})
this.todos.forEach((todo) => {
if (completedTodos === totalTodos) {
todo.completed = false
} else {
todo.completed = true
}
})
},
deleteTodo (position) {
this.todos.splice(position, 1)
}
}
let handlers = {
toggleAll () {
todoList.toggleAll()
view.displayTodos()
},
addTodo () {
let addTodoTextInput = document.getElementById('addTodoTextInput')
todoList.addTodo(addTodoTextInput.value)
addTodoTextInput.value = ''
view.displayTodos()
},
changeTodo () {
let changeTodoPositionValue = document.getElementById('changeTodoPositionValue')
let changeTodoNewValue = document.getElementById('changeTodoNewValue')
todoList.changeTodo(changeTodoPositionValue.valueAsNumber, changeTodoNewValue.value)
changeTodoNewValue.value = ''
changeTodoPositionValue.value = ''
view.displayTodos()
},
deleteTodo (position) {
todoList.deleteTodo(position)
view.displayTodos()
},
toggleTodo () {
let toggleTodoPositionInput = document.getElementById('toggleTodoPositionInput')
todoList.toggleTodo(toggleTodoPositionInput.valueAsNumber)
toggleTodoPositionInput.value = ''
view.displayTodos()
}
}
let view = {
displayTodos () {
let todosUl = document.querySelector('ul')
todosUl.innerHTML = ''
todoList.todos.forEach((todo, position) => {
let todoLi = document.createElement('li')
let todoTextWithCompletion = ''
if (todo.completed === true) {
todoTextWithCompletion = '(x) ' + todo.todoText
} else {
todoTextWithCompletion = '( ) ' + todo.todoText
}
todoLi.id = position
todoLi.textContent = todoTextWithCompletion
todoLi.appendChild(this.createDeleteButton())
todosUl.appendChild(todoLi)
}, this)
},
createDeleteButton () {
let deleteButton = document.createElement('button')
deleteButton.textContent = 'Delete Me'
deleteButton.className = 'deleteButton'
return deleteButton
},
setUpEventListeners () {
let todosUl = document.querySelector('ul')
todosUl.addEventListener('click', event => {
let elementClicked = event.target
if (elementClicked.className === 'deleteButton') {
handlers.deleteTodo(parseInt(elementClicked.parentNode.id))
}
})
}
}
view.setUpEventListeners()