forked from jchadwick/EssentialTypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoApp.ts
71 lines (53 loc) · 1.8 KB
/
TodoApp.ts
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
import { Todo, TodoState } from './Model';
import TodoService, { ITodoService } from './TodoService';
import TodoListComponent from './TodoListComponent';
export class TodoApp {
private todoService: ITodoService;
private todoList: TodoListComponent;
constructor(el, todos) {
this.todoService = new TodoService(todos);
this.initialize(el);
}
addTodo(todoName) {
try {
this.todoService.add(todoName);
} catch(x) {
console.error(x)
}
this.renderTodos();
}
clearCompleted() {
this.todoService.clearCompleted();
this.renderTodos();
}
toggleTodoState(todoId) {
this.todoService.toggle(todoId);
this.renderTodos();
}
renderTodos() {
var todos = this.todoService.getAll();
this.todoList.render(todos);
}
initialize(el) {
var _this = this;
var addTodoFormEl = el.getElementsByClassName('add-todo')[0],
addTodoNameEl = addTodoFormEl.getElementsByTagName('input')[0],
todoListEl = el.getElementsByClassName('todo-list')[0],
clearCompletedEl = el.getElementsByClassName('clear-completed')[0];
addTodoFormEl.addEventListener('submit', function(evnt) {
_this.addTodo(addTodoNameEl.value)
addTodoNameEl.value = '';
evnt.preventDefault();
});
todoListEl.addEventListener('todo-toggle', function(evnt) {
var todoId = evnt.details.todoId;
_this.todoService.toggle(todoId);
_this.renderTodos();
});
clearCompletedEl.addEventListener('click', function() {
_this.clearCompleted();
});
this.todoList = new TodoListComponent(todoListEl);
this.renderTodos();
}
}