-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastingline.html
780 lines (670 loc) · 29.2 KB
/
fastingline.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fasting Time Tracker</title>
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<style>
:root {
--primary-color: #2196f3;
--success-color: #4caf50;
--background-color: #f5f5f5;
--text-color: #333;
--timeline-height: 200px;
--block-height: 40%;
--indicator-width: 2px;
--indicator-color: red;
--tick-color: #ddd;
--tick-width: 1px;
--tick-height: 10px;
--minute-tick-height: 5px;
--minute-tick-color: #eee;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
}
.controls {
margin-bottom: 2rem;
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: center;
}
.speed-control {
display: none;
align-items: center;
gap: 0.5rem;
margin-left: 1rem;
padding: 0.5rem;
background: rgba(0,0,0,0.05);
border-radius: 4px;
}
button {
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
background-color: var(--primary-color);
color: white;
cursor: pointer;
font-size: 1rem;
transition: opacity 0.2s;
}
button:hover {
opacity: 0.9;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.timeline-container {
width: 100%;
height: var(--timeline-height);
position: relative;
overflow: hidden;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 50px;
}
.timeline {
position: absolute;
height: 100%;
display: flex;
align-items: center;
will-change: transform;
transition: transform 1s linear;
left: 50%;
}
.timeline-block {
position: absolute;
min-width: 100px;
border-radius: 0px; /* Not rounded. Initially was 4px */
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
transition: transform 0.2s;
padding: 0 1rem;
text-align: center;
white-space: nowrap;
opacity: 0.8;
}
.timeline-block:hover {
transform: scale(1.05);
opacity: 1;
z-index: 3;
}
.current-time-indicator {
position: absolute;
left: 50%;
height: 100%;
width: var(--indicator-width);
background-color: var(--indicator-color);
z-index: 10;
}
.hour-tick {
position: absolute;
width: var(--tick-width);
height: var(--tick-height);
background-color: var(--tick-color);
bottom: 0;
transform: translateX(-50%);
}
.minute-tick {
position: absolute;
width: var(--tick-width);
height: var(--minute-tick-height);
background-color: var(--minute-tick-color);
bottom: 0;
transform: translateX(-50%);
}
.hour-label {
position: absolute;
bottom: calc(var(--tick-height) + 2px);
transform: translateX(-50%);
font-size: 0.7rem;
color: #666;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
justify-content: center;
align-items: center;
z-index: 100;
}
.modal-content {
background-color: white;
padding: 2rem;
border-radius: 8px;
max-width: 500px;
width: 90%;
position: relative;
}
.close-modal {
position: absolute;
top: 1rem;
right: 1rem;
background: none;
border: none;
color: var(--text-color);
font-size: 1.5rem;
cursor: pointer;
}
.elapsed-time {
font-size: 2rem;
margin-bottom: 2rem;
text-align: center;
font-weight: bold;
}
.modal-times {
margin-top: 1rem;
line-height: 1.6;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 2rem;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.no-fasts {
text-align: center;
margin-top: 1rem;
font-style: italic;
}
.delete-button {
background: none;
border: none;
color: red;
cursor: pointer;
}
.confirmation-modal {
display: none; /* Hidden by default */
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.details-modal {
display: none; /* Hidden by default */
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="elapsed-time" id="elapsedTime">00:00:00</div>
<div class="controls">
<button id="startEditButton">Start Fasting</button>
<button id="stopButton" disabled>Stop Fasting</button>
<div class="speed-control">
<label>Speed:</label>
<button id="normalSpeed" class="speed-btn">1x</button>
<button id="fastSpeed" class="speed-btn">100x</button>
</div>
</div>
<div class="timeline-container">
<div class="current-time-indicator"></div>
<div class="timeline" id="timeline"></div>
</div>
<h2>Finished Fasts</h2>
<table id="fastsTable">
<thead>
<tr><th>Start Time</th><th>End Time</th><th>Duration</th><th>Actions</th></tr>
</thead>
<tbody></tbody>
</table>
<div id="noFastsMessage" class="no-fasts">No finished fasts yet.</div>
<div class="modal" id="modal">
<div class="modal-content">
<button class="close-modal" id="closeModal">
<span class="mdi mdi-close"></span>
</button>
<h2 id="modalTitle"></h2>
<p class="elapsed-time" id="modalElapsedTime"></p>
</div>
</div>
<div class="modal details-modal" id="detailsModal">
<div class="modal-content">
<button class="close-modal" id="closeDetailsModal">
<span class="mdi mdi-close"></span>
</button>
<h2 id="detailsModalTitle"></h2>
<p id="detailsModalDescription"></p>
</div>
</div>
<div class="modal" id="editStartTimeModal">
<div class="modal-content">
<button class="close-modal" id="closeEditStartTimeModal">
<span class="mdi mdi-close"></span>
</button>
<h2>Edit Start Time</h2>
<input type="datetime-local" id="newStartTimeInput">
<button id="confirmStartTimeEdit">Confirm</button>
</div>
</div>
<div class="confirmation-modal" id="confirmationModal">
<p>Are you sure you want to delete this entry?</p>
<button id="confirmDelete">Yes</button>
<button id="cancelDelete">No</button>
</div>
<script>
// ... (your existing JavaScript code) ...
let fastToDelete = null; // Store the fast entry to be deleted
// ... (your existing JavaScript code) ...
const CONFIG = {
STORAGE_KEY: 'fastingData',
PIXEL_PER_HOUR: 100,
PIXEL_PER_MINUTE: 100 / 60,
ANIMATION_INTERVAL: 1000,
TIMELINE_RANGE: 48,
SAMPLE_BLOCKS: [
{
title: "Absorptive Phase",
description: "As food is digested, blood glucose levels rise, stimulating insulin production. Energy is primarily derived from glucose, and excess is stored as glycogen in muscles and the liver.",
color: "#2196f3",
offsetHours: 0,
durationHours: 4,
height: "40%",
top: "30%"
},
{
title: "Early Fasting Phase",
description: "Blood glucose levels begin to drop as the body switches to glycogen stores for energy. Insulin levels fall, and the hormone glucagon rises to maintain glucose levels.",
color: "#4caf50",
offsetHours: 4,
durationHours: 4,
height: "40%",
top: "30%"
},
{
title: "Glycogen Depletion",
description: "Glycogen stores start to deplete, and the body begins transitioning to fat as its energy source, releasing fatty acids into the bloodstream.",
color: "#ff9800",
offsetHours: 8,
durationHours: 4,
height: "40%",
top: "30%"
},
{
title: "Ketone Production",
description: "As glycogen stores decrease, the liver begins producing ketone bodies from fatty acids. Ketones serve as an alternative fuel, especially for the brain, which prefers glucose but can adapt to ketones in times of need.",
color: "#f44336",
offsetHours: 12,
durationHours: 4,
height: "40%",
top: "30%"
},
{
title: "Autophagy Activation",
description: "With energy supplies low, the body enters autophagy, a cellular process that breaks down old and dysfunctional cells for parts to recycle or eliminate.",
color: "#9c27b0",
offsetHours: 16,
durationHours: 2,
height: "40%",
top: "30%"
},
{
title: "Increased Fat Breakdown",
description: "The body relies heavily on fat for energy, and levels of human growth hormone (HGH) increase dramatically, potentially by up to fivefold.",
color: "#673ab7",
offsetHours: 18,
durationHours: 6,
height: "40%",
top: "30%"
},
{
title: "Deepened Ketosis",
description: "The liver continues to produce ketones at a higher rate to fuel the brain, while autophagy accelerates, breaking down misfolded proteins and damaged cellular components.",
color: "#3f51b5",
offsetHours: 24,
durationHours: 6,
height: "40%",
top: "30%"
},
{
title: "Enhanced Insulin Sensitivity",
description: "With glycogen long depleted, the body is fully adapted to fat burning. Cellular repair reaches a peak, as does the cleansing effect of autophagy. Insulin sensitivity is enhanced.",
color: "#00bcd4",
offsetHours: 30,
durationHours: 6,
height: "40%",
top: "30%"
}
]
};
class StorageService {
static getData() {
const stored = localStorage.getItem(CONFIG.STORAGE_KEY);
return stored ? JSON.parse(stored) : { fasts: [] };
}
static saveData(data) {
localStorage.setItem(CONFIG.STORAGE_KEY, JSON.stringify(data));
}
}
class TimeUtils {
static formatDuration(ms) {
const seconds = Math.floor(Math.max(0, ms) / 1000);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
return [hours, minutes, remainingSeconds]
.map(num => String(num).padStart(2, '0'))
.join(':');
}
static calculatePosition(time, startTime) {
const hoursDiff = (time - startTime) / (1000 * 60 * 60);
return hoursDiff * CONFIG.PIXEL_PER_HOUR;
}
static formatHourLabel(hour) {
return `${hour}h`;
}
}
class TimelineBlock {
static create(block, startTime, onClick) {
const blockEl = document.createElement('div');
blockEl.className = 'timeline-block';
blockEl.style.backgroundColor = block.color;
const start = new Date(block.startTime || startTime.getTime() + block.offsetHours * 3600000);
const end = new Date(block.endTime || start.getTime() + (block.durationHours || 0) * 3600000);
const startPos = TimeUtils.calculatePosition(start, startTime);
const width = Math.abs(TimeUtils.calculatePosition(end, startTime) - startPos);
blockEl.style.left = `${startPos}px`;
blockEl.style.width = `${width}px`;
blockEl.style.height = block.height || '40%';
blockEl.style.top = block.top || '30%';
blockEl.textContent = block.title;
blockEl.addEventListener('click', () => onClick(block));
return blockEl;
}
static createHourTick(hour) {
const tickContainer = document.createElement('div');
const tick = document.createElement('div');
const label = document.createElement('div');
tick.className = 'hour-tick';
label.className = 'hour-label';
const position = hour * CONFIG.PIXEL_PER_HOUR;
tick.style.left = `${position}px`;
label.style.left = `${position}px`;
label.textContent = TimeUtils.formatHourLabel(hour);
tickContainer.appendChild(tick);
tickContainer.appendChild(label);
return tickContainer;
}
static createMinuteTick(minute) {
const tick = document.createElement('div');
tick.className = 'minute-tick';
const position = minute * CONFIG.PIXEL_PER_MINUTE;
tick.style.left = `${position}px`;
return tick;
}
}
class Modal {
static show(block) {
const modal = document.getElementById('modal');
document.getElementById('modalTitle').textContent = 'Congratulations!';
const elapsedTimeString = TimeUtils.formatDuration(block);
document.getElementById('modalElapsedTime').textContent = `You fasted for ${elapsedTimeString}.`;
modal.style.display = 'flex';
}
static hide() {
document.getElementById('modal').style.display = 'none';
}
}
class DetailsModal {
static show(block) {
const modal = document.getElementById('detailsModal');
document.getElementById('detailsModalTitle').textContent = block.title;
document.getElementById('detailsModalDescription').textContent = block.description;
modal.style.display = 'flex';
}
static hide() {
document.getElementById('detailsModal').style.display = 'none';
}
}
class FastingTracker {
constructor() {
this.data = StorageService.getData();
this.initializeFastingState();
this.speedMultiplier = 1;
// Initialize buttons based on fasting state
this.updateButtonVisibility();
}
initializeFastingState() {
// Ensure this.data.fasts is initialized as an array
if (!Array.isArray(this.data.fasts)) {
this.data.fasts = [];
}
// Check for an active fast
const activeFast = this.data.fasts.find(fast => fast.endTime === null);
if (activeFast) {
this.startTime = new Date(activeFast.startTime);
this.isRunning = true; // Set isRunning to true if there's an active fast
} else {
this.startTime = null;
this.isRunning = false;
}
// Initialize buttons based on fasting state
this.updateButtonVisibility();
}
updateButtonVisibility() {
const startEditButton = document.getElementById('startEditButton');
const stopButton = document.getElementById('stopButton');
if (this.isFastingInProgress()) {
startEditButton.textContent = 'Edit Start Time';
stopButton.disabled = false;
} else {
startEditButton.textContent = 'Start Fasting';
stopButton.disabled = true;
}
}
showEditStartTimeModal() {
const editStartTimeModal = document.getElementById('editStartTimeModal');
const newStartTimeInput = document.getElementById('newStartTimeInput');
// Set the current start time in the input field
newStartTimeInput.value = this.startTime.toISOString().slice(0, 16);
editStartTimeModal.style.display = 'flex';
}
hideEditStartTimeModal() {
document.getElementById('editStartTimeModal').style.display = 'none';
}
confirmStartTimeEdit() {
const newStartTime = new Date(document.getElementById('newStartTimeInput').value);
// Update the start time of the current fast
this.startTime = newStartTime;
this.data.fasts[this.data.fasts.length - 1].startTime = newStartTime.toISOString();
StorageService.saveData(this.data);
// Update the UI
this.updateTimeline();
this.updateFastsTable();
this.hideEditStartTimeModal();
}
setSpeed(multiplier) {
this.speedMultiplier = multiplier;
document.querySelectorAll('.speed-btn').forEach(btn => {
btn.style.opacity = btn.id === (multiplier === 1 ? 'normalSpeed' : 'fastSpeed') ? '1' : '0.5';
});
}
startFasting() {
this.startTime = new Date();
this.data.fasts.push({ startTime: this.startTime.toISOString(), endTime: null, duration: null }); // Add new fast to the array
StorageService.saveData(this.data);
this.isRunning = true;
this.updateButtonVisibility(); // Update button visibility
this.updateTimeline();
}
isFastingInProgress() {
return this.startTime !== null;
}
stopFasting() {
const endTime = new Date();
const duration = endTime - this.startTime;
// Find the active fast and update its endTime and duration
const activeFastIndex = this.data.fasts.findIndex(fast => fast.endTime === null);
if (activeFastIndex !== -1) {
this.data.fasts[activeFastIndex].endTime = endTime.toISOString();
this.data.fasts[activeFastIndex].duration = duration;
} else {
// Handle the case where there's no active fast (shouldn't happen normally)
console.error("No active fast found when stopping.");
}
StorageService.saveData(this.data);
this.updateButtonVisibility(); // Update button visibility
this.isRunning = false;
document.getElementById('elapsedTime').textContent = '00:00:00';
this.startTime = null;
this.updateTimeline();
this.updateButtonVisibility(); // Ensure button text is updated
this.updateFastsTable();
// Display greeting with elapsed time
Modal.show(duration);
}
updateFastsTable() {
const tableBody = document.querySelector('#fastsTable tbody');
tableBody.innerHTML = '';
if (this.data.fasts.length === 0) {
document.getElementById('noFastsMessage').style.display = 'block';
return;
}
document.getElementById('noFastsMessage').style.display = 'none';
// Filter out the active fast (endTime is null) and then reverse the order to show newest first
this.data.fasts.filter(fast => fast.endTime !== null).reverse().forEach(fast => {
const row = tableBody.insertRow();
row.insertCell().textContent = new Date(fast.startTime).toLocaleString();
row.insertCell().textContent = new Date(fast.endTime).toLocaleString();
row.insertCell().textContent = TimeUtils.formatDuration(fast.duration);
// Add delete button
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.className = 'delete-button';
deleteButton.innerHTML = '<span class="mdi mdi-delete"></span>';
deleteButton.addEventListener('click', () => {
fastToDelete = fast; // Store the fast to be deleted
document.getElementById('confirmationModal').style.display = 'block'; // Show the confirmation modal
});
// Event listeners for confirmation modal buttons
document.getElementById('confirmDelete').addEventListener('click', () => {
this.data.fasts = this.data.fasts.filter(f => f !== fastToDelete);
StorageService.saveData(this.data);
this.updateFastsTable();
document.getElementById('confirmationModal').style.display = 'none'; // Hide the confirmation modal
fastToDelete = null; // Reset fastToDelete
});
document.getElementById('cancelDelete').addEventListener('click', () => {
document.getElementById('confirmationModal').style.display = 'none'; // Hide the confirmation modal
fastToDelete = null; // Reset fastToDelete
});
deleteCell.appendChild(deleteButton);
});
}
setupAnimation() {
setInterval(() => {
if (this.isFastingInProgress()) {
this.updateElapsedTime();
this.updateTimeline();
}
}, CONFIG.ANIMATION_INTERVAL);
}
updateElapsedTime() {
const elapsed = (Date.now() - this.startTime) * this.speedMultiplier;
document.getElementById('elapsedTime').textContent = TimeUtils.formatDuration(elapsed);
}
updateTimeline() {
const timeline = document.getElementById('timeline');
timeline.innerHTML = '';
// Add minute ticks
for (let minute = 0; minute <= CONFIG.TIMELINE_RANGE * 60; minute += 10) {
const tickEl = TimelineBlock.createMinuteTick(minute);
timeline.appendChild(tickEl);
}
// Add hour ticks
for (let hour = 0; hour <= 48; hour++) {
const tickEl = TimelineBlock.createHourTick(hour);
timeline.appendChild(tickEl);
}
// Calculate timeline offset based on startTime
if (this.startTime) {
const elapsedHours = (Date.now() - this.startTime) * this.speedMultiplier / (1000 * 60 * 60);
timeline.style.transform = `translateX(${-elapsedHours * CONFIG.PIXEL_PER_HOUR}px)`;
} else {
timeline.style.transform = 'translateX(0)';
}
// Add fasting phase blocks
CONFIG.SAMPLE_BLOCKS.forEach(block => {
// Null check for startTime
const safeStartTime = this.startTime ? this.startTime : new Date();
const blockEl = TimelineBlock.create(
block,
safeStartTime,
DetailsModal.show // Pass DetailsModal.show as the callback
);
timeline.appendChild(blockEl);
});
}
}
document.addEventListener('DOMContentLoaded', () => {
const tracker = new FastingTracker();
tracker.updateFastsTable();
tracker.updateTimeline(); // Render the timeline initially
tracker.setupAnimation();
// Setup event listeners
document.getElementById('startEditButton').addEventListener('click', () => {
if (tracker.isFastingInProgress()) {
tracker.showEditStartTimeModal();
} else {
tracker.startFasting();
}
});
document.getElementById('stopButton').addEventListener('click', () => tracker.stopFasting());
document.getElementById('closeModal').addEventListener('click', () => Modal.hide());
document.getElementById('closeDetailsModal').addEventListener('click', () => DetailsModal.hide());
document.getElementById('normalSpeed').addEventListener('click', () => tracker.setSpeed(1));
document.getElementById('fastSpeed').addEventListener('click', () => tracker.setSpeed(100));
document.getElementById('closeEditStartTimeModal').addEventListener('click', () => tracker.hideEditStartTimeModal());
document.getElementById('confirmStartTimeEdit').addEventListener('click', () => tracker.confirmStartTimeEdit());
document.getElementById('confirmDelete').addEventListener('click', () => {
// ... (Existing delete confirmation logic)
});
document.getElementById('cancelDelete').addEventListener('click', () => {
// ... (Existing delete cancellation logic)
});
});
</script>
</body>
</html>