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
|
class Timer {
constructor(elem) {
// DOM stuff
this.elem = elem;
this.row = elem.parentElement.parentElement.parentElement;
this.icon = elem.children[0].children[0];
this.time_span = elem.parentElement.children[1];
this.length = this.row.dataset.timerSeconds;
this.secondsLeft = this.length;
this.interval = null;
this.paused = true;
this.finished = false;
}
static fromElem(elem) {
if (!elem.timer) {
elem.timer = new Timer(elem);
}
return elem.timer;
}
setIcon(c) {
this.icon.classList.remove(...this.icon.classList);
this.icon.classList.add('fa');
this.icon.classList.add(c);
}
drawTime() {
const minutes = String(parseInt(this.secondsLeft / 60)).padStart(2, '0');
const seconds = String(parseInt(this.secondsLeft % 60)).padStart(2, '0');
this.time_span.innerHTML = `${minutes}:${seconds}`;
}
// Button is pressed for timer
press() {
if (this.finished) {
this.reset();
} else if (this.paused) {
this.play();
} else {
this.pause();
}
}
// Start the timer
play() {
console.log("Playing...");
this.paused = false;
// Run this code every 1 second
this.interval = setInterval(() => {
// Update time
this.secondsLeft--;
this.drawTime();
// Check for finish
if (this.secondsLeft <= 0) {
this.finish();
}
}, 1000);
// Update button
this.setIcon('fa-pause');
}
// Pause timer
pause() {
console.log("Pausing...");
this.paused = true;
// Stop interval
clearInterval(this.interval);
// Update button
this.setIcon('fa-play');
}
// Finish timer
finish() {
if (this.finished) {
return;
}
console.log("Finished...");
this.finished = true;
this.pause();
this.setIcon('fa-refresh');
this.elem.disabled = true;
this.elem.classList.add('is-success')
setCheck(this.row);
}
reset() {
this.paused = true;
this.finished = false;
this.secondsLeft = this.length;
this.drawTime();
this.setIcon('fa-play');
}
}
class Set {
constructor(row) {
this.row = row;
if (row.dataset.isTimer) {
this.timer = Timer.fromElem(row.children[1].children[0].children[0]);
} else {
this.lbsInput = row.children[1].children[0];
this.repsInput = row.children[2].children[0];
}
}
static fromRow(row) {
if (!row.set) {
row.set = new Set(row);
}
return row.set;
}
// TODO
reset() {
}
check() {
if (self.timer) {
self.timer.finish();
} else {
this.lbsInput.disabled = true;
this.repsInput.disabled = true;
}
// TODO finish
}
}
// Get an arrayt of all inputs
allInputs = Array.from(document.getElementsByClassName('input')).filter(e => e.type == 'number');
// Register onsubmit handler for form (NOTE: unfortunatly not a better way to do this)
document.getElementById("form").addEventListener('submit', (el) => {
Array.from(event.target.getElementsByTagName('input')).forEach(i => i.disabled = false);
});
function setReset(row, index, values=true) {
if (row.dataset.isTimer) {
let timerButton = row.children[1].children[0].children[0];
timerButton.disabled = false;
timerButton.classList.remove('is-success');
timerButton.classList.remove('is-danger');
row.children[2].children[0].classList.remove('is-success');
} else if (row.dataset.isReps) {
let lbsInput = row.children[1].children[0];
let repsInput = row.children[2].children[0];
// Enable inputs
lbsInput.disabled = false;
repsInput.disabled = false;
// Remove classes
lbsInput.classList.remove('is-success');
lbsInput.classList.remove('is-danger');
repsInput.classList.remove('is-success');
repsInput.classList.remove('is-danger');
// update id/classes to new row index
current = lbsInput.id;
updated = current.substring(0, current.length - 5);
lbs = updated + index + '-lbs';
lbsInput.id = lbs;
lbsInput.name = lbs;
reps = updated + index + '-reps'
repsInput.id = reps;
repsInput.name = reps;
row.children[3].children[0].classList.remove('is-success');
if (values) {
lbsInput.value = '';
repsInput.value = '';
}
}
}
function setSetid(row, id) {
let setNumber = row.children[0];
//let lbsInput = row.children[1].children[0];
//let repsInput = row.children[2].children[0];
//let doneButton = row.children[3].children[0];
setNumber.textContent = id + 1;
}
function setCheck(row) {
// TODO: better way to check
if (row.dataset.isTimer) {
const timerButton = row.children[1].children[0].children[0];
Timer.fromElem(timerButton).finish();
} else {
// Disable input editing
row.children[1].firstChild.disabled = true;
row.children[2].firstChild.disabled = true;
// Add is-success to inputs
row.children[1].firstChild.classList.add('is-success')
row.children[2].firstChild.classList.add('is-success')
}
doneButton = row.children[2].children[0];
doneButton.classList.add('is-success');
}
// ----- ----- CALLBACKS ----- ----- //
function onClickPausePlayTimer(elem) {
// Create new timer object if one does not exist
if (!elem.timer) {
elem.timer = new Timer(elem);
}
// Toggle timer
elem.timer.press();
}
function onKeyDown(event) {
if (event.key === "Enter" || event.key === "Tab") {
event.preventDefault();
//Isolate the node that we're after
const currentNode = event.target;
//Find the current tab index.
currentIndex = [...allInputs].findIndex(el => currentNode.isEqualNode(el));
//focus the following element
const targetIndex = (currentIndex + 1) % allInputs.length;
const targetNode = allInputs[targetIndex];
if (!targetNode.parentElement.parentElement.isEqualNode(currentNode.parentElement.parentElement)) {
// going to new row
if (currentNode.value != "" && allInputs[currentIndex - 1].value && allInputs[currentIndex - 1] != "") {
onClickSetCheck(currentNode.parentElement.parentElement.children[3].children[0]);
}
}
targetNode.focus();
}
}
function onClickAddSet(elem) {
// Create the new row
const tableBody = elem.parentElement.parentElement.children[0].children[1];
const row = tableBody.children[0].cloneNode(true);
// Add new row to table
setReset(row, tableBody.children.length, true);
setSetid(row, tableBody.children.length)
tableBody.appendChild(row)
}
function onClickSetCheck(elem) {
const row = elem.parentElement.parentElement;
if (elem.classList.contains('is-success')) {
setReset(row, row.parentElement.children.length - 1, false);
} else {
elem.classList.add('is-success');
setCheck(elem.parentElement.parentElement);
}
}
|