-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
476 lines (438 loc) · 16.5 KB
/
script.js
File metadata and controls
476 lines (438 loc) · 16.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const table = document.querySelector(".training-plan__table");
const tableBody = table.querySelector("tbody");
const savedPlansContainer = document.querySelector(".saved-plans__content");
const createPlanButton = document.querySelector(".choice--create button");
const dialog = document.querySelector("dialog");
const dialogContent = document.querySelector(".dialog__content");
const distance = document.querySelector(".choice--distance");
let distanceSelect = distance.querySelector("select");
let distanceValue = distanceSelect.value;
const weeks = document.querySelector(".choice--weeks");
let numberOfWeeksToTrain = parseInt(weeks.querySelector("input").value);
const speedRunSessions = [
"100m Intervals",
"400m Intervals",
"800m Intervals",
"Hill Repeats",
];
const mediumRunSessions = [
"Fartlek",
"Tempo",
"Progression",
"Long Intervals",
"Threshold",
];
const longRunSessions = [
"Long Run",
"Long Run",
"Long Run",
"Long Run (Sprint Finish)",
"Long Run (Sprint Finish)",
"Long Run (Progression)",
"Long Run (Progression)",
"Long Run (Race Pace)",
];
let minimumEasyRunDistance;
let maximumEasyRunDistance;
let minimumMediumRunDistance;
let maximumMediumRunDistance;
let minimumSpeedRunDistance;
let maximumSpeedRunDistance;
let minimumLongRunDistance;
let maximumLongRunDistance;
function handleNumberOfWeeksToTrain() {
numberOfWeeksToTrain = parseInt(weeks.querySelector("input").value);
// We take 1 week off, so we can specify a specific race week plan.
// This race week plan then gets added as the last row in the table.
numberOfWeeksToTrain = numberOfWeeksToTrain - 1;
// We take 2 weeks off for half-marathon tapering.
if (distanceValue === "half-marathon") {
numberOfWeeksToTrain = numberOfWeeksToTrain - 2;
}
// We take 3 weeks off for marathon tapering.
if (distanceValue === "marathon") {
numberOfWeeksToTrain = numberOfWeeksToTrain - 3;
}
}
// Set minimum and maximum distances for each type of run per distance.
function handleDistanceChange() {
distanceValue = distanceSelect.value;
switch (distanceValue) {
case "5k":
minimumEasyRunDistance = 5;
maximumEasyRunDistance = 8;
minimumMediumRunDistance = 4;
maximumMediumRunDistance = 5;
minimumSpeedRunDistance = 3;
maximumSpeedRunDistance = 4;
minimumLongRunDistance = 8;
maximumLongRunDistance = 12;
break;
case "10k":
minimumEasyRunDistance = 6;
maximumEasyRunDistance = 9;
minimumMediumRunDistance = 5;
maximumMediumRunDistance = 8;
minimumSpeedRunDistance = 4;
maximumSpeedRunDistance = 7;
minimumLongRunDistance = 8;
maximumLongRunDistance = 14;
break;
case "half-marathon":
minimumEasyRunDistance = 7;
maximumEasyRunDistance = 12;
minimumMediumRunDistance = 6;
maximumMediumRunDistance = 10;
minimumSpeedRunDistance = 5;
maximumSpeedRunDistance = 10;
minimumLongRunDistance = 10;
maximumLongRunDistance = 18;
break;
case "marathon":
minimumEasyRunDistance = 8;
maximumEasyRunDistance = 16;
minimumMediumRunDistance = 7;
maximumMediumRunDistance = 13;
minimumSpeedRunDistance = 7;
maximumSpeedRunDistance = 10;
minimumLongRunDistance = 12;
maximumLongRunDistance = 35;
break;
}
return (
minimumLongRunDistance,
maximumLongRunDistance,
minimumMediumRunDistance,
maximumMediumRunDistance,
minimumSpeedRunDistance,
maximumSpeedRunDistance,
minimumEasyRunDistance,
maximumEasyRunDistance
);
}
// Get a random session from each session type.
function getRandomSession(array) {
return array[Math.floor(Math.random() * array.length)];
}
// Each time the user changes their choice, we need to update the plan.
function clearPlan() {
tableBody.innerHTML = "";
}
// Given 2 numbers, return an increasing number between them for each week.
// i parameter is the index from the loop that this function is called in.
function getIncreasingNumberBetween(min, max, i, numberOfWeeksToTrain) {
const difference = max - min;
const increment = difference / numberOfWeeksToTrain;
return Math.round(min + increment * i);
}
// Create a row for each taper week.
function handleTaperWeeks(week) {
const mediumRunSession = getRandomSession(mediumRunSessions);
const mediumRunSessionLink = mediumRunSession
.replace(/\s+/g, "-")
.toLowerCase();
const speedRunSession = getRandomSession(speedRunSessions);
const speedRunSessionLink = speedRunSession
.replace(/\s+/g, "-")
.toLowerCase();
const longRunSession = getRandomSession(longRunSessions);
const longRunSessionLink = longRunSession
.replace(/\s+/g, "-")
.replace(/\(/g, "")
.replace(/\)/g, "")
.toLowerCase();
const numberOfTableRows = tableBody.querySelectorAll("tr").length;
const weekNumber = parseInt(numberOfTableRows + 1);
const row = document.createElement("tr");
row.classList.add("training-plan__row");
row.innerHTML = `
<td headers="traning-plan-week" class="training-plan__cell training-plan__cell--week-number">${weekNumber}</td>
<td headers="traning-plan-day-1" class="training-plan__cell training-plan__cell--day-1">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="traning-plan-day-2" class="training-plan__cell training-plan__cell--day-2">
<a href="#run-type-${mediumRunSessionLink}">${mediumRunSession}</a> - ${week.mediumRunDistance}k</td>
<td headers="traning-plan-day-3" class="training-plan__cell training-plan__cell--day-3"><a href="#run-type-easy">Easy</a> - ${week.easyRunDistance}k</td>
<td headers="traning-plan-day-4" class="training-plan__cell training-plan__cell--day-4">
<a href="#run-type-${speedRunSessionLink}">${speedRunSession}</a> - ${week.speedRunDistance}k</td>
<td headers="traning-plan-day-5" class="training-plan__cell training-plan__cell--day-5">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="traning-plan-day-6" class="training-plan__cell training-plan__cell--day-6"><a href="#run-type-easy">Easy</a> - ${week.easyRunDistance}k</td>
<td headers="traning-plan-day-7" class="training-plan__cell training-plan__cell--day-7">
<a href="#run-type-${longRunSessionLink}">${longRunSession}</a> - ${week.longRunDistance}k</td>
`;
tableBody.appendChild(row);
}
// This function is used for when you click on a link in the training plan.
// It opens the dialog and loads the content from the corresponding link.
function runningPlanDialog() {
const runTypeLinks = document.querySelectorAll(".training-plan__cell a");
const runTypeLinksArray = Array.from(runTypeLinks);
runTypeLinksArray.forEach((link) => {
const linkHref = link.getAttribute("href");
const correspondingDialog = document.querySelector(linkHref);
link.addEventListener("click", (e) => {
e.preventDefault();
dialogContent.innerHTML = correspondingDialog.innerHTML;
dialog.showModal();
});
});
}
// General function to handle closing the dialog.
function closeDialog() {
const dialog = document.querySelector("dialog");
const closeButton = document.querySelector(".dialog__close");
closeButton.addEventListener("click", () => {
dialog.close();
});
dialog.addEventListener("click", (e) => {
if (e.target === dialog) {
dialog.close();
}
});
}
// This function handles the options for save/copy/print.
function handleHeaderOptions() {
const saveButton = document.querySelector(".action-item--save");
const copyButton = document.querySelector(".action-item--copy");
const printButton = document.querySelector(".action-item--print");
if (saveButton) {
saveButton.addEventListener("click", () => {
const trainingPlan = document.querySelector(".training-plan");
const trainingPlanText = trainingPlan.innerHTML;
const distanceSelect = document.querySelector("#distance");
const distanceValue = distanceSelect.value;
const trainingPlans =
JSON.parse(localStorage.getItem("trainingPlans")) || [];
const existingIndex = trainingPlans.findIndex(
(plan) => plan.distance === distanceValue
);
if (existingIndex !== -1) {
trainingPlans[existingIndex].plan = trainingPlanText;
} else {
trainingPlans.push({
distance: distanceValue,
plan: trainingPlanText,
});
}
localStorage.setItem("trainingPlans", JSON.stringify(trainingPlans));
handleSavedPlans();
});
}
if (copyButton) {
const copyButtons = document.querySelectorAll(".action-item--copy");
copyButtons.forEach((copyButton) => {
copyButton.addEventListener("click", () => {
const trainingPlan = document.querySelector(".training-plan");
let trainingPlanText = trainingPlan.innerHTML;
const tempDiv = document.createElement("div");
tempDiv.innerHTML = trainingPlanText;
const links = tempDiv.querySelectorAll("a");
links.forEach((link) => {
const textNode = document.createTextNode(link.textContent);
link.replaceWith(textNode);
});
trainingPlanText = tempDiv.innerHTML;
navigator.clipboard.writeText(trainingPlanText);
dialogContent.innerHTML = `
<p>Your training plan has been copied to your clipboard.</p>
<p>You can now paste it into a document or spreadsheet such as Microsoft Word or Excel.</p>
`;
dialog.showModal();
});
});
}
if (printButton) {
const printButtons = document.querySelectorAll(".action-item--print");
printButtons.forEach((printButton) => {
printButton.addEventListener("click", () => {
window.print();
});
});
}
}
function handleSavedPlans() {
const savedPlans = JSON.parse(localStorage.getItem("trainingPlans")) || [];
if (savedPlans.length > 0) {
savedPlansContainer.innerHTML = `
<p>Click on any of the plans below to load it into the running plan table.</p>
<ul class="saved-plans__list">
${savedPlans
.map(
(plan) => `
<li class="saved-plans__item">
<button aria-label="Load the ${
plan.distance
} plan in to the running plan table." class="action-item action-item--ghost saved-plans__action">${
plan.distance.charAt(0).toUpperCase() + plan.distance.slice(1)
}</button>
</li>
`
)
.join("")}
</ul>
`;
const savedPlansActions = document.querySelectorAll(
".saved-plans__item button"
);
// If you click on any of the saved plans, replace the table with that plan.
savedPlansActions.forEach((action, index) => {
action.addEventListener("click", () => {
const savedPlan = savedPlans[index].plan;
table.innerHTML = savedPlan;
});
});
} else {
savedPlansContainer.innerHTML = `
<p>You have no saved plans.</p>
`;
}
}
function handleCreatePlan() {
clearPlan();
handleDistanceChange();
handleHeaderOptions();
runningPlanDialog();
closeDialog();
handleSavedPlans();
distanceValue = distanceSelect.value;
for (let i = 1; i <= numberOfWeeksToTrain; i++) {
const mediumRunSession = getRandomSession(mediumRunSessions);
const mediumRunSessionLink = mediumRunSession
.replace(/\s+/g, "-")
.toLowerCase();
const speedRunSession = getRandomSession(speedRunSessions);
const speedRunSessionLink = speedRunSession
.replace(/\s+/g, "-")
.toLowerCase();
const longRunSession = getRandomSession(longRunSessions);
const longRunSessionLink = longRunSession
.replace(/\s+/g, "-")
.replace(/\(/g, "")
.replace(/\)/g, "")
.toLowerCase();
const row = document.createElement("tr");
row.classList.add("training-plan__row");
row.innerHTML = `
<td headers="training-plan-week" class="training-plan__cell training-plan__cell--week-number">${i}</td>
<td headers="training-plan-day-1" class="training-plan__cell training-plan__cell--day-1">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="training-plan-day-2" class="training-plan__cell training-plan__cell--day-2">
<a href="#run-type-${mediumRunSessionLink}">${mediumRunSession}</a> - ${getIncreasingNumberBetween(
minimumMediumRunDistance,
maximumMediumRunDistance,
i,
numberOfWeeksToTrain
)}k
</td>
<td headers="training-plan-day-3" class="training-plan__cell training-plan__cell--day-3">
<a href="#run-type-easy">Easy</a> - ${getIncreasingNumberBetween(
minimumEasyRunDistance,
maximumEasyRunDistance,
i,
numberOfWeeksToTrain
)}k
</td>
<td headers="training-plan-day-4" class="training-plan__cell training-plan__cell--day-4">
<a href="#run-type-${speedRunSessionLink}">${speedRunSession}</a> - ${getIncreasingNumberBetween(
minimumSpeedRunDistance,
maximumSpeedRunDistance,
i,
numberOfWeeksToTrain
)}k
</td>
<td headers="training-plan-day-5" class="training-plan__cell training-plan__cell--day-5">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="training-plan-day-6" class="training-plan__cell training-plan__cell--day-6">
<a href="#run-type-easy">Easy</a> - ${getIncreasingNumberBetween(
minimumEasyRunDistance,
maximumEasyRunDistance,
i,
numberOfWeeksToTrain
)}k
</td>
<td headers="training-plan-day-7" class="training-plan__cell training-plan__cell--day-7">
<a href="#run-type-${longRunSessionLink}">${longRunSession}</a> - ${getIncreasingNumberBetween(
minimumLongRunDistance,
maximumLongRunDistance,
i,
numberOfWeeksToTrain
)}k
</td>
`;
handleNumberOfWeeksToTrain();
tableBody.appendChild(row);
}
if (distanceValue === "half-marathon") {
// Create 2 more rows for the 2 half-marathon taper weeks
const taperWeeks = [
{
week: 1,
longRunDistance: 15,
mediumRunDistance: 7,
speedRunDistance: 7,
easyRunDistance: 10,
},
{
week: 2,
longRunDistance: 12,
mediumRunDistance: 6,
speedRunDistance: 5,
easyRunDistance: 10,
},
];
taperWeeks.forEach((week) => {
handleTaperWeeks(week);
});
}
if (distanceValue === "marathon") {
// Create 3 more rows for the 3 marathon taper weeks
const taperWeeks = [
{
week: 1,
longRunDistance: 26,
mediumRunDistance: 10,
speedRunDistance: 8,
easyRunDistance: 12,
},
{
week: 2,
longRunDistance: 20,
mediumRunDistance: 8,
speedRunDistance: 6,
easyRunDistance: 10,
},
{
week: 3,
longRunDistance: 16,
mediumRunDistance: 7,
speedRunDistance: 4,
easyRunDistance: 10,
},
];
taperWeeks.forEach((week) => {
handleTaperWeeks(week);
});
}
// Add one final row to the table
// This is the race week workouts, and is the same for all distances.
const lastRow = document.createElement("tr");
lastRow.classList.add("training-plan__row");
const numberOfTableRows = tableBody.querySelectorAll("tr").length;
lastRow.innerHTML = `
<td headers="training-plan-week" class="training-plan__cell training-plan__cell--week-number">${
numberOfTableRows + 1
}</td>
<td headers="training-plan-day-1" class="training-plan__cell training-plan__cell--day-1">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="training-plan-day-2" class="training-plan__cell training-plan__cell--day-2"><a href="#run-type-easy">Easy</a> - 5k</td>
<td headers="training-plan-day-3" class="training-plan__cell training-plan__cell--day-3">
<a href="#run-type-rest-crosstrain">Rest or <br>Crosstrain</a></td>
<td headers="training-plan-day-4" class="training-plan__cell training-plan__cell--day-4">Rest</td>
<td headers="training-plan-day-5" class="training-plan__cell training-plan__cell--day-5"><a href="#run-type-easy">Easy</a> - 3k</td>
<td headers="training-plan-day-6" class="training-plan__cell training-plan__cell--day-6">Rest</td>
<td headers="training-plan-day-7" class="training-plan__cell training-plan__cell--day-7">Race Day!</td>
`;
tableBody.appendChild(lastRow);
}
handleCreatePlan();
createPlanButton.addEventListener("click", handleCreatePlan);