-
Notifications
You must be signed in to change notification settings - Fork 1
/
clock.js
184 lines (161 loc) · 4.47 KB
/
clock.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
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
/*
* TODO: Add copyright
* Add unit tests
* Stop chimes on stop()
* Documentation
* Lint
*/
function create_clock(opts) {
function validate_2digit_int(name, values, defaults) {
var value = values[name];
var defaultValue = defaults[name];
var asInt = parseInt(values[name], 10);
if (value == "" || value === undefined) {
values[name] = 0;
} else if (value != asInt) {
console.log(`${name} value was not integer ("${value}"), using default: ${defaultValue}`);
values[name] = defaultValue;
} else if (asInt < 0 || asInt > 99) {
console.log(`${name} value was out of range (${value}), using default: ${defaultValue}`);
values[name] = defaultValue;
} else if (value !== asInt) {
values[name] = asInt;
}
}
var defaults = {
round: 1,
rounds: 0,
duration_mins: 13,
duration_secs: 0,
warning_mins: 0,
warning_secs: 30,
}
if (opts === undefined) {
opts = {};
}
validate_2digit_int("round", opts, defaults);
validate_2digit_int("rounds", opts, defaults);
validate_2digit_int("duration_mins", opts, defaults);
validate_2digit_int("duration_secs", opts, defaults);
validate_2digit_int("warning_mins", opts, defaults);
validate_2digit_int("warning_secs", opts, defaults);
function to2Digits(number) {
return ("0" + number).slice(-2)
}
var round = $(document.createElement("div"))
.attr("class", "clock-round");
var time = $(document.createElement("div"))
.attr("class", "clock-time")
.addClass("paused")
.append(document.createTextNode("00 : 00"));
var top = $(document.createElement("div"))
.append(round)
.append(time);
var clock = {
round: opts.round,
rounds: opts.rounds,
duration: opts.duration_mins * 60 + opts.duration_secs,
warning: opts.warning_mins * 60 + opts.warning_secs,
dom: top,
chimeSound: new buzz.sound("chime.mp3"),
updateTimeDisplay: function() {
var now = clock.time + 29;
var mins = to2Digits(Math.floor(now / 60));
var secs = to2Digits(Math.floor(now % 60 / 30) * 30);
time.text(`${mins} : ${secs}`);
},
updateRoundDisplay: function() {
var ofText;
if (clock.rounds == 0) {
ofText = ""
} else {
ofText = ` of ${clock.rounds}`;
}
round.text(`Round ${clock.round}${ofText}`);
},
reset: function() {
clock.time = clock.duration;
clock.updateTimeDisplay();
clock.updateRoundDisplay();
},
next: function() {
if (clock.round != clock.rounds) {
clock.round += 1;
clock.reset();
} else {
clock.flash();
clock.stop();
clock.time = 0;
clock.updateTimeDisplay();
}
},
tick: function(delta) {
if (typeof delta == 'undefined') {
delta = -1;
}
clock.time += delta;
clock.updateTimeDisplay();
if (clock.time <= 0) {
clock.chime(3);
clock.next();
} else if (clock.time == clock.warning) {
clock.chime(1);
}
},
start: function() {
if (clock.time > 0) {
clock.ticker = setInterval(clock.tick, 1000);
time.removeClass("paused");
}
clock.flash();
},
stop: function() {
clearInterval(clock.ticker);
clock.ticker = undefined;
time.addClass("paused");
},
flash: function(duration) {
duration = duration || 350;
time.css("visibility", "hidden");
setTimeout(function() {
time.css("visibility", "visible");
}, duration);
},
chime: function(count) {
clock.chimeSound
.play()
.loop()
.bind("playing", function(e) {
if (count-- == 0) {
clock.chimeSound.stop();
} else {
clock.flash(clock.chimeSound.getDuration() * 650);
}
});
},
keydownHandler: function(evt) {
if (evt.which == 32) {
if (clock.ticker === undefined) {
if (clock.time == 0) {
clock.next();
}
clock.start();
} else {
clock.stop();
}
} else if (evt.which == 78) {
clock.next();
} else if (evt.which == 219) {
clock.tick(-60);
} else if (evt.which == 221) {
clock.tick(60);
}
},
}
if (clock.warning >= clock.duration) {
console.log("warning time greater than or equal to duration: ignoring");
clock.warning = undefined;
}
clock.reset();
return clock;
}