-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
188 lines (166 loc) · 6.08 KB
/
script.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var todoList = {
todos: [],
displayTodos: function() {
console.log('My Todos:');
if(this.todos.length === 0){
console.log('Your todo list is empty.');
} else {
for(var i = 0; i < this.todos.length; i++){
if(this.todos[i].completed === true){
console.log('(x)',this.todos[i].todoText);
}else{
console.log('( )',this.todos[i].todoText);
}
}
}
},
addTodo: function(todoText) {
this.todos.push({ //Add a itemObject
todoText: todoText,
completed: false
});
view.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText;
view.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
view.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
view.displayTodos();
},
toggleAll: function(){
var totalTodos = this.todos.length;
var todosCompleted = 0;
for(var i = 0; i < totalTodos; i++){
if(this.todos[i].completed === true){
todosCompleted++;
}
}
//Case 1: If everything's true, make everything false.
if(todosCompleted === totalTodos){
for(i = 0; i < totalTodos; i++){
this.todos[i].completed = false;
}
//Case 2: Otherwise, make everything true.
} else {
for(i = 0; i < totalTodos; i++){
this.todos[i].completed = true;
}
}
view.displayTodos();
}
};
//Object to handle events
var handlers = {
//Display Todos botton must run displayTodoList()
displayTodos: function() {
view.displayTodos();
},
//Toggle Todos botton must run toggleAll()
toggleAll: function() {
todoList.toggleAll();
},
addTodo: function(event) {
if (event.key === "Enter") {
var addTodoInputText = document.getElementById('addTodoInputText');
todoList.addTodo(addTodoInputText.value);
addTodoInputText.value = '';
}
},
changeTodo: function(position, changeTodoTextInput) {
todoList.changeTodo(position, changeTodoTextInput);
},
deleteTodo: function(position) {
todoList.deleteTodo(position);
},
toggleCompleteTodo: function(position) {
todoList.toggleCompleted(position);
}
};
var view = {
createDeleteButton: function() {
var buttonDelete = document.createElement('img');
//buttonDelete.textContent = 'Delete';
buttonDelete.src = 'img/delete.png';
buttonDelete.className = 'deleteBtn';
return buttonDelete;
},
createCompletedButton: function(completed) {
var buttonCompleted = document.createElement('img');
if(completed === true) {
buttonCompleted.src = 'img/checked.png';
buttonCompleted.className = 'completedBtn';
} else {
buttonCompleted.src = 'img/unchecked.png';
buttonCompleted.className = 'completedBtn';
}
return buttonCompleted;
},
displayTodos: function() {
//debugger;
var todoUL = document.querySelector('ul.todos');
var todos = todoList.todos;
//Replaces the ul content with an empty string
todoUL.innerHTML = '';
todos.forEach(function(todo, position) {
var todoLI = document.createElement('li');
todoLI.appendChild(this.createCompletedButton(todo.completed));
if(todo.completed === true) {
todoLI.append(todo.todoText);
} else {
todoLI.append(todo.todoText);
}
todoLI.id = position;
todoLI.appendChild(this.createDeleteButton());
todoUL.appendChild(todoLI);
}, this);
if(todos.length === 0) {
var todoListIsEmpty = document.createElement('p');
todoListIsEmpty.className = 'todoListIsEmpty';
todoListIsEmpty.textContent = 'Your todo list is empty';
todoUL.appendChild(todoListIsEmpty);
}
},
clickToDelete: function() {
var todoUL = document.querySelector('ul.todos');
todoUL.addEventListener('click', function(event) {
//Get the element that was clicked
var elementClicked = event.target;
if(elementClicked.className === 'deleteBtn') {
handlers.deleteTodo(parseInt(elementClicked.parentNode.id));
} else if(elementClicked.className === 'completedBtn' ||
elementClicked.className === 'completedBtn') {
handlers.toggleCompleteTodo(parseInt(elementClicked.parentNode.id));
}
});
todoUL.addEventListener('dblclick', function(event) {
//debugger;
var elementClicked = event.target;
var position = parseInt(elementClicked.id);
var changeTodoTextInput = document.createElement('input');
changeTodoTextInput.className = 'changeTodoTextInput';
changeTodoTextInput.value = elementClicked.textContent;
elementClicked.textContent = '';
elementClicked.appendChild(changeTodoTextInput);
changeTodoTextInput.focus();
changeTodoTextInput.select();
changeTodoTextInput.addEventListener('keydown', function(event) {
//debugger;
//console.log(position, changeTodoTextInput);
if(event.key === 'Enter') {
handlers.changeTodo(position, changeTodoTextInput.value);
elementClicked.removeChild(changeTodoTextInput);
}
});
});
}
};
//Waiting for the click on 'Delete' button to delete
view.displayTodos();
view.clickToDelete();