-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
166 lines (139 loc) · 4.09 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
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
// Global variables
var colorPicker
var oneSecondInterval
var shutDownDate = 0
var serverTimerSeconds = 0
window.onload = function() {
calcPageLook()
colorPicker = new iro.ColorPicker("#picker",
{
borderWidth: 2,
borderColor: "#d3d3d3",
wheelLightness: false,
})
colorPicker.on('color:change',
function(color) {
setEffectBtnColor(color)
});
colorPicker.on("input:end",
function(color) {
fetch("/color", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(color.rgb)
})
})
// Get the app state from server
// Example response: {"color":{"r":0,"g":0,"b":0},"timer":333,"effect":"fire"}
fetch("/state")
.then((response) => response.json())
.then((data) => {
colorPicker.color.set(data.color)
toggleEffectBtn(data.effect)
setEffectBtnColor(colorPicker.color)
serverTimerSeconds = data.timer
timerHandler()
})
// Update the shut down timer
var timerValue = document.getElementById("timer-value")
var sliderInput = document.getElementById("timer-slider")
timerValue.textContent = sliderInput.value
sliderInput.addEventListener("input", (event) => { timerValue.textContent = event.target.value })
}
window.addEventListener("orientationchange",
function ()
{
calcPageLook()
});
function setEffectBtnColor(color) {
document.getElementById("ebtn-color").style.backgroundColor = color.hexString
document.documentElement.style.setProperty("--breathe-color", color.rgbString.slice(4, -1))
}
function toggleEffectBtn(effectName) {
// Simulate radio button behavior
let btnPressedClass = "effect-button-pressed"
let btnId = "ebtn-" + effectName
let btns = document.getElementsByTagName("button")
for (let i = 0; i < btns.length; i++) {
if (btns[i].classList.contains(btnPressedClass)) {
btns[i].classList.toggle(btnPressedClass)
}
}
document.getElementById(btnId).classList.toggle(btnPressedClass)
}
function runEffect(effectName) {
toggleEffectBtn(effectName)
fetch("/effect", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({effect: effectName})
})
}
function timerHandler() {
var icon = document.getElementById("timer-button-icon")
var slider = document.getElementById("timer-slider")
var time = document.getElementById("timer-value")
function clearTimer() {
clearInterval(oneSecondInterval)
serverTimerSeconds = 0
shutDownDate = 0
icon.className = "bi bi-clock"
time.textContent = 0
slider.disabled = false
slider.step = 5
slider.value = 0
}
function setTimerOnServer(timerValueSeconds) {
fetch("/timer", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({seconds: timerValueSeconds})
})
}
function intervalHandler() {
const remainingTimeSeconds = Math.round((shutDownDate - Date.now()) / 1000)
slider.value = Math.ceil(remainingTimeSeconds / 60)
time.textContent = slider.value
if (remainingTimeSeconds <= 0) {
colorPicker.color.set("#000000")
clearTimer()
}
}
function enableTimer() {
icon.className = "bi bi-clock-history"
slider.disabled = true
slider.step = 1
if (serverTimerSeconds == 0) {
serverTimerSeconds = slider.value * 60
setTimerOnServer(serverTimerSeconds)
}
shutDownDate = Date.now() + serverTimerSeconds * 1000
intervalHandler()
oneSecondInterval = setInterval(intervalHandler, 1000);
}
function disableTimer() {
clearTimer()
setTimerOnServer(0)
}
if (serverTimerSeconds == 0 && slider.value == 0) {
return
}
if (shutDownDate == 0) {
enableTimer()
} else {
disableTimer()
}
}
function calcPageLook() {
const isMobileDevice = /Mobi/i.test(window.navigator.userAgent)
if (!isMobileDevice) {
return
}
const orientation = (screen.orientation || {}).type || screen.mozOrientation || screen.msOrientation
const oriLandscape = orientation.startsWith("landscape")
if (oriLandscape) {
document.body.setAttribute("style", "height: 100vh;");
} else {
document.body.setAttribute("style", "height: auto;");
}
}