-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
167 lines (140 loc) · 4.9 KB
/
todo.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
156
157
158
159
160
161
162
163
164
165
166
167
var todoList = {
todos: [],
addTodo: function(todoText) {
this.todos.push({
todoText: todoText,
completed: false
});
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText;
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
},
toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0;
// Get number of completed todos.
//for (var i = 0; i < totalTodos; i++) {
// if (this.todos[i].completed === true) {
//completedTodos++;
// }
// }
this.todos.forEach (function (todo) {
if (todo.completed === true) {
completedTodos++;
}
});
// Case 1: If everything’s true, make everything false.
// if (completedTodos === totalTodos) {
//for (var i = 0; i < totalTodos; i++) {
//this.todos[i].completed = false;
//}
// this.todos.forEach (function (todo) {
// todo.completed = false;
//});
// Case 2: Otherwise, make everything true.
// } else {
//for (var i = 0; i < totalTodos; i++) {
// this.todos[i].completed = true;
this.todos.forEach (function (todo) {
// Case 1: If everything’s true, make everything false.
if (completedTodos === totalTodos) {
todo.completed = false;
// Case 2: Otherwise, make everything true.
} else {
todo.completed = true;
}
});
}
};
var handlers = {
addTodo: function() {
var addTodoTextInput = document.getElementById('addTodoTextInput');
todoList.addTodo(addTodoTextInput.value);
addTodoTextInput.value = '';
view.displayTodos();
},
changeTodo: function() {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
view.displayTodos();
},
deleteTodo: function(position) {
//var deleteTodoPositionInput = document.getElementById('deleteTodoPositionInput');
//todoList.deleteTodo(deleteTodoPositionInput.valueAsNumber);
// deleteTodoPositionInput.value = '';
todoList.deleteTodo(position);
view.displayTodos();
},
toggleCompleted: function() {
var toggleCompletedPositionInput = document.getElementById('toggleCompletedPositionInput');
todoList.toggleCompleted(toggleCompletedPositionInput.valueAsNumber);
toggleCompletedPositionInput.value = '';
view.displayTodos();
},
toggleAll: function() {
todoList.toggleAll();
view.displayTodos();
}
};
var view = {
displayTodos: function() {
var todosUl = document.querySelector('ul');
todosUl.innerHTML = '';
/*for (var i = 0; i < todoList.todos.length; i++) {
var todoLi = document.createElement('li');
var todo = todoList.todos[i];
var todoTextWithCompletion = '';
if (todo.completed === true) {
todoTextWithCompletion = '(x) ' + todo.todoText;
} else {
todoTextWithCompletion = '( ) ' + todo.todoText;
}
todoLi.id = i;
todoLi.textContent = todoTextWithCompletion;
todoLi.appendChild(this.createDeleteButton())
todosUl.appendChild(todoLi);
}*/
todoList.todos.forEach (function(todo,position){
var todoLi = document.createElement('li');
//var todo = todoList.todos[i];
var 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: function () {
var deleteButton = document.createElement('button');
deleteButton.textContent = "Delete";
deleteButton.className = "deleteButton";
return deleteButton;
},
setUpEventListeners: function (){
var todosUl = document.querySelector('ul');
todosUl.addEventListener ('click', function (event) {
//get the element that was clicked on
var elementClicked = event.target;
//check if elementClicked is a delete button
if (elementClicked.className === 'deleteButton') {
handlers.deleteTodo(parseInt(elementClicked.parentNode.id));
}
});
}
};
view.setUpEventListeners();