Repository navigation
Expand file tree
/
Copy pathtimer_panel.js
More file actions
314 lines (265 loc) · 7.5 KB
/
Copy pathtimer_panel.js
File metadata and controls
314 lines (265 loc) · 7.5 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
define(function(require) {
'use strict';
var Panel = require('panel');
var Picker = require('picker/picker');
var asyncStorage = require('shared/js/async_storage');
var View = require('view');
var Utils = require('utils');
var Timer = require('timer');
var FormButton = require('form_button');
var _ = require('l10n').get;
var priv = new WeakMap();
function timeFromPicker(value) {
var hm, ms;
hm = value.split(':');
ms = Utils.dateMath.toMS({
hours: hm[0],
minutes: hm[1]
});
return ms;
}
/**
* Timer.Panel
*
* Construct a UI panel for the Timer panel.
*
* @return {Timer.Panel} Timer.Panel object.
*
*/
Timer.Panel = function(element) {
Panel.apply(this, arguments);
this.timer = null;
this.nodes = {};
this.picker = new Picker({
element: this.element.querySelector('#time-picker'),
pickers: {
hours: {
range: [0, 23]
},
minutes: {
range: [0, 59],
isPadded: true
}
}
});
asyncStorage.getItem('active_timer', function(result) {
if (result !== null) {
this.timer = new Timer(JSON.parse(result));
}
}.bind(this));
// Gather elements
[
'create', 'cancel', 'dialog',
'pause', 'start', 'sound', 'time', 'vibrate'
].forEach(function(id) {
this.nodes[id] = this.element.querySelector('#timer-' + id);
}, this);
// Bind click events
[
'create', 'cancel', 'pause', 'start'
].forEach(function(action) {
var element = this.nodes[action];
if (priv.has(element)) {
priv.delete(element);
}
priv.set(element, {
action: action,
panel: this
});
element.addEventListener('click', this.onclick.bind(this), false);
}, this);
var sound = this.nodes.sound;
sound.addEventListener('blur', this.pauseAlarm.bind(this), false);
sound.addEventListener('change', this.previewAlarm.bind(this), false);
var soundMenuConfig = {
id: 'timer-sound-menu',
formatLabel: function(sound) {
return (sound === null || sound === '0') ?
_('noSound') :
_(sound.replace('.', '_'));
}
};
this.soundButton = new FormButton(sound, soundMenuConfig);
this.soundButton.refresh();
View.instance(element).on(
'visibilitychange', this.onvisibilitychange.bind(this)
);
};
Timer.Panel.prototype = Object.create(Panel.prototype);
Timer.Panel.prototype. {
var nodes = this.nodes;
var timer = this.timer;
var isPaused = false;
if (isVisible) {
// No active timer, or timer has expired...
//
// - Cancel the expired timer
// - Show the new timer dialog
//
if (timer === null || timer.endAt < Date.now()) {
if (timer) {
timer.cancel();
}
this.dialog();
} else {
if (timer.state !== Timer.STARTED) {
// Active timer exists...
timer.on('tick', this.update.bind(this));
timer.on('end', this.dialog.bind(this));
if (timer.state === Timer.REACTIVATING) {
// Reviving to started state,
// show the pause button, hide the start button
this.toggle(nodes.pause, nodes.start);
} else {
// Reviving to paused state,
// show the start button, hide the pause button
this.toggle(nodes.start, nodes.pause);
isPaused = true;
}
if (timer.state === Timer.INITIALIZED) {
this.dialog();
this.update();
} else {
// Calling start will "revive" and update
// both a paused and not-paused timers.
// - Updates the endAt and resets the pauseAt
// - Sync the object.
timer.start();
if (isPaused) {
// Immediately re-pause the timer.
timer.pause();
}
this.dialog({ isVisible: false });
}
}
}
}
};
/**
* dialog Show or hide the Timer creation dialog.
*
* @param {Object} opts Optional parameters to show/hide dialog.
* - isVisible, true|false (show|hide).
* Defaults to true.
*
* @return {Object} Timer.Panel.
*/
Timer.Panel.prototype.dialog = function(opts = { isVisible: true }) {
View.instance(this.nodes.dialog).visible = opts.isVisible;
setTimeout(this.picker.reset.bind(this.picker), 0);
return this;
};
Timer.Panel.prototype.elapsed = function() {
// display the "elapsed time since notification" screen
return this;
};
/**
* update Update the Timer UI's time display
* @param {Number} remaining Seconds remaining in timer countdown.
* Defaults to 0.
*
* @return {Object} Timer.Panel.
*/
Timer.Panel.prototype.update = function(remaining = 0) {
// this should localize
// console.log(this);
this.nodes.time.textContent = Utils.format.hms(remaining, 'hh:mm:ss');
return this;
};
/**
* toggle Toggle any two UI elements with each other.
*
* @param {Node} show The node to show.
* @param {Node} hide The node to hide.
*
* @return {Object} Timer.Panel.
*/
Timer.Panel.prototype.toggle = function(show, hide) {
show.classList.remove('hidden');
hide.classList.add('hidden');
return this;
};
/**
* previewAlarm Plays the currently selected alarm value on a loop.
*/
Timer.Panel.prototype.previewAlarm = function() {
if (!this.ringtonePlayer) {
this.ringtonePlayer = new Audio();
this.ringtonePlayer.mozAudioChannelType = 'alarm';
this.ringtonePlayer.loop = true;
}
this.ringtonePlayer.pause();
var ringtoneName = Utils.getSelectedValue(this.nodes.sound);
var previewRingtone = 'shared/resources/media/alarms/' + ringtoneName;
this.ringtonePlayer.src = previewRingtone;
this.ringtonePlayer.play();
};
/**
* pauseAlarm stops the alarm if it is playing
*/
Timer.Panel.prototype.pauseAlarm = function() {
if (this.ringtonePlayer) {
this.ringtonePlayer.pause();
}
};
/**
* handleEvent Handler for all panel bound UI events.
* (`this` context object is not Timer.Panel)
*
* @param {Event} event The Event object.
*/
Timer.Panel.prototype. {
var meta = priv.get(event.target);
var panel = meta.panel;
var nodes = panel.nodes;
var time;
if (panel.timer && panel.timer[meta.action]) {
// meta.action => panel.timer[meta.action]()
//
// ie.
//
// if start => panel.timer.start()
// if pause => panel.timer.pause()
// if cancel => panel.timer.cancel()
//
panel.timer[meta.action]();
if (meta.action === 'cancel' || meta.action === 'new') {
// Reset shared timer object
panel.timer = null;
// Restore the panel to 00:00:00
panel.update(0);
// Show new timer dialog
panel.dialog();
}
if (meta.action === 'start') {
panel.toggle(nodes.pause, nodes.start);
}
if (meta.action === 'pause') {
panel.toggle(nodes.start, nodes.pause);
}
} else {
if (meta.action === 'create') {
time = timeFromPicker(panel.picker.value);
if (!time) {
return;
}
// Create a new Timer with the
// selected duration time.
panel.timer = new Timer({
sound: nodes.sound.value,
duration: time,
vibrate: nodes.vibrate.checked
});
// Bind the tick and end events to the
// newly created timer object.
panel.timer.on('tick', panel.update.bind(panel)).start();
panel.timer.on('end', panel.dialog.bind(panel));
// Update the UI
panel.toggle(nodes.pause, nodes.start);
// Hide the new timer dialog
panel.dialog({ isVisible: false });
}
}
};
return Timer.Panel;
});