-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
183 lines (152 loc) · 5.77 KB
/
script.js
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
document.getElementById("wakeup-time").addEventListener("input", () => {
calculateMealTimes();
createHourLines();
updateIndicator();
});
const MEAL_BREAKFAST = "Breakfast";
const MEAL_MMS = "Mid-morning Snack";
const MEAL_LUNCH = "Lunch";
const MEAL_AS = "Afternoon Snack";
const MEAL_DINNER = "Dinner";
const getMealTimes = (wakeupTime) => {
const breakfastTime = new Date(wakeupTime.getTime() + 1 * 60 * 60 * 1000);
const midMorningSnackTime = new Date(
breakfastTime.getTime() + 3 * 60 * 60 * 1000
);
const lunchTime = new Date(breakfastTime.getTime() + 5 * 60 * 60 * 1000);
const arvoSnackTime = new Date(lunchTime.getTime() + 3 * 60 * 60 * 1000);
const dinnerTime = new Date(lunchTime.getTime() + 5 * 60 * 60 * 1000);
return {
[MEAL_BREAKFAST]: {
time: breakfastTime,
rangeStart: wakeupTime,
rangeEnd: new Date(wakeupTime.getTime() + 1 * 60 * 60 * 1000),
},
[MEAL_MMS]: {
time: midMorningSnackTime,
rangeStart: new Date(breakfastTime.getTime() + 2 * 60 * 60 * 1000),
rangeEnd: new Date(breakfastTime.getTime() + 3 * 60 * 60 * 1000),
},
[MEAL_LUNCH]: {
time: lunchTime,
rangeStart: new Date(breakfastTime.getTime() + 4 * 60 * 60 * 1000),
rangeEnd: new Date(breakfastTime.getTime() + 5 * 60 * 60 * 1000),
},
[MEAL_AS]: {
time: arvoSnackTime,
rangeStart: new Date(lunchTime.getTime() + 2 * 60 * 60 * 1000),
rangeEnd: new Date(lunchTime.getTime() + 3 * 60 * 60 * 1000),
},
[MEAL_DINNER]: {
time: dinnerTime,
rangeStart: new Date(lunchTime.getTime() + 4 * 60 * 60 * 1000),
rangeEnd: new Date(lunchTime.getTime() + 5 * 60 * 60 * 1000),
},
};
};
const calculateMealTimes = () => {
const wakeupTimeInput = document.getElementById("wakeup-time").value;
if (!wakeupTimeInput) {
return;
}
const wakeupTime = new Date(`1970-01-01T${wakeupTimeInput}:00`);
const mealTimes = getMealTimes(wakeupTime);
const mealTimesDiv = document.getElementById("meal-times");
mealTimesDiv.innerHTML = "";
for (let meal in mealTimes) {
const mealTime = mealTimes[meal];
const timeStr = formatTime(mealTime.time);
const rangeStartStr = formatTime(mealTime.rangeStart);
const rangeEndStr = formatTime(mealTime.rangeEnd);
let rangeStr = `Between ${rangeStartStr} and ${rangeEndStr}`;
if (meal === MEAL_BREAKFAST) {
rangeStr = "Within an hour of waking up";
}
mealTimesDiv.innerHTML += `
<div class="meal-time">
<strong>${meal}</strong>: ${timeStr}
<div class="range">${rangeStr}</div>
</div>
`;
}
};
const formatTime = (date) => {
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${hours}:${minutes}`;
};
const setWakeupTime = (time) => {
document.getElementById("wakeup-time").value = time;
// Trigger the input event to recalculate meal times if necessary
document.getElementById("wakeup-time").dispatchEvent(new Event("input"));
};
function updateIndicator() {
const wakeupTimeInput = document.getElementById("wakeup-time").value;
if (!wakeupTimeInput) {
return;
}
const wakeupTime = new Date(`1970-01-01T${wakeupTimeInput}:00`);
const startHour = getStartHour();
const startOfDay = new Date(new Date("1970-01-01T00:00:00").setHours(startHour));
const endOfDay = new Date("1970-01-02T00:00:00");
const indicatorContainer = document.getElementById("indicator-container");
const totalDuration = endOfDay.getTime() - startOfDay.getTime();
// Remove existing indicators
const existingIndicators =
indicatorContainer.getElementsByClassName("indicator");
while (existingIndicators.length > 0) {
existingIndicators[0].remove();
}
function addIndicator(time, className, label) {
const start = time.rangeStart;
const end = time.rangeEnd;
const duration = end.getTime() - start.getTime();
const left =
((start.getTime() - startOfDay.getTime()) / totalDuration) * 100;
const width = (duration / totalDuration) * 100;
const indicator = document.createElement("div");
indicator.className = `indicator ${className}`;
indicator.style.left = `${left}%`;
indicator.style.width = `${width}%`;
indicator.innerHTML = `<span>${label}</span>`;
indicatorContainer.appendChild(indicator);
}
const mealTimes = getMealTimes(wakeupTime);
const breakfastTime = mealTimes[MEAL_BREAKFAST];
const midMorningSnackTime = mealTimes[MEAL_MMS];
const lunchTime = mealTimes[MEAL_LUNCH];
const arvoSnackTime = mealTimes[MEAL_AS];
const dinnerTime = mealTimes[MEAL_DINNER];
addIndicator(breakfastTime, "breakfast", "Breakfast");
addIndicator(midMorningSnackTime, "mid-morning-snack", "Mid-morning Snack");
addIndicator(lunchTime, "lunch", "Lunch");
addIndicator(arvoSnackTime, "afternoon-snack", "Afternoon Snack");
addIndicator(dinnerTime, "dinner", "Dinner");
}
const getStartHour = () => {
const wakeupTimeInput = document.getElementById("wakeup-time").value;
if (wakeupTimeInput) {
const wakeupHour = new Date(`1970-01-01T${wakeupTimeInput}:00`).getHours();
if (wakeupHour <= 2) {
return wakeupHour;
}
return wakeupHour - 2;
}
return 0
};
function createHourLines() {
const indicatorContainer = document.getElementById("indicator-container");
indicatorContainer.replaceChildren([])
const startHour = getStartHour();
for (let i = startHour; i < 24; i++) {
const hourLine = document.createElement("div");
hourLine.className = "hour-line";
const hourLabel = document.createElement("div");
hourLabel.className = "hour-label";
hourLabel.innerText = `${i}:00`;
hourLine.appendChild(hourLabel);
indicatorContainer.appendChild(hourLine);
}
}
// Create hour lines on page load
createHourLines();