-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtodoCtrl.ts
104 lines (86 loc) · 2.88 KB
/
todoCtrl.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
interface TodoCtrlScope extends ng.IScope {
todos:Todo[];
newTodo:string;
editedTodo:Todo;
originalTodo:Todo;
remainingCount:number;
completedCount:number;
allChecked:boolean;
status:string;
statusFilter:{completed:boolean};
addTodo();
editTodo(todo:Todo);
doneEditing(todo:Todo);
revertEditing(todo:Todo);
removeTodo(todo:Todo);
clearCompletedTodos(todo);
markAll(completed:boolean);
}
interface TodoCtrlRouteParams {
status:string;
}
/**
* The main controller for the app. The controller:
* - retrieves and persists the model via the todoStorage service
* - exposes the model to the template and provides event handlers
*/
todomvc.controller('TodoCtrl', function TodoCtrl($scope:TodoCtrlScope, $routeParams:TodoCtrlRouteParams, todoStorage:TodoStorage, filterFilter) {
var todos = $scope.todos = todoStorage.get();
$scope.newTodo = ""
$scope.editedTodo = null;
$scope.$watch('todos', function(newValue, oldValue) {
$scope.remainingCount = filterFilter(todos, { completed: false }).length;
$scope.completedCount = todos.length - $scope.remainingCount;
$scope.allChecked = !$scope.remainingCount;
if (newValue !== oldValue) { // This prevents unneeded calls to the local storage
todoStorage.put(todos);
}
}, true);
// Monitor the current route for changes and adjust the filter accordingly.
$scope.$on('$routeChangeSuccess', function() {
var status = $scope.status = $routeParams.status || '';
$scope.statusFilter = (status === 'active') ?
{ completed: false } : (status === 'completed') ?
{ completed: true } : null;
});
$scope.addTodo = function() {
var newTodo = $scope.newTodo.trim();
if (!newTodo.length) {
return;
}
todos.push({
title: newTodo,
completed: false
});
$scope.newTodo = '';
};
$scope.editTodo = function(todo) {
$scope.editedTodo = todo;
// Clone the original todo to restore it on demand.
$scope.originalTodo = angular.extend({}, todo);
};
$scope.doneEditing = function(todo) {
$scope.editedTodo = null;
todo.title = todo.title.trim();
if (!todo.title) {
$scope.removeTodo(todo);
}
};
$scope.revertEditing = function(todo) {
todos[todos.indexOf(todo)] = $scope.originalTodo;
$scope.doneEditing($scope.originalTodo);
};
$scope.removeTodo = function(todo) {
todos.splice(todos.indexOf(todo), 1);
};
$scope.clearCompletedTodos = function() {
$scope.todos = todos = todos.filter(function (val) {
return !val.completed;
});
};
$scope.markAll = function(completed) {
todos.forEach(function(todo) {
todo.completed = !completed;
});
};
});