This repository was archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview.js
More file actions
219 lines (168 loc) · 5.19 KB
/
view.js
File metadata and controls
219 lines (168 loc) · 5.19 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
217
218
var View = function(taskList) {
// cache the taskList UL dom element
// TODO: move to script, pass here by value
var taskList = document.getElementById('task_list');
var self = this;
self.timer = null;
self.startTime = 0;
self.duration = 0;
self.hhmmss = null;
// if the time entity is single digit, prefix 0
var preFix = function(val) {
return ('' + val).length === 2 ? '' + val : '0' + val;
}
// Convert ms into HH:MM:SS format
var toHHMMSS = function(ms) {
var time = '';
var decs = Math.floor(ms % 1000 / 100);
var secs = Math.floor(ms % 60000 / 1000);
var mins = Math.floor(ms % 3600000 / 60000);
var hour = Math.floor(ms % (24 * 3600000) / 3600000);
if(hour > 0) {
time += preFix(hour) + ':';
}
if(mins > 0) {
time += preFix(mins) + ':';
}
time += preFix(secs) + '.' + decs;
return time;
}
/* Get Date.now()
* @return {Number} time in miliseconds
*/
var now = function() {
return Date.now ? Date.now() : +(new Date);
};
function runTimer() {
var totalDuration = (now() - self.startTime) + self.duration;
self.hhmmss.innerHTML = toHHMMSS(totalDuration);
}
function stopTimer() {
if(self.timer) {
clearInterval(self.timer);
self.timer = null;
}
}
// start the counter for active task
var initTimer = function(options) {
// if there is a timer currently running, stop that
// also checked in the controler, & informs the model
if(self._timer) {
stopTimer();
var parent = self.hhmmss.parentNode;
parent.setAttribute( 'data-status', 'pause' );
}
var task = document.getElementById(options.id);
self.startTime = options.startTime;
self.duration = options.duration;
self.hhmmss = task.lastChild;
self.timer = setInterval(runTimer, 100);
};
// append a single task entry into the taskList
var appendTask = function(options) {
var task = document.createElement('li');
task.id = options.id;
task.setAttribute( 'data-status', options.status );
task.setAttribute( 'data-start-time', options.startTime );
var ctrls = document.createElement('div');
ctrls.className = 'ctrls';
var start = document.createElement('button');
start.className = 'start';
var pause = document.createElement('button');
pause.className = 'pause';
var complete = document.createElement('button');
complete.className = 'complete';
var discard = document.createElement('button');
discard.className = 'discard';
var title = document.createElement('h3');
title.className = 'title';
title.innerHTML = options.title;
var duration = document.createElement('span');
duration.className = 'duration';
duration.innerHTML = options.duration ? toHHMMSS(options.duration) : '';
// append controls into ctrl
ctrls.appendChild(start);
ctrls.appendChild(pause);
ctrls.appendChild(complete);
ctrls.appendChild(discard);
// append ctrl, title, duration into task
task.appendChild(ctrls);
task.appendChild(title);
task.appendChild(duration);
// append task into taskList (DOM)
taskList.appendChild(task);
if(options.status === 'active') {
initTimer({
id: options.id,
startTime: options.startTime,
duration: options.duration
});
}
};
// build the whole task list by iterating the taskArray
var buildTaskList = function(taskArray) {
var i = 0;
var j = taskArray.length;
for(; i < j; i++) {
appendTask({
'id': taskArray[i].id,
'title': taskArray[i].title,
'status': taskArray[i].status,
'startTime': taskArray[i].startTime,
'duration': taskArray[i].duration
});
}
};
// remove a task from the task list
var removeTask = function(id) {
var task = document.getElementById(id);
task.className = 'discarding';
// when the discarding animation is completed
task.parentNode.removeChild( task );
};
// update a task
var updateTask = function(options) {
var task = document.getElementById(options.id);
task.setAttribute( 'data-status', options.status );
task.setAttribute( 'data-start-time', options.startTime );
if( options.status === 'active' ) {
initTimer({
id: options.id,
startTime: options.startTime,
duration: options.duration
});
} else {
stopTimer();
}
};
// Initilizing for the first time, localStorage key added
// When there are task that were saved in localStorage
window.addEventListener('hasTasks', function(e) {
buildTaskList( e.data.taskArray );
}, false);
// When a new task, added by the user is saved in localStorage
window.addEventListener('tasksAdded', function(e) {
appendTask({
'id': e.data.id,
'title': e.data.title,
'status': e.data.status,
'startTime': e.data.startTime,
'duration': e.data.duration
});
}, false);
// When a task, added by the user is discarded from localStorage
window.addEventListener('taskDiscarded', function(e) {
removeTask(e.data.id);
}, false);
// When a task, added by the user is updated in localStorage
window.addEventListener('taskUpdated', function(e) {
updateTask({
'id': e.data.id,
'status': e.data.status,
'startTime': e.data.startTime,
'duration': e.data.duration
});
}, false);
// create the model instance
var model = new Model();
};