Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (60 loc) · 1.88 KB
/
Copy pathscript.js
File metadata and controls
75 lines (60 loc) · 1.88 KB
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
const prayerPeriods = {
morning: {
label: "Good morning",
switchLabel: "Switch to evening prayer",
},
evening: {
label: "Good evening",
switchLabel: "Switch to morning prayer",
},
};
const prayerElements = document.querySelectorAll("[data-prayer-period]");
const periodToggle = document.querySelector("#period-toggle");
const periodLabel = document.querySelector("#period-label");
const currentTime = document.querySelector("#current-time");
const timeFormatter = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "2-digit",
timeZoneName: "short",
});
let automaticPeriod = null;
let selectedPeriod = null;
function getPrayerPeriod(date) {
return date.getHours() < 12 ? "morning" : "evening";
}
function updatePeriodLabel(period) {
const copy = prayerPeriods[period];
periodLabel.textContent = copy.label;
periodToggle.setAttribute("aria-label", copy.switchLabel);
}
function showPrayer(period) {
selectedPeriod = period;
prayerElements.forEach((element) => {
element.hidden = element.dataset.prayerPeriod !== period;
});
updatePeriodLabel(period);
}
function updateClock() {
const now = new Date();
const nextAutomaticPeriod = getPrayerPeriod(now);
currentTime.textContent = timeFormatter.format(now);
currentTime.dateTime = now.toISOString();
if (nextAutomaticPeriod !== automaticPeriod) {
automaticPeriod = nextAutomaticPeriod;
showPrayer(nextAutomaticPeriod);
}
}
function togglePrayer() {
const nextPeriod = selectedPeriod === "morning" ? "evening" : "morning";
showPrayer(nextPeriod);
}
function refreshWhenVisible() {
if (document.visibilityState === "visible") {
updateClock();
}
}
periodToggle.addEventListener("click", togglePrayer);
document.addEventListener("visibilitychange", refreshWhenVisible);
window.addEventListener("pageshow", updateClock);
updateClock();
window.setInterval(updateClock, 60_000);