-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
178 lines (160 loc) · 4.99 KB
/
main.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
"use strict";
// User configuration
var dailyItems = {
B: [
"Tea + Coffee",
"Boiled Egg / Omelette",
"Bread + Butter + Jam",
"Bournvita",
"Hot + Cold Milk",
"Cornflakes",
],
L: [],
S: ["Tea + Coffee"],
D: [],
};
// Global variables
let selected_day = 0;
let menu = null;
function setupPWABanner() {
document.querySelector(".pwa-install-banner").style.display = "block";
const ua = navigator.userAgent;
let install_text = "";
if (
[
"iPad Simulator",
"iPhone Simulator",
"iPod Simulator",
"iPad",
"iPhone",
"iPod",
].includes(navigator.platform)
) {
// iOS + not Safari
install_text = "[In Safari] Share → Add to Home Screen";
} else if (ua.match(/edg/i)) {
// Edge
install_text = "Options (≡) → Add to phone";
} else if (ua.match(/samsungbrowser/i)) {
// Fkin Samsung Internet
install_text = "Click on the download button (near the URL)";
} else if (ua.match(/chrome|chromium|crios/i)) {
// Chrome
install_text = "Options (⋮) → Install App";
} else if (ua.match(/firefox|fxios/i)) {
// Firefox
install_text = "Options (⋮) → Install App";
} else if (ua.match(/opr\//i)) {
// Opera
install_text = "Options (⋮) → Install App";
} else {
// Unknown / Unsupported Browser
install_text = "Options → Install App";
}
document.querySelector("#pwa-install-instructions").innerText =
install_text;
}
/**
* Make the document jump to a day's meal based on the current time.
*/
function jumpToMenuPosition() {
// TODO: Meal times shouldn't be hardcoded
// Jump to the current time in the day
const current_hours = new Date().getHours();
let scrollAmt = -document.querySelector(".day-picker").offsetHeight;
if (current_hours < 10) {
// Already at #B
} else if (current_hours < 14) {
// Jump to #L
scrollAmt += document.querySelector("#L").offsetTop;
} else if (current_hours < 18) {
// Jump to #S
scrollAmt += document.querySelector("#S").offsetTop;
} else if (current_hours < 22) {
// Jump to #D
scrollAmt += document.querySelector("#D").offsetTop;
}
document.querySelector("#actual-menu").scrollTo(0, scrollAmt);
}
/**
* Asynchronously load the menu, and update the UI accordingly.
*/
async function loadMenu() {
menu = await fetch("./menu.json").then((res) => res.json());
addDailyItemsToMenu();
initializeMenuUI(selected_day);
jumpToMenuPosition();
}
// Immediately invoked setup function
(async function setup() {
loadMenu();
// PWA Analytics
if (
navigator.standalone === true ||
window.matchMedia("(display-mode: standalone)").matches
) {
gtag("event", "pwa_use", {
event_category: "engagement",
value: 1,
});
} else {
setupPWABanner();
}
// getDay() returns 0 for Sunday
// Shift it so that 0 represents Monday
selected_day = (new Date().getDay() + 6) % 7;
// Set the current day and select it
document
.querySelector(".day-picker")
.children[selected_day].classList.add(
"day-choice--today",
"day-choice--selected"
);
// Setup the day choice buttons
document.querySelectorAll(".day-choice").forEach((day_choice, index) => {
day_choice.onclick = (event) => {
if ("vibrate" in navigator) navigator.vibrate(50);
document
.querySelector(".day-choice--selected")
.classList.remove("day-choice--selected");
day_choice.classList.add("day-choice--selected");
selected_day = index;
initializeMenuUI(selected_day);
};
});
})()
/**
* Update the menu shown on screen for the given day.
*
* @param {number} dayIndex
*/
function initializeMenuUI(dayIndex) {
let items = Object.entries(menu)[dayIndex][1];
for (let [mealName, list] of Object.entries(items)) {
document
.getElementById(mealName)
.querySelector(".menu__items").innerHTML = [
...list[0].map(variableItemHTML),
...list[1].map(everydayItemHTML),
].join("");
}
}
function variableItemHTML(item_name) {
return `<div class="menu__item menu__item--variable">${item_name}</div>`;
}
function everydayItemHTML(item_name) {
return `<div class="menu__item menu__item--everyday">${item_name}</div>`;
}
/**
* Add daily items to each the meal items of each meal in the menu.
* Modifies the menu in-place.
*/
function addDailyItemsToMenu() {
for (let day of Object.keys(menu)) {
for (let [mealName, items] of Object.entries(menu[day])) {
let variable_items = items;
let dailyItemsForMeal = dailyItems[mealName];
menu[day][mealName] = [variable_items, dailyItemsForMeal];
}
}
}