-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (99 loc) · 2.94 KB
/
app.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
var todomvc = angular.module('todomvc', ['firebase']);
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, $firebase){
var fireRef = new $firebase('database');
$scope.todos = $firebase(fireRef).$asArray();
$scope.newTodo = '';
$scope.editedTodo = null;
$scope.$watch('todos', function(){
var total = 0;
var remaining = 0;
var completed = 0;
var deleted = 0;
$scope.todos.forEach(function(todo){
total++;
if (todo.completed === false){
remaining++;
}
});
$scope.totalCount = total
$scope.remainingCount = remaining;
$scope.totalCompleted = completed
$scope.totalDeleted = deleted
$scope.allchecked = remaining === 0;
}, true);
$scope.addTodo = function(){
var newTodo = $scope.newTodo.trim();
if (!newTodo.length){
return;
}
// add to firebase
$scope.todos.add({
title: newTodo,
completed: false,
deleted: false
});
$scope.newTodo = '';
};
$scope.editTodo = function(todo){
$scope.editedTodo = todo;
$scope.originalTodo = angular.extend({}, $scope.editedTodo);
};
// update todo
$scope.doneEditing = function(todo){
$scope.editedTodo = null;
var title = todo.title.trim();
if (title) {
$scope.todos.$save(todo);
}else{
$scope.removeTodo(todo);
}
};
$scope.removeTodo = function(todo){
$scope.tofos.$remove(todo);
};
// delete completed
$scope.clearCompletedTodos = function(){
angular.forEach($scope.todos, function(todos){
if(todo.completed){
$scope.todos.$remove(todo);
}
});
};
//toggle completetion status of all todos
$scope.markAll = function(allCompleted){
console.log(allCompleted)
angular.forEach($scope.todos, function(todo){
todo.completed = allCompleted;
console.log(todo)
$scope.todos.$save(todo);
});
};
$scope.markAll = function(allDeleted){
console.log(allDeleted)
angular.forEach($scope.todos, function(todo){
todo.deleted = allDeleted;
console.log(todo)
$scope.todos.$save(todo);
});
};
})
todomvc.directive('todoFocus', function todoFocus ($timeout){
return function(scope, elem, attrs){
scope.$watch(attrs.tofoFocus, function(newVal){
if (newVal){
$timeout(function(){
//sets focus for edit
elem[0].focus();
}), 0, false
}
})
}
});
todomvc.directive('todoBlur', function (){
return function (scope, elem, attrs){
elem.bind('blur', function(){
//run function
scope.$apply(attrs.todoBlur);
})
};
});