forked from iamvijaydev/TaskTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
216 lines (179 loc) · 5.09 KB
/
model.js
File metadata and controls
216 lines (179 loc) · 5.09 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Task Tracker: A Google Chrome plugin to track your task, with timer
* By Vijay Dev http://vijaydev.com/
* MIT Licensed.
*/
/* Create a new task
* @constructor
* @param {String} title - title of the task.
* @param {String} status - current status of the task.
* @param {Number} sleepTime - when plugin is closed, record the time for the active task.
*/
var Task = function (id, title, status, sleepTime) {
this.id = id;
this.title = title;
this.status = 'create'; // create, active, pause, complete
this.startTime = 0;
this.duration = 0;
};
/* Model for the task data
* @constructor
*/
var Model = function () {
// a constant localStorage key name.
var STORAGE = 'TASK_TRACKER';
/* Get the data from localStorage
* @private
* @return {Array} associative array
*/
var getLocalStorage = function() {
return JSON.parse( localStorage.getItem(STORAGE) );
};
/* Set the data to localStorage
* @private
* @param {Array} taskArray - to stringify & saved
*/
var setLocalStorage = function(taskArray) {
localStorage.setItem( STORAGE, JSON.stringify(taskArray) );
};
/* Returns Date.now()
* @private
* @return {Number} time in seconds
*/
var now = function() {
return Date.now ? Date.now() : +(new Date);
};
/* Returns the (i)th position having the requested id
* @private
* @return {Number} (i)th position
*/
var getTaskPos = function(id) {
var taskArray = getLocalStorage();
var i = 0;
for(i in taskArray) {
if(taskArray[i].id === id){
break;
}
}
return i;
};
/* Add a new task
* @param {String} title - title of the new task
* @dispatch {tEvent} taskAdded - data { id, title, status }
*/
var add = function(title) {
var id = 'task' + now();
var task = new Task(id, title);
var taskArray = getLocalStorage();
taskArray[taskArray.length] = task;
setLocalStorage( taskArray );
var tEvent = document.createEvent('Event');
tEvent.initEvent('tasksAdded', true, true);
tEvent.data = {
'id': id,
'title': title,
'status': task.status,
'startTime': task.startTime
}
window.dispatchEvent(tEvent);
log('M: ' + id + ' added');
};
/* Discard a task
* @param {String} id - ID of the task
* @dispatch {tEvent} taskDiscarded - data { id }
*/
var discard = function(id) {
var taskArray = getLocalStorage();
taskArray.remove( getTaskPos(id) );
setLocalStorage( taskArray );
var tEvent = document.createEvent('Event');
tEvent.initEvent('taskDiscarded', true, true);
tEvent.data = {
'id': id
}
window.dispatchEvent(tEvent);
log('M: ' + id + ' discarded');
};
/* Update a task
* @param {String} id - ID of the task
* @param {String} status - current status of the task.
* @dispatch {tEvent} taskUpdated - data { id, status, duration }
*/
var update = function(options) {
var taskArray = getLocalStorage();
var i = getTaskPos(options.id);
taskArray[i].status = options.status;
switch(options.status) {
case 'active':
taskArray[i].startTime = options.time;
break;
case 'pause':
taskArray[i].duration += options.time - taskArray[i].startTime;
log(taskArray[i].duration);
break;
case 'complete':
taskArray[i].duration += options.time - taskArray[i].startTime;
break;
}
var tEvent = document.createEvent('Event');
tEvent.initEvent('taskUpdated', true, true);
tEvent.data = {
'id': options.id,
'status': options.status,
'startTime': options.time,
'duration': taskArray[i].duration
}
window.dispatchEvent(tEvent);
setLocalStorage( taskArray );
};
/* Created localStorage entry for the first time
* @private
* @dispatch {tEvent} storageCreated
*/
var createStorage = function() {
var taskJSON = [];
setLocalStorage(taskJSON)
var tEvent = document.createEvent('Event');
tEvent.initEvent('storageCreated', true, true);
window.dispatchEvent(tEvent);
log('M: localStorage created');
};
/* Inform view that there are tasks in localStorage
* @private
* @dispatch {tEvent} hasTasks - data { taskArray }
*/
var informHasTask = function() {
var tEvent = document.createEvent('Event');
tEvent.initEvent('hasTasks', true, true);
tEvent.data = {
'taskArray': getLocalStorage()
}
window.dispatchEvent(tEvent);
log('M: has previous task');
};
// Fire when initialized
this.init = function() {
// Initialize localStorage
if( localStorage.getItem(STORAGE) ) {
informHasTask();
} else {
createStorage();
}
// When user adds a new task
window.addEventListener('newTask', function(e) {
add( e.data.title );
}, false);
// When user discards an existing task
window.addEventListener('discardTask', function(e) {
discard( e.data.id );
}, false);
// When task status/time needs to be updated
window.addEventListener('updateTask', function(e) {
update({
'id': e.data.id,
'status': e.data.status,
'time': e.data.time
});
}, false);
};
this.init();
};