-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.js
113 lines (113 loc) · 3.5 KB
/
functions.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
function getExtendedTime() {
var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
if (hour < 5)
hour += 24;
var time = (hour * 100) + minute;
return time;
}
function insertColon(time) {
return time.slice(0, -2) + ":" + time.slice(-2, time.length);
}
function stringifyHour(hour) {
var output = "";
if ((hour == 2400) || (hour == 0)) {
output = "midnight";
}
else if (hour == 1200) {
output = "noon";
}
else if (hour > 2400) {
hour -= 2400;
if (hour < 100)
hour += 1200;
output += insertColon(hour.toString()) + "am";
}
else if (hour > 1200) {
hour -= 1200;
if (hour < 100)
hour += 1200;
output += insertColon(hour.toString()) + "pm";
}
else {
if (hour < 100)
hour += 1200;
output = insertColon(hour.toString()) + "am";
}
return output;
}
function printUpcomingOpenings(place) {
var hours = place.getHoursForToday();
var time = getExtendedTime();
var output = "";
for (var i = 0; i < hours.length; i++) {
var x = hours[i];
if (time < x.getClose()) {
output += x.getIntervalString() + " and ";
}
}
if ((hours == Interval.none) || (output == "")) {
var hours = place.getHoursForTomorrow();
if (hours == Interval.none)
return "closed for today and tomorrow";
output += "[Tomorrow] ";
for (var i = 0; i < hours.length; i++) {
var x = hours[i];
output += x.getIntervalString() + " and ";
}
}
return output.slice(0, -5);
}
function redrawCurrentTime() {
var date = new Date();
var time = (date.getHours() * 100) + date.getMinutes();
var timeString = "Current time is: " + stringifyHour(time);
var currentTime = document.getElementById("currentTime");
currentTime.innerHTML = timeString;
}
function redrawPlaces() {
var openPlaces = document.getElementById("openPlaces");
var closedPlaces = document.getElementById("closedPlaces");
openPlaces.innerHTML = "";
closedPlaces.innerHTML = "";
var placeGroup = [];
if (document.getElementById("foodPlaces").checked) {
placeGroup = placeGroup.concat(foodPlaces);
}
if (document.getElementById("libraryPlaces").checked) {
placeGroup = placeGroup.concat(libraryPlaces);
}
if (document.getElementById("miscPlaces").checked) {
placeGroup = placeGroup.concat(miscPlaces);
}
for (var i = 0; i < placeGroup.length; i++) {
var place = placeGroup[i];
var row = document.createElement("tr");
var name = document.createElement("td");
var spacer = document.createElement("td");
var hours = document.createElement("td");
var nametext = document.createTextNode(place.getName());
var hourstext = document.createTextNode(printUpcomingOpenings(place));
name.appendChild(nametext);
hours.appendChild(hourstext);
row.appendChild(name);
row.appendChild(spacer);
row.appendChild(hours);
if (place.isOpenNow()) {
addClass(row, "open");
openPlaces.appendChild(row);
}
else {
addClass(row, "closed");
closedPlaces.appendChild(row);
}
}
}
window.onload = function () {
fetchDatabase(redrawPlaces);
redrawCurrentTime();
setInterval(redrawCurrentTime, 5000);
redrawPlaces();
setInterval(redrawPlaces, 5000);
};