-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkeytype.html
345 lines (313 loc) · 11 KB
/
monkeytype.html
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<title>MonkeyType</title>
<style type="text/css">
body {
background-color: #F2EDC4;
color: #7A674F;
}
#text {
float: left;
margin: 10px 20px 10px 10px;
height: 590px;
width: 600px;
font-family: times, serif;
font-size: 18px;
}
#stats {
font-size: 30px;
}
#timer {
font-size: 60px;
}
#footer {
clear: both;
}
.start-stop-button {
font-size: 18px;
padding: 8px 60px;
}
.text-align-right {
text-align: right;
}
.display-none {
display: none;
}
.display-inline {
display:
}
.background-color-green {
background-color: #CED452;
}
.background-color-red {
background-color: #F8CD9F;
}
</style>
<script src="jquery-1.5.1.min.js"></script>
<script>
/*
TODO:
* on countdown, make sure all activity stops at 0, not -1
* 10 second inactivity warning
* 10 seconds left on timer warning
*/
var interval = 0;
var seconds = 0;
var firstKeyup = true;
var timerType = 'countup'; // options are "countup" and "countdown"
/**
* Get the word count for the provided text.
* @param {String} text
* @return {Number}
*/
function getWordCount(text) {
// Set word count by filtering tokens that don't contain word characters.
var tokens = text.split(/\s+/);
var words = $.grep(tokens, function(value) {
return value.match(/\w/);
});
return words.length;
}
/**
* Get the percentage of progress towards the word count goal.
* @param {int} wordCount
* @return {Number}
*/
function getWordCountPercent(wordCount) {
var wordCountGoal = parseInt($('#word-count-goal').val());
var percent = Math.floor((wordCount / wordCountGoal) * 100);
return percent;
}
/**
* Get the words per minute for the provided word count, given the number of
* seconds since the first keyup.
* @param {Number} wordCount
* @return {Number}
*/
function getWordsPerMinute(wordCount) {
if ('countup' == timerType) {
var wordsPerMinute = Math.round(wordCount / (seconds / 60));
} else if ('countdown' == timerType) {
// Freeze words per minute once timer gets to 00:00:00.
if (1 == seconds) {
var wordsPerMinute = parseInt($('#stat-words-per-minute').text());
} else {
var countdownMinutes = parseInt($('#timer-type-countdown-minutes').val());
var secondsPassed = (countdownMinutes * 60) - seconds;
var wordsPerMinute = Math.round(wordCount / (secondsPassed / 60));
}
}
return wordsPerMinute;
}
/**
* Get the words remaining based on the word count goal.
* @param {Number} wordCount
* @return {Number}
*/
function getWordsRemaining(wordCount) {
var wordCountGoal = parseInt($('#word-count-goal').val());
var wordsRemaining = wordCount >= wordCountGoal ? 0 : wordCountGoal - wordCount;
return wordsRemaining;
}
/**
* Set the timer on the page.
* @param {Number} second
* @param {Number} minute
* @param {Number} hour
*/
function setTimer() {
var hour = Math.floor(seconds / 3600);
var minute = Math.floor(seconds / 60);
var second = Math.floor(seconds - (minute * 60));
$('#timer-second').text('0'.substring(9 < second) + second);
$('#timer-minute').text('0'.substring(9 < minute) + minute);
$('#timer-hour').text('0'.substring(9 < hour) + hour);
}
/**
* Set the intitial countdown timer.
*/
function setTimerCountdown() {
var countdownMinutes = $('#timer-type-countdown-minutes').val();
seconds = countdownMinutes * 60;
setTimer();
}
/**
* Set the initial countup timer.
*/
function setTimerCountup() {
setTimer();
}
/**
* Start the timer according to its type.
*/
function timerStart() {
interval = setInterval(function() {
// Increment or decrement the seconds depending on the timer type.
'countup' == timerType ? seconds++ : seconds--;
setTimer();
// For countdown, stop the timer once it gets to 00:00:00.
if (0 == seconds) {
timerStop();
$('#timer-stop').attr('disabled', 'disabled');
$('#text').addClass('background-color-red');
}
}, 1000);
}
/**
* Stop the timer.
*/
function timerStop() {
clearInterval(interval);
interval = 0;
}
/**
* Bind event handlers when document is ready.
*/
$(document).ready(function() {
// Set defaults.
$('#text').val('');
$('#text').removeAttr('disabled');
$('#word-count-goal').val(0);
$('#timer-type-countdown-minutes').val(0);
$('#timer-type-countdown').removeAttr('checked');
$('#timer-type-countup').attr('checked', 'checked');
$('#timer-stop').removeAttr('disabled');
$('.timer-type-countdown-increment').attr('disabled', 'disabled');
// When writing in the textarea.
$('#text').keyup(function(e) {
// Start timer on first keyup.
if (firstKeyup) {
$('#timer-controls').removeClass('display-none').addClass('display-inline');
$('#config-controls').addClass('display-none');
// Consider a 0-minute countdown a countup.
if (0 == $('#timer-type-countdown-minutes').val()) {
timerType = 'countup';
}
timerStart();
firstKeyup = false;
}
// Get the text.
var text = $(this).val();
// Set word count.
var wordCount = getWordCount(text);
// Set words per minute.
var wordsPerMinute = getWordsPerMinute(wordCount);
// Set word count percent.
var wordCountPercent = getWordCountPercent(wordCount);
// Set words remaining.
var wordsRemaining = getWordsRemaining(wordCount);
// Change the background color to green if the word cound goal has been
// reached and the timer is not 00:00:00.
if (0 == wordsRemaining
&& 0 < $('#word-count-goal').val()
&& 0 < seconds) {
$(this).addClass('background-color-green');
}
$('#stat-word-count').text(wordCount);
$('#stat-words-per-minute').text(wordsPerMinute);
$('#stat-word-count-percent').text(wordCountPercent);
$('#stat-words-remaining').text(wordsRemaining);
});
// When clicking the "Start Timer" button.
$('#timer-start').click(function() {
$('#text').removeAttr('disabled');
$(this).removeClass('display-inline').addClass('display-none');
$('#timer-stop').removeClass('display-none').addClass('display-inline');
timerStart();
firstKeyup = false;
});
// When clicking the "Stop Timer" button.
$('#timer-stop').click(function() {
$('#text').attr('disabled', 'disabled');
$(this).removeClass('display-inline').addClass('display-none');
$('#timer-start').removeClass('display-none').addClass('display-inline');
timerStop();
});
// When clicking the "Reset All" button.
$('#reset-all').click(function() {
var confirmMessage = 'THIS WILL DELETE YOUR TEXT! Are you sure you want to start another session?'
if (window.confirm(confirmMessage)) {
location.reload();
}
});
// When changing the "Count up"/"Countdown" radio buttons.
$('input[name="timer-type"]').change(function() {
timerType = $(this).val();
if ('countup' == timerType) {
$('.timer-type-countdown-increment').attr('disabled', 'disabled');
setTimerCountup();
} else if ('countdown' == timerType) {
$('.timer-type-countdown-increment').removeAttr('disabled');
setTimerCountdown();
}
});
// When clicking the "Countdown" timer type increment buttons.
$('.timer-type-countdown-increment').click(function() {
var minutes = parseInt($('#timer-type-countdown-minutes').val());
var increment = parseInt($(this).val());
minutes = increment ? minutes + increment : 0;
$('#timer-type-countdown-minutes').val(minutes);
setTimerCountdown();
});
// When clicking the word count goal increment buttons.
$('.word-count-goal-increment').click(function() {
var goal = parseInt($('#word-count-goal').val());
var increment = parseInt($(this).val());
goal = increment ? goal + increment : 0;
$('#word-count-goal').val(goal);
$('#stat-word-count-goal').text(goal);
$('#stat-words-remaining').text(goal);
});
});
</script>
</head>
<body>
<textarea id="text"></textarea>
<div id="sidebar">
<div id="stats">
<div><span id="stat-word-count">0</span> / <span id="stat-word-count-goal">0</span> words (<span id="stat-word-count-percent">0</span>%)</div>
<div><span id="stat-words-remaining">0</span> words remaining</div>
<div><span id="stat-words-per-minute">0</span> words per minute</div>
</div>
<div id="timer">
<span id="timer-hour">00</span>:<span id="timer-minute">00</span>:<span id="timer-second">00</span>
</div>
<div id="timer-controls" class="display-none">
<button id="timer-start" class="start-stop-button display-none">Start Timer</button>
<button id="timer-stop" class="start-stop-button">Stop Timer</button>
<p>Green means you've reached your goal.</p>
<p>Red means you've run out of time.</p>
<p>Click <button id="reset-all">Reset All</button> to start another session.</p>
</div>
<div id="config-controls">
<h2>Welcome to <i>MonkeyType</i>!</h2>
<h2>Just start typing! Or...</h2>
<p>Set your word count goal...</p>
<input type="text" size="4" id="word-count-goal" class="text-align-right" value="0" disabled="disabled" /> words
<div>
<button class="word-count-goal-increment" value="25">+25</button>
<button class="word-count-goal-increment" value="50">+50</button>
<button class="word-count-goal-increment" value="100">+100</button>
<button class="word-count-goal-increment" value="250">+250</button>
<button class="word-count-goal-increment" value="0">0</button>
</div>
<p>Now choose your timer...</p>
<input type="radio" id="timer-type-countup" name="timer-type" value="countup" /> Count up<br />
<input type="radio" id="timer-type-countdown" name="timer-type" value="countdown" /> Countdown for
<input type="text" size="2" id="timer-type-countdown-minutes" class="text-align-right" value="0" disabled="disabled" /> minutes
<div>
<button class="timer-type-countdown-increment" value="1">+1</button>
<button class="timer-type-countdown-increment" value="5">+5</button>
<button class="timer-type-countdown-increment" value="10">+10</button>
<button class="timer-type-countdown-increment" value="0">0</button>
</div>
<p>The timer will start once you start typing!</p>
</div>
<div id="footer">
<p>©2011 <i>MonkeyType</i> is a speed writing tool for simian writers.</p>
</div>
</div>
</body>
</html>