-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
136 lines (111 loc) · 3.59 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
const datePicker = $("#date-picker");
// const buildingPicker = $("#building-picker");
const yesterdayButton = $("#yesterday");
const tomorrowButton = $("#tomorrow");
const wadsBtm = $("#wads");
const mcnairBtm = $("#mcnair");
const dhhBtm = $("#dhh");
const weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
let buildingMenu = undefined;
datePicker.on("change", () => onDateChange(datePicker.val()));
// buildingPicker.on("change", () => onBuildingChange(buildingPicker.val()));
wadsBtm.on("click", () => {
onBuildingChange(wadsBtm.val());
});
mcnairBtm.on("click", () => {
onBuildingChange(mcnairBtm.val());
});
dhhBtm.on("click", () => {
onBuildingChange(dhhBtm.val());
});
yesterdayButton.on("click", () => {
let date = datePicker.datepicker("getDate");
date.setDate(date.getDate() - 1);
datePicker.datepicker("setDate", date);
onDateChange(date);
});
tomorrowButton.on("click", () => {
let date = datePicker.datepicker("getDate");
date.setDate(date.getDate() + 1);
datePicker.datepicker("setDate", date);
onDateChange(date);
});
datePicker.datepicker();
datePicker.datepicker("setDate", new Date());
datePicker.datepicker({
minDate: new Date(2019, 0, 14),
maxDate: new Date(2019, 4, 4)
});
onDateChange(datePicker.val());
// onBuildingChange(buildingPicker.val());
onBuildingChange("wads");
function onDateChange(date) {
date = parseDate(date); // transform date into an actual Date object
$("#dayofweek").text(weekdays[date.getDay()])
if (!buildingMenu) {
return;
}
$(".loading").hide();
$(".menu").show();
$(".menu-row").remove();
let weekNum = Math.floor(calendarDays(new Date(2019, 0, 14), date) / 7);
console.log(weekNum)
if (weekNum === 8) {
noData();
return;
}
if (weekNum > 8) {
weekNum--;
}
weekday = ((date.getDay() - 1) + 7) % 7;
const dayMenu = buildingMenu[weekNum][weekday];
const maxLength = Math.max(dayMenu.breakfast.length, dayMenu.lunch.length, dayMenu.dinner.length);
for (let i = 0; i < maxLength; i++) {
$(".menu").append($("<tr>").addClass("menu-row").append(
$("<td>").text(dayMenu.breakfast[i] || ""),
$("<td>").text(dayMenu.lunch[i] || ""),
$("<td>").text(dayMenu.dinner[i] || "")
));
}
if (maxLength === 0 || (maxLength === 1 && dayMenu.breakfast[0] === "")) {
noData()
}
}
function noData() {
$(".menu-row").remove();
$(".menu").append(
$("<tr>").addClass("menu-row").append("<td colspan='3'>No data available</td>")
)
}
async function onBuildingChange(building) {
console.log(building)
$(".menu").hide();
$(".loading").show();
$(".menurow").remove();
const res = await fetch("data/"+building+"/data.json");
buildingMenu = await res.json();
$("#heading").text(building.toUpperCase())
onDateChange(datePicker.val())
}
// parses date in format mm/dd/yyyy
function parseDate(date) {
if (date instanceof Date) {
return date;
}
let sections = date.split("/");
if (sections.length != 3) {
throw new Error("sections length must be 3");
}
let retVal = new Date(parseInt(sections[2], 10), parseInt(sections[0], 10)-1, parseInt(sections[1], 10))
return retVal;
}
function calendarDays(start, end) {
let cur = new Date(start.getYear(), start.getMonth(), start.getDate());
let stop = new Date(end.getYear(), end.getMonth(), end.getDate());
let days = 0;
while (cur < stop) {
cur.setDate(cur.getDate()+1);
days++;
}
return days;
}