-
Notifications
You must be signed in to change notification settings - Fork 8
/
open-hours.js
80 lines (66 loc) · 2.1 KB
/
open-hours.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
(function (window) {
"use strict"
function define_openHours () {
var OpenHours = {}
var insertColon = function (time) {
var center = time.length - 2
var timeWithColon = [time.slice(0, center), ":", time.slice(center)].join("")
return timeWithColon
}
var formatTime = function (time) {
if (time) {
if (time >= 1200) {
var formattedTime = time === 1200 ? 1200 : time - 1200
return `${insertColon(formattedTime.toString())} PM`
} else {
return `${insertColon(time.toString())} AM`
}
}
}
const weekIndex = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday"
}
var determineDay = function (day, dayDiv) {
var now = new Date()
if (day === weekIndex[now.getDay()]) {
dayDiv.style["font-weight"] = "bold"
}
}
OpenHours["generateTime"] = function (hours) {
var rootElement = document.getElementById("open-hours")
var table = document.createElement("table")
if (rootElement) {
for (var day in hours) {
var dayDiv = document.createElement("tr")
determineDay(day, dayDiv)
var dayTitleElement = document.createElement("td")
var dayTimesElement = document.createElement("td")
var dayTitle = document.createTextNode(day)
dayTitleElement.appendChild(dayTitle)
dayDiv.appendChild(dayTitleElement)
var dayHours = ""
if (Object.keys(hours[day]).length === 0) {
dayHours = `Closed`
} else {
dayHours = `${formatTime(hours[day]["start"])} - ${formatTime(hours[day]["end"])}`
}
var dayTimes = document.createTextNode(dayHours)
dayTimesElement.appendChild(dayTimes)
dayDiv.appendChild(dayTimesElement)
table.appendChild(dayDiv)
}
rootElement.appendChild(table)
}
}
return OpenHours
}
if (typeof OpenHours === "undefined") {
window.OpenHours = define_openHours()
}
}(window))