-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (115 loc) · 4.23 KB
/
script.js
File metadata and controls
128 lines (115 loc) · 4.23 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
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]; //saving a refrence to the specific todo that we are interested in,which it save us to type it in 2 places
todo.completed = !todo.completed; //we grap the todo.completed which is a boolean value, and we want to change the value to the oposite of what it is.
},
toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0;
//get number of completed todos
this.todos.forEach(function(todo){
if (todo.completed === true){
completedTodos++;
}
});
this.todos.forEach(function(todo){
//case 1: if everything true, make everything false
if (completedTodos === totalTodos) {
todo.completed = false;
}else {
todo.completed = true;
}
});
}
};
//we want the methods on this object to handel different events
var handler = {
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) {
todoList.deleteTodo(position);
view.displayTodos();
},
toggleCompleted: function() {
var deleteTodoPositionInput = document.getElementById('togglecompletedPositionInput');
todoList.toggleCompleted(togglecompletedPositionInput.valueAsNumber);
deleteTodoPositionInput.value = '';
view.displayTodos();
},
toggleAll: function(){
todoList.toggleAll();
view.displayTodos();
}
};
//its responsible to what user sees
var view = {
displayTodos: function() {
//to grab the unordered list we created
var todoUl = document.querySelector('ul');
//at the beginning we want to make sure it start from zero
todoUl.innerHTML = '';
todoList.todos.forEach(function(todo, position){
var todoLi = document.createElement('li');
//combine the representation of the todo if its completed or not.
var todoTextWithCompletion = '';
if(todo.completed === true) {
todoTextWithCompletion = '(X) ' + todo.todoText;
} else {
todoTextWithCompletion = '( ) ' + todo.todoText;
}
//id is the property that i can access the element id.
todoLi.id = position;
todoLi.textContent = todoTextWithCompletion;
todoLi.appendChild(this.createDeleteButton());
todoUl.appendChild(todoLi);
}, this);
},
createDeleteButton: function() {
var deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.className = 'deleteButton';
return deleteButton;
},
setUpEventListeners: function() {
//access and add an event listener just to the unordered list so we need to get a reference to that. grab the unordered list
var todoUl = document.querySelector('ul');
//here we add the eventListener that we want to wait for the click event.
//addEventListener will run callback function.addEventListener is the higher order function.
//when it runs the call back function its gonna pass in an event object, so we need to add a parameter in callback function.
todoUl.addEventListener('click', function(event) {
//console.log(event.target.parentNode.id);
//get the element that was clicked on
var elementClicked = event.target;
//check if elementClicked is the a delete button
if (elementClicked.className === 'deleteButton') {
handler.deleteTodo(parseInt(elementClicked.parentNode.id));
}
});
}
};
view.setUpEventListeners();