-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical_js.js
114 lines (106 loc) · 3.76 KB
/
practical_js.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
function todoItem(text, completed) {
this.todoText = text;
this.completed = completed;
}
var todoList = {
todos: [],
addTodo: function (todoText) {
this.todos.splice(0, 0, new todoItem(todoText, false));
},
toggleCompleted: function (position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
},
toggleAll: function () {
var allCompleted = this.todos.every(function (todo) {
return todo.completed;
});
this.todos.forEach(function (todo) {
todo.completed = !allCompleted;
});
},
deleteTodo: function (position) {
this.todos.splice(position, 1);
},
changeTodoText: function (position, newTodoText) {
this.todos[position].todoText = newTodoText;
}
};
var handlers = { // methods in this object are handling different events
toggleAll: function () {
todoList.toggleAll();
view.displayTodos();
},
toggleTodo: function (id) {
todoList.toggleCompleted(id);
view.displayTodos();
},
deleteTodo: function (id) {
todoList.deleteTodo(id);
view.displayTodos();
},
addTodo: function () {
var text_elem = document.getElementById('addTodoTextInput');
var text = text_elem.value;
if (text === '') return;
text_elem.value = ''; // clear input form
todoList.addTodo(text);
view.displayTodos();
}
};
var view = { // methods in this object are responsible for what user sees, object itself does not contain logic
displayTodos: function () {
var todosUl = document.getElementById('todoList');
todosUl.innerHTML = ''; // clears list before adding all list items
todoList.todos.forEach(function (todo, position) {
let todoLi = document.createElement('li');
let textParagraph = document.createElement('p');
textParagraph.textContent = todo.todoText;
todoLi.id = position;
todoLi.appendChild(this.createToggleButton(todo.completed));
todoLi.appendChild(this.createDeleteButton());
todoLi.appendChild(textParagraph);
todosUl.appendChild(todoLi);
}, this);
},
createDeleteButton: function () {
var icon = document.createElement('i');
icon.className = "far fa-trash-alt deleteButton";
return icon;
},
createToggleButton: function (toggled) {
var icon = document.createElement('i');
var sign = toggled === true ? "check-circle" : "circle";
icon.className = "far fa-" + sign + " toggleButton";
return icon;
},
setupButtonClickEventListener: function () {
var todosUl = document.getElementById('todoList');
todosUl.addEventListener('click', function (event) {
var elementClicked = event.target; // the real element that was clicked on
var todoID = parseInt(elementClicked.parentNode.id)
//check if element clicked is a proper button
if (elementClicked.className.includes('deleteButton')) {
handlers.deleteTodo(todoID);
}
if (elementClicked.className.includes('toggleButton')) {
handlers.toggleTodo(todoID);
}
});
},
setupInputEventListener: function () {
var input = document.getElementById('addTodoTextInput');
input.addEventListener('keyup', function (event) {
if (event.keyCode === 13) {
handlers.addTodo();
}
});
},
setupAutofocus: function () {
var input = document.getElementById('addTodoTextInput');
input.autofocus = true;
}
};
view.setupButtonClickEventListener();
view.setupInputEventListener();
view.setupAutofocus();