- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
426 lines (379 loc) · 12.8 KB
/
Copy pathscript.js
File metadata and controls
426 lines (379 loc) · 12.8 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
// Data storage with mock data
letruns=JSON.parse(localStorage.getItem("runs")||"[]");
// Add mock data if no runs exist
// if (runs.length === 0) {
// const mockRuns = generateMockData();
// runs = mockRuns;
// localStorage.setItem("runs", JSON.stringify(runs));
// }
// Generate 4 weeks of mock running data
functiongenerateMockData(){
constmockData=[];
construnTypes=["easy","tempo","long"];
constsurfaces=["road","trail"];
consttoday=newDate();
// Generate runs for the past 28 days
for(letdaysAgo=27;daysAgo>=0;daysAgo--){
// Skip some days randomly (rest days)
if(Math.random()>0.75)continue;
construnDate=newDate(today);
runDate.setDate(today.getDate()-daysAgo);
runDate.setHours(Math.floor(Math.random()*12)+6);// Morning runs between 6-18
// Determine run type based on day pattern
letrunType;
constdayOfWeek=runDate.getDay();
if(dayOfWeek===0){
// Sunday - long run
runType="long";
}elseif(dayOfWeek===2||dayOfWeek===4){
// Tuesday/Thursday - tempo
runType=Math.random()>0.5 ? "tempo" : "easy";
}else{
runType="easy";
}
// Set distance based on run type
letdistance;
if(runType==="long"){
distance=10+Math.random()*6;// 10-16 miles
}elseif(runType==="tempo"){
distance=5+Math.random()*3;// 5-8 miles
}else{
distance=3+Math.random()*4;// 3-7 miles
}
// Set pace based on run type
letpaceMinutes,paceSeconds;
if(runType==="tempo"){
paceMinutes=7+Math.floor(Math.random()*1);
paceSeconds=Math.floor(Math.random()*60);
}elseif(runType==="long"){
paceMinutes=8+Math.floor(Math.random()*2);
paceSeconds=Math.floor(Math.random()*60);
}else{
paceMinutes=8+Math.floor(Math.random()*2);
paceSeconds=Math.floor(Math.random()*60);
}
constpace=`${paceMinutes}:${paceSeconds.toString().padStart(2,"0")}`;
// Mix surfaces with preference for road
constsurface=Math.random()>0.3 ? "road" : "trail";
mockData.push({
id: Date.now()+Math.random()*1000,
date: runDate.toISOString(),
distance: parseFloat(distance.toFixed(1)),
pace: pace,
runType: runType,
surface: surface,
});
}
returnmockData.sort((a,b)=>newDate(a.date)-newDate(b.date));
}
// Chart instances
letweeklyChart=null;
letsurfaceChart=null;
letrunTypeChart=null;
// Initialize app
document.addEventListener("DOMContentLoaded",function(){
updateStats();
updateRunsTable();
initCharts();
updateCharts();
});
// Add run form handler
document.getElementById("runForm").addEventListener("submit",function(e){
e.preventDefault();
construn={
id: Date.now(),
date: newDate().toISOString(),
distance: parseFloat(document.getElementById("distance").value),
pace: document.getElementById("pace").value,
runType: document.getElementById("runType").value,
surface: document.getElementById("surface").value,
};
runs.push(run);
localStorage.setItem("runs",JSON.stringify(runs));
// Reset form
this.reset();
// Update UI
updateStats();
updateRunsTable();
updateCharts();
// Show success animation
showNotification("Run added successfully!");
});
// Update statistics
functionupdateStats(){
constnow=newDate();
constweekStart=newDate(now);
weekStart.setDate(now.getDate()-now.getDay());
weekStart.setHours(0,0,0,0);
letweekTotal=0;
letweekRoad=0;
letweekTrail=0;
letroadTotal=0;
lettrailTotal=0;
runs.forEach((run)=>{
construnDate=newDate(run.date);
// This week's totals
if(runDate>=weekStart){
weekTotal+=run.distance;
if(run.surface==="road"){
weekRoad+=run.distance;
}else{
weekTrail+=run.distance;
}
}
// All-time totals
if(run.surface==="road"){
roadTotal+=run.distance;
}else{
trailTotal+=run.distance;
}
});
document.getElementById("weekTotal").textContent=weekTotal.toFixed(1);
document.getElementById("weekRoad").textContent=weekRoad.toFixed(1);
document.getElementById("weekTrail").textContent=weekTrail.toFixed(1);
document.getElementById("roadTotal").textContent=roadTotal.toFixed(1);
document.getElementById("trailTotal").textContent=trailTotal.toFixed(1);
document.getElementById("combinedTotal").textContent=(roadTotal+trailTotal).toFixed(1);
}
// Update runs table
functionupdateRunsTable(){
consttbody=document.getElementById("runsTable");
tbody.innerHTML="";
// Sort runs by date (newest first)
constsortedRuns=[...runs].sort((a,b)=>newDate(b.date)-newDate(a.date));
// Show only last 10 runs
sortedRuns.slice(0,10).forEach((run)=>{
constrow=tbody.insertRow();
construnDate=newDate(run.date);
row.innerHTML=`
<td class="py-2">${runDate.toLocaleDateString()}</td>
<td class="py-2">${run.distance} mi</td>
<td class="py-2">${run.pace}</td>
<td class="py-2">
<span class="px-2 py-1 rounded text-xs ${
run.runType==="easy"
? "bg-green-500/20 text-green-400"
: run.runType==="tempo"
? "bg-yellow-500/20 text-yellow-400"
: "bg-red-500/20 text-red-400"
}">${run.runType}</span>
</td>
<td class="py-2">
<span class="px-2 py-1 rounded text-xs ${
run.surface==="road"
? "bg-blue-500/20 text-blue-400"
: "bg-green-500/20 text-green-400"
}">${run.surface}</span>
</td>
<td class="py-2">
<button onclick="deleteRun(${run.id})" class="text-red-400 hover:text-red-300">
Delete
</button>
</td>
`;
});
}
// Delete run
functiondeleteRun(id){
if(confirm("Are you sure you want to delete this run?")){
runs=runs.filter((run)=>run.id!==id);
localStorage.setItem("runs",JSON.stringify(runs));
updateStats();
updateRunsTable();
updateCharts();
showNotification("Run deleted");
}
}
// Initialize charts
functioninitCharts(){
// Weekly mileage chart
constweeklyCtx=document.getElementById("weeklyChart").getContext("2d");
weeklyChart=newChart(weeklyCtx,{
type: "line",
data: {
labels: [],
datasets: [
{
label: "Weekly Mileage",
data: [],
borderColor: "rgb(147, 51, 234)",
backgroundColor: "rgba(147, 51, 234, 0.1)",
tension: 0.4,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false,
},
},
scales: {
y: {
beginAtZero: true,
grid: {
color: "rgba(255, 255, 255, 0.1)",
},
ticks: {
color: "rgba(255, 255, 255, 0.7)",
},
},
x: {
grid: {
color: "rgba(255, 255, 255, 0.1)",
},
ticks: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
// Surface distribution chart
constsurfaceCtx=document.getElementById("surfaceChart").getContext("2d");
surfaceChart=newChart(surfaceCtx,{
type: "doughnut",
data: {
labels: ["Road","Trail"],
datasets: [
{
data: [0,0],
backgroundColor: [
"rgba(59, 130, 246, 0.8)",
"rgba(34, 197, 94, 0.8)",
],
borderColor: [
"rgb(59, 130, 246)",
"rgb(34, 197, 94)",
],
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "bottom",
labels: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
// Run type distribution chart
construnTypeCtx=document.getElementById("runTypeChart").getContext("2d");
runTypeChart=newChart(runTypeCtx,{
type: "doughnut",
data: {
labels: ["Easy","Tempo","Long"],
datasets: [
{
data: [0,0,0],
backgroundColor: [
"rgba(34, 197, 94, 0.8)",
"rgba(234, 179, 8, 0.8)",
"rgba(239, 68, 68, 0.8)",
],
borderColor: [
"rgb(34, 197, 94)",
"rgb(234, 179, 8)",
"rgb(239, 68, 68)",
],
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "bottom",
labels: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
}
// Update charts
functionupdateCharts(){
// Calculate weekly totals for last 8 weeks
constweeklyData=calculateWeeklyTotals();
weeklyChart.data.labels=weeklyData.labels;
weeklyChart.data.datasets[0].data=weeklyData.data;
weeklyChart.update();
// Calculate surface distribution
letroadMiles=0;
lettrailMiles=0;
runs.forEach((run)=>{
if(run.surface==="road"){
roadMiles+=run.distance;
}else{
trailMiles+=run.distance;
}
});
surfaceChart.data.datasets[0].data=[roadMiles,trailMiles];
surfaceChart.update();
// Calculate run type distribution
leteasyMiles=0;
lettempoMiles=0;
letlongMiles=0;
runs.forEach((run)=>{
if(run.runType==="easy"){
easyMiles+=run.distance;
}elseif(run.runType==="tempo"){
tempoMiles+=run.distance;
}else{
longMiles+=run.distance;
}
});
runTypeChart.data.datasets[0].data=[easyMiles,tempoMiles,longMiles];
runTypeChart.update();
}
// Calculate weekly totals
functioncalculateWeeklyTotals(){
constweeks=8;
constlabels=[];
constdata=[];
constnow=newDate();
for(leti=weeks-1;i>=0;i--){
constweekEnd=newDate(now);
weekEnd.setDate(now.getDate()-i*7);
constweekStart=newDate(weekEnd);
weekStart.setDate(weekEnd.getDate()-7);
letweekTotal=0;
runs.forEach((run)=>{
construnDate=newDate(run.date);
if(runDate>=weekStart&&runDate<=weekEnd){
weekTotal+=run.distance;
}
});
labels.push(`Week ${weeks-i}`);
data.push(weekTotal);
}
return{ labels, data };
}
// Show notification
functionshowNotification(message){
constnotification=document.createElement("div");
notification.className=
"fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg transform translate-x-full transition-transform duration-300";
notification.textContent=message;
document.body.appendChild(notification);
setTimeout(()=>{
notification.style.transform="translateX(0)";
},100);
setTimeout(()=>{
notification.style.transform="translateX(100%)";
setTimeout(()=>{
document.body.removeChild(notification);
},300);
},3000);
}