-
Notifications
You must be signed in to change notification settings - Fork 1
/
todos.js
146 lines (131 loc) · 3.81 KB
/
todos.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
define(
[
'scripts/config',
'durandal/app',
'durandal/system',
'scripts/dataservice',
'viewmodels/model'
],
function(config, app, system, dataservice, model) {
'use strict';
var todos = ko.observableArray([]),
current = ko.observable(), // store the new todo value being entered
showMode = ko.observable('all');
// count of all completed todos
var completedCount = ko.computed(function () {
return ko.utils.arrayFilter(todos(), function (todo) {
if(!todo) return;
return todo.completed();
}).length;
});
// count of todos that are not complete
var remainingCount = ko.computed(function () {
return todos().length - completedCount();
});
// writeable computed observable to handle marking all complete/incomplete
var allCompleted = ko.computed({
//always return true/false based on the done flag of all todos
read: function () {
return !remainingCount();
},
// set all todos to the written value (true/false)
write: function (newValue) {
ko.utils.arrayForEach(todos(), function (todo) {
// set even if value is the same, as subscribers are not notified in that case
todo.completed(newValue);
});
}
});
//filter the todos based on the completion status
var filteredTodos = ko.computed(function () {
switch (showMode()) {
case 'active':
return todos().filter(function (todo) {
return !todo.completed();
});
case 'completed':
return todos().filter(function (todo) {
return todo.completed();
});
default:
return todos();
}
});
// add a new todo, when enter key is pressed
var add = function() {
var current = this.current().trim();
if (current) {
todos.push(new model.Todo(current, false));
this.current('');
}
};
// remove a single todo
var remove = function(todo) {
todos.remove(todo);
};
// remove all completed todos
var removeCompleted = function() {
this.todos.remove(function(todo) {
return todo.completed();
});
};
// edit an item
var editItem = function(item) {
item.editing(true);
};
// stop editing an item. Remove the item, if it is now empty
var stopEditing = function(item) {
item.editing(false);
if (!item.title().trim()) {
remove(item);
}
};
// helper function to keep expressions out of markup
var getLabel = function(count) {
return ko.utils.unwrapObservable(count) === 1 ? 'item' : 'items';
};
function init(){
// internal computed observable that fires whenever anything changes in our todos
ko.computed(function () {
// store a clean copy to local storage, which also creates a dependency on the observableArray and all observables in each item
localStorage.setItem(config.localStorageItem, ko.toJSON( todos ));
}).extend({
throttle: 500
}); // save at most twice per second
}
return {
displayName: 'Todos',
activate: function(data){
//Fetch local data, if exists
todos = dataservice.getTodos(todos);
//Update the mode
if(data.mode){
showMode(data.mode);
}else{
showMode('all');
}
// internal computed observable that fires whenever anything changes in our todos
ko.computed(function() {
// store a clean copy to local storage, which also creates a dependency on the observableArray and all observables in each item
localStorage.setItem( config.localStorageItem, ko.toJSON( todos ) );
}).extend({
throttle: 500
}); // save at most twice per second
},
init: init,
todos: todos,
current: current,
showMode: showMode,
add: add,
remove: remove,
removeCompleted: removeCompleted,
editItem: editItem,
stopEditing: stopEditing,
getLabel: getLabel,
filteredTodos: filteredTodos,
allCompleted: allCompleted,
remainingCount: remainingCount,
completedCount: completedCount
};
}
);