This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtodo.ts
88 lines (85 loc) · 1.96 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
76
77
78
79
80
81
82
83
84
85
86
87
88
import {Component, View, bootstrap, NgFor, bind} from 'angular2/angular2';
import {AngularFire, FirebaseArray} from 'firebase/angularfire';
@Component({
selector: 'todo-app',
appInjector: [
AngularFire,
bind(Firebase).toValue(new Firebase('https://webapi.firebaseio-demo.com/test'))
]})
@View({
templateUrl: 'todo.html',
directives: [NgFor]
})
class TodoApp {
todoService: FirebaseArray;
todoEdit: any;
todoFilter: Boolean;
constructor(sync: AngularFire) {
this.todoService = sync.asArray();
this.todoEdit = null;
this.todoFilter = null;
}
enterTodo($event, newTodo) {
if($event.which === 13) { // ENTER_KEY
var todoText = newTodo.value.trim();
if (todoText) {
this.addTodo(todoText);
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.todoService.save(todo);
this.todoEdit = null;
} else if (which === 27) {
this.todoEdit = null;
target.value = todo.title;
}
}
addTodo(newTitle) {
this.todoService.add({
title: newTitle,
completed: false
});
}
completeMe(todo) {
todo.completed = !todo.completed;
this.todoService.save(todo);
}
deleteMe(todo) {
this.todoService.remove(todo);
}
toggleAll($event) {
var isComplete = $event.target.checked;
this.todoService.list.forEach((todo)=> {
todo.completed = isComplete;
this.todoService.save(todo);
});
}
clearCompleted() {
var toClear = {};
this.todoService.list.forEach((todo) => {
if(todo.completed) {
toClear[todo._key] = null;
}
});
this.todoService.bulkUpdate(toClear);
}
showAll() {
this.todoFilter = null;
}
showActive() {
this.todoFilter = true;
}
showCompleted() {
this.todoFilter = false;
}
}
bootstrap(TodoApp);