-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (153 loc) · 5.33 KB
/
script.js
File metadata and controls
199 lines (153 loc) · 5.33 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
// This file contains scripts that control the functionality of the webpage. All calculations are performed client-side.
/**
* @function
* Sets the total display to the given number of hours, minutes, and seconds.
* @param {int} hours
* @param {int} minutes
* @param {int} seconds
*/
function setTotal(hours, minutes, seconds) {
let hoursString = hours.toString().padStart(2, '0');
let minutesString = minutes.toString().padStart(2, '0');
let secondsString = seconds.toString().padStart(2, '0');
let totalString = `${hoursString}:${minutesString}:${secondsString}`;
let decimalHours = hours + (minutes / 60) + (seconds / 60 / 60);
let decimalHoursString = decimalHours.toFixed(2);
var hourString = decimalHoursString == "1.00" ? 'hour' : 'hours';
$('#totalDisplay').text(totalString);
$('#totalDecimalDisplay').text(`${decimalHoursString} ${hourString}`);
}
/**
* Adds a new input row.
* @param {bool} focus - Whether to focus on the first element of the new row.
*/
function newInputRow(focus = false) {
var numOfChildren = $('#inputForm').children().length;
const rowId = numOfChildren - 1;
$('#inputForm').append(`
<div class="row mb-2">
<div class="col">
<input type="number" class="form-control font-monospace text-center hours-input" id="hours${rowId}" placeholder="00" min=0 oninput="calculateTotal()"/>
</div>
<div class="col">
<input type="number" class="form-control font-monospace text-center minutes-input" id="minutes${rowId}" placeholder="00" min=0 max=60 oninput="calculateTotal()"/>
</div>
<div class="col">
<input type="number" class="form-control font-monospace text-center seconds-input" id="seconds${rowId}" placeholder="00" min=0 max=60 oninput="calculateTotal()"/>
</div>
</div>
`);
// keypress handler for the hours input
$(`#hours${rowId}`).keypress(function (e) {
const key = e.key;
// cancels the input if the input is not a digit
if (!/\d/.test(key)) {
return false;
}
const val = $(this).val() + key;
if ( val.length > 2 ) {
return false;
}
})
// keypress handler for the minutes input
$(`#minutes${rowId}`).keypress(function (e) {
const key = e.key;
// cancels the input if the input is not a digit
if (!/\d/.test(key)) {
return false;
}
const val = $(this).val() + key;
const valNum = parseInt(val);
if (valNum > 60) {
return false;
}
if ( val.length > 2 ) {
return false;
}
})
// keypress handler for the seconds input
$(`#seconds${rowId}`).keypress(function (e) {
const key = e.key;
// cancels the input if the input is not a digit
if (!/\d/.test(key)) {
return false;
}
const val = $(this).val() + key;
const valNum = parseInt(val);
if (valNum > 60) {
return false;
}
if (val.length > 2) {
return false;
}
})
// keydown handler for the seconds input
$(`#seconds${rowId}`).keydown(function (e) {
if (e.key == 'Tab') {
// checks to see if the row is the last row or not
if($(this).parent().parent().is(':last-child')) {
newInputRow();
}
}
});
if (focus) {
$(`#hours${rowId}`).focus();
}
}
/**
* Gets the integer value of a input on the webpage.
* @param {int} column - Specifies whether the input is hours, minutes, or seconds. 0 for hours, 1 for minutes, 2 for seconds.
* @param {int} row - Specifies the row the input is on.
* @returns {int} - The value of the input. 0 if the input is empty.
*/
function getValue(column, row) {
var columnName;
switch (column) {
case 0:
columnName = 'hours';
break;
case 1:
columnName = 'minutes';
break;
case 2:
columnName = 'seconds';
break;
default:
console.log('Not a valid column number.');
return null;
}
const val = $(`#${columnName}${row}`).val();
if (val.length == 0) {
return 0;
}
return parseInt(val);
}
/**
* Gets the sum of the values from a single column.
* @param {int} column - The column. 0 for hours, 1 for minutes, 2 for seconds.
* @returns {int} - The sum of all the columns.
*/
function getColumnSum(column) {
const numOfRows = $('#inputForm').children().length - 1;
var sum = 0;
for (var i = 0; i < numOfRows; i++) {
sum += getValue(column, i);
}
return sum;
}
/**
* Calculates the total number of hours, minutes, and seconds, and sets the total display accordingly.
*/
function calculateTotal() {
const combinedHours = getColumnSum(0);
const combinedMinutes = getColumnSum(1);
const combinedSeconds = getColumnSum(2);
let seconds = combinedSeconds + (combinedMinutes * 60) + (combinedHours * 60 * 60);
var finalHours, finalMinutes, finalSeconds;
finalHours = Math.floor(seconds / 60 / 60);
seconds -= finalHours * 60 * 60;
finalMinutes = Math.floor(seconds / 60);
seconds -= finalMinutes * 60;
finalSeconds = seconds;
setTotal(finalHours, finalMinutes, finalSeconds);
}