-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
123 lines (111 loc) · 4.49 KB
/
todo.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
112
113
114
115
116
117
118
119
120
121
122
123
angular.module('todoApp', ['ui.bootstrap', 'indexedDB'])
.config(function ($indexedDBProvider) {
$indexedDBProvider
.connection('todoDB')
.upgradeDatabase(1, function(event, db, tx){
var objStore = db.createObjectStore('todos', {keyPath: "created"});
console.log("Store created");
}).upgradeDatabase(2, function(event, db, tx){
db.deleteObjectStore('todos');
var objStore = db.createObjectStore('todos', {keyPath: "key", autoIncrement: true});
});
});
angular.module('todoApp')
.controller('TodoController', ['$scope', '$indexedDB', '$log', function($scope, $indexedDB, $log) {
$scope.todos = [];
$scope.activeTags = [];
$scope.tagsSorting = "-count";
$indexedDB.openStore('todos', function(todos){
todos.getAll().then(function(todos) {
$log.debug("Got the items from store");
angular.forEach(todos, function(todo){
todo.selected = false;
});
$scope.todos = todos;
});
});
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};
$scope.completeSelected = function(){
$indexedDB.openStore('todos', function(todoStore){
angular.forEach($scope.todos, function(todo){
if (todo.selected) {
todo.selected = false;
todo.completed = new Date();
todo.done = true;
todoStore.upsert(todo).then(function(){
$log.debug("Updated an object");
$scope.updateTags();
});
}
});
});
};
$scope.searchFor = function (str){
$scope.searchString = str;
$log.debug(str);
};
function setPriority(priority) {
$indexedDB.openStore('todos', function(todoStore){
angular.forEach($scope.todos, function(todo){
if (todo.selected) {
todo.priority = priority;
todoStore.upsert(todo).then(function(){
$log.debug("Updated an object");
});
}
});
});
}
$scope.selectAllNotCompleted = function(select) {
angular.forEach($scope.todos, function (todo) {
if (!todo.done){
todo.selected = select;
}
});
};
$scope.keyPressed = function($event){
if ($event.keyCode == 65 || $event.keyCode == 97) {
$scope.selectAllNotCompleted(true);
} else if ($event.keyCode == 78 || $event.keyCode == 110) {
$scope.selectAllNotCompleted(false);
} else if ($event.keyCode == 67 || $event.keyCode == 99) {
$scope.searchString = '';
} else if ($event.keyCode >= 49 && $event.keyCode <= 51) {
setPriority($event.keyCode - 48);
}
$log.debug('Key pressed: ' + $event.keyCode);
};
$scope.updateTags = function (){
$log.debug("Updating tags array");
var activeTagsMap = {};
angular.forEach($scope.todos, function(todo){
if (!todo.done) {
angular.forEach(todo.tags, function (tag){
if (tag in activeTagsMap) {
var oldValue = activeTagsMap[tag];
oldValue ++;
activeTagsMap[tag] = oldValue;
} else {
activeTagsMap[tag] = 1;
}
});
}
});
var activeTags = [];
angular.forEach(activeTagsMap, function(count, tag){
$log.debug("adding tag " + tag + ": " + count);
this.push ({"tag":tag, "count":count});
}, activeTags);
$scope.activeTags = activeTags;
};
$scope.$watch('todos', $scope.updateTags);
$scope.doNothingOnKeyPress = function($event){
$event.stopImmediatePropagation();
};
}]);