Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
/*
2
* Copyright (c) 2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* TodoStore
10
*/
11
12
var AppDispatcher = require('../dispatcher/AppDispatcher');
13
var EventEmitter = require('events').EventEmitter;
14
var TodoConstants = require('../constants/TodoConstants');
15
var assign = require('object-assign');
16
17
var CHANGE_EVENT = 'change';
18
19
var _todos = {};
20
21
/**
22
* Create a TODO item.
23
* @param {string} text The content of the TODO
24
*/
25
function create(text) {
26
// Hand waving here -- not showing how this interacts with XHR or persistent
27
// server-side storage.
28
// Using the current timestamp + random number in place of a real id.
29
var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
30
_todos[id] = {
31
id: id,
32
complete: false,
33
text: text
34
};
35
}
36
37
/**
38
* Update a TODO item.
39
* @param {string} id
40
* @param {object} updates An object literal containing only the data to be
41
* updated.
42
*/
43
function update(id, updates) {
44
_todos[id] = assign({}, _todos[id], updates);
45
}
46
47
/**
48
* Update all of the TODO items with the same object.
49
* the data to be updated. Used to mark all TODOs as completed.
50
* @param {object} updates An object literal containing only the data to be
51
* updated.
52
53
*/
54
function updateAll(updates) {
55
for (var id in _todos) {
56
update(id, updates);
57
}
58
}
59
60
/**
61
* Delete a TODO item.
62
* @param {string} id
63
*/
64
function destroy(id) {
65
delete _todos[id];
66
}
67
68
/**
69
* Delete all the completed TODO items.
70
*/
71
function destroyCompleted() {
72
for (var id in _todos) {
73
if (_todos[id].complete) {
74
destroy(id);
75
}
76
}
77
}
78
79
var TodoStore = assign({}, EventEmitter.prototype, {
80
81
/**
82
* Tests whether all the remaining TODO items are marked as completed.
83
* @return {boolean}
84
*/
85
areAllComplete: function() {
86
for (var id in _todos) {
87
if (!_todos[id].complete) {
88
return false;
89
}
90
}
91
return true;
92
},
93
94
/**
95
* Get the entire collection of TODOs.
96
* @return {object}
97
*/
98
getAll: function() {
99
return _todos;
100
},
101
102
emitChange: function() {
103
this.emit(CHANGE_EVENT);
104
},
105
106
/**
107
* @param {function} callback
108
*/
109
addChangeListener: function(callback) {
110
this.on(CHANGE_EVENT, callback);
111
},
112
113
/**
114
* @param {function} callback
115
*/
116
removeChangeListener: function(callback) {
117
this.removeListener(CHANGE_EVENT, callback);
118
}
119
});
120
121
// Register callback to handle all updates
122
AppDispatcher.register(function(action) {
123
var text;
124
125
switch(action.actionType) {
126
case TodoConstants.TODO_CREATE:
127
text = action.text.trim();
128
if (text !== '') {
129
create(text);
130
TodoStore.emitChange();
131
}
132
break;
133
134
case TodoConstants.TODO_TOGGLE_COMPLETE_ALL:
135
if (TodoStore.areAllComplete()) {
136
updateAll({complete: false});
137
} else {
138
updateAll({complete: true});
139
}
140
TodoStore.emitChange();
141
break;
142
143
case TodoConstants.TODO_UNDO_COMPLETE:
144
update(action.id, {complete: false});
145
TodoStore.emitChange();
146
break;
147
148
case TodoConstants.TODO_COMPLETE:
149
update(action.id, {complete: true});
150
TodoStore.emitChange();
151
break;
152
153
case TodoConstants.TODO_UPDATE_TEXT:
154
text = action.text.trim();
155
if (text !== '') {
156
update(action.id, {text: text});
157
TodoStore.emitChange();
158
}
159
break;
160
161
case TodoConstants.TODO_DESTROY:
162
destroy(action.id);
163
TodoStore.emitChange();
164
break;
165
166
case TodoConstants.TODO_DESTROY_COMPLETED:
167
destroyCompleted();
168
TodoStore.emitChange();
169
break;
170
171
default:
172
// no op
173
}
174
});
175
176
module.exports = TodoStore;
177
178