-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.ts
75 lines (71 loc) · 2.03 KB
/
todo.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
72
73
74
75
/// <reference path="./components/angular2/typings/es6-shim/es6-shim.d.ts" />
/// <reference path="./components/angular2/typings/angular2/angular2.d.ts" />
import {Component, bootstrap, Inject, FORM_DIRECTIVES, CORE_DIRECTIVES} from 'angular2/angular2';
import {TodoStore} from './deps/services/TodoStore';
@Component({
selector: 'todo-app',
templateUrl: __uri('todo.html'),
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES],
providers: [TodoStore]
})
class TodoApp {
todoStore: TodoStore;
todoEdit: any;
todos: Array;
constructor( @Inject(TodoStore) store: TodoStore) {
this.todoStore = store;
this.todoEdit = null;
this.todos = store.list;
}
enterTodo($event, newTodo) {
if ($event.which === 13) { // ENTER_KEY
this.addTodo(newTodo.value);
newTodo.value = '';
}
}
editTodo($event, todo) {
this.todoEdit = todo;
}
doneEditing($event, todo) {
var which = $event.which;
var target = $event.target;
if (which === 13) {
todo.title = target.value;
this.todoStore.save(todo);
this.todoEdit = null;
} else if (which === 27) {
this.todoEdit = null;
target.value = todo.title;
}
}
addTodo(newTitle) {
this.todoStore.add({
title: newTitle,
completed: false
});
}
completeMe(todo) {
todo.completed = !todo.completed;
this.todoStore.save(todo);
}
deleteMe(todo) {
this.todoStore.remove(todo);
}
toggleAll($event) {
var isComplete = $event.target.checked;
this.todoStore.list.forEach(function(todo) {
todo.completed = isComplete;
this.todoStore.save(todo);
}.bind(this));
}
clearCompleted() {
[].concat(this.todoStore.list).forEach((todo) => {
if (todo.completed) {
this.deleteMe(todo);
}
});
}
}
export function run() {
bootstrap(TodoApp);
}