Skip to content

Commit 78c0ad6

Browse files
committed
Replace spinners with native js variant
1 parent e9695ae commit 78c0ad6

File tree

3 files changed

+115
-32
lines changed

3 files changed

+115
-32
lines changed

data/index.css

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ input, textarea {
9999
margin: 0px 4px;
100100
}
101101
.form-control {
102-
border-width: 0px;
102+
border-width: 0px !important;
103103
}
104104
.form-check {
105105
margin-top: 20px;
@@ -156,3 +156,41 @@ body .form-range::-moz-range-progress {
156156
max-width: 450px;
157157
margin: auto;
158158
}
159+
.spinner {
160+
padding: 10px 0px;
161+
}
162+
.spinner .btn-spinner {
163+
padding: 0px 5px;
164+
font-size: 19px;
165+
font-weight: bold;
166+
cursor: pointer;
167+
user-select: none;
168+
vertical-align: middle;
169+
}
170+
.spinner .btn-spinner:hover {
171+
color: white;
172+
}
173+
.spinner .spinner-input {
174+
-moz-appearance: textfield;
175+
-webkit-appearance: none;
176+
width: 100px;
177+
height: 32px;
178+
border: 1px solid #6E6F7A;
179+
border-radius: 5px;
180+
color: #000;
181+
text-align: center;
182+
padding: 0.375rem 0.75rem;
183+
line-height: 1.5;
184+
}
185+
.spinner input::-webkit-outer-spin-button,
186+
.spinner input::-webkit-inner-spin-button {
187+
-webkit-appearance: none;
188+
margin: 0;
189+
}
190+
.spinner input[type=number]{
191+
-moz-appearance: textfield;
192+
}
193+
.form-spinner-label {
194+
flex: 1;
195+
padding-top: 4px;
196+
}

data/index.htm

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,27 @@ <h5 class="offcanvas-title">Settings</h5>
4848
<button type="button" class="btn-close btn-close-white text-reset" data-bs-dismiss="offcanvas"></button>
4949
</div>
5050
<div class="offcanvas-body">
51+
5152
<h6>Startup animation</h6>
5253
<div class="form-check form-switch">
5354
<input type="checkbox" class="form-check-input" id="useStartupAnimation" data-bs-toggle="collapse" data-bs-target="#useStartupAnimationCollapse" aria-controls="useStartupAnimationCollapse">
5455
<label class="form-check-label" for="useStartupAnimation">Enabled</label>
5556
</div>
5657
<div id="useStartupAnimationCollapse" class="collapse">
57-
<label id="startupAnimationLabel" for="startupAnimation" class="form-label">Animation</label>
58+
<label for="startupAnimation" class="form-label">Animation</label>
5859
<select class="form-select" id="startupAnimation"></select>
5960
</div>
6061

62+
<h6>Time</h6>
63+
<div class="spinner d-flex">
64+
<label for="timeZone" class="form-spinner-label">Time zone (GMT)</label>
65+
<input id="timeZone" class="form-control" type="number" min="-23" max="23" step="1" value="0" />
66+
</div>
67+
<div class="form-check form-switch">
68+
<input type="checkbox" class="form-check-input" id="summerTime">
69+
<label class="form-check-label" for="summerTime">Summer time</label>
70+
</div>
71+
6172
<h6>Alarm</h6>
6273
<div class="form-check form-switch">
6374
<input type="checkbox" class="form-check-input" id="alarmEnabled" data-bs-toggle="collapse" data-bs-target="#alarmEnabledCollapse" aria-controls="alarmEnabledCollapse">
@@ -66,8 +77,10 @@ <h6>Alarm</h6>
6677
<div id="alarmEnabledCollapse" class="collapse">
6778
<label for="alarmTime" class="form-label">Time</label>
6879
<input id="alarmTime" type="time" class="form-control">
69-
<label for="alarmDuration" class="form-label">Duration</label>
70-
<input id="alarmDuration" name="alarmDuration">
80+
<div class="spinner d-flex mt-3">
81+
<label for="alarmDuration" class="form-spinner-label">Duration (min)</label>
82+
<input id="alarmDuration" class="form-control" type="number" min="1" max="1439" step="1" value="1" />
83+
</div>
7184
<label for="alarmAnimation" class="form-label">Animation</label>
7285
<select class="form-select" id="alarmAnimation"></select>
7386
<label for="postAlarmAnimation" class="form-label">Animation (timer finished)</label>
@@ -80,22 +93,20 @@ <h6>Sunset</h6>
8093
<label class="form-check-label" for="sunsetEnabled">Enabled</label>
8194
</div>
8295
<div id="sunsetEnabledCollapse" class="collapse">
83-
<label for="sunsetDuration" class="form-label">Duration</label>
84-
<input id="sunsetDuration" name="sunsetDuration">
85-
<label for="sunsetOffset" class="form-label">Offset</label>
86-
<input id="sunsetOffset" name="sunsetOffset">
96+
<div class="spinner d-flex">
97+
<label for="sunsetDuration" class="form-spinner-label">Duration (min)</label>
98+
<input id="sunsetDuration" class="form-control" type="number" min="1" max="1439" step="1" value="1" />
99+
</div>
100+
<div class="spinner d-flex">
101+
<label for="sunsetOffset" class="form-spinner-label">Offset (min)</label>
102+
<input id="sunsetOffset" class="form-control" type="number" min="-1439" max="1439" step="1" value="0" />
103+
</div>
87104
<label for="sunsetAnimation" class="form-label">Animation</label>
88105
<select class="form-select" id="sunsetAnimation"></select>
89106
<label for="longitude" class="form-label">Longitude</label>
90107
<input type="text" class="form-control" id="longitude" name="longitude">
91108
<label for="latitude" class="form-label">Latitude</label>
92109
<input type="text" class="form-control" id="latitude" name="latitude">
93-
<label for="timeZone" class="form-label">Time zone</label>
94-
<input id="timeZone" name="timeZone">
95-
<div class="form-check form-switch">
96-
<input type="checkbox" class="form-check-input" id="summerTime">
97-
<label class="form-check-label" for="summerTime">Summer time</label>
98-
</div>
99110
</div>
100111
</div>
101112

@@ -128,9 +139,7 @@ <h6>Sunset</h6>
128139
<div id="${colorPickerId}" class="text-center collapse"></div>
129140
</script>
130141

131-
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
132142
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
133-
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-touchspin/3.1.2/jquery.bootstrap-touchspin.min.js"></script>
134143
<script src="https://cdn.jsdelivr.net/npm/@jaames/iro@5"></script>
135144
<script src="index.js"></script>
136145
</body>

data/index.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
let connection;
2-
let ws_queue;
3-
let ws_cooldown_timer;
2+
let queue;
3+
let cooldownTimer;
44

55
function openWebsocketConnection() {
6-
const ws_uri = 'ws://' + (location.hostname ? location.hostname : 'localhost') + ':81/';
7-
connection = new WebSocket(ws_uri, ['arduino']);
6+
const uri = 'ws://' + (location.hostname ? location.hostname : 'localhost') + ':81/';
7+
connection = new WebSocket(uri, ['arduino']);
88
connection.binaryType = 'arraybuffer';
99
connection.onopen = function (e) {
1010
document.getElementById('navbarTitle').innerHTML = 'FastLEDHub';
@@ -23,6 +23,45 @@ function openWebsocketConnection() {
2323
};
2424
}
2525

26+
function setupSpinners() {
27+
const spinners = document.getElementsByClassName('spinner');
28+
[].forEach.call(spinners, (spinnerDiv) => {
29+
const inputElement = spinnerDiv.getElementsByTagName('input')[0];
30+
inputElement.classList.add('spinner-input');
31+
32+
const buttonDec = document.createElement("span");
33+
buttonDec.classList.add('btn-spinner', 'btn-spinner-dec');
34+
buttonDec.innerHTML = '&minus;';
35+
buttonDec.addEventListener('click', (ev) => {
36+
const attrMin = inputElement.getAttribute('min');
37+
const attrStep = inputElement.getAttribute('step');
38+
const value = Number(inputElement.value);
39+
const step = attrStep ? Number(attrStep) : 1;
40+
const newValue = value - step;
41+
if (value !== newValue) {
42+
inputElement.value = attrMin ? Math.max(Number(attrMin), newValue) : newValue;
43+
}
44+
});
45+
46+
const buttonInc = document.createElement("span");
47+
buttonInc.classList.add('btn-spinner', 'btn-spinner-inc');
48+
buttonInc.innerHTML = '&plus;';
49+
buttonInc.addEventListener('click', (ev) => {
50+
const attrMin = inputElement.getAttribute('max');
51+
const attrStep = inputElement.getAttribute('step');
52+
const value = Number(inputElement.value);
53+
const step = attrStep ? Number(attrStep) : 1;
54+
const newValue = value + step;
55+
if (value !== newValue) {
56+
inputElement.value = attrMin ? Math.min(Number(attrMin), newValue) : newValue;
57+
}
58+
});
59+
60+
inputElement.insertAdjacentElement('beforebegin', buttonDec);
61+
inputElement.insertAdjacentElement('afterend', buttonInc);
62+
});
63+
}
64+
2665
function displayConnectedState(connected) {
2766
document.getElementById('settingsButton').style.display = connected ? 'block' : 'none';
2867
document.getElementById('controlsWrapper').style.display = connected ? 'block' : 'none';
@@ -168,27 +207,27 @@ function sendConfig() {
168207
}
169208

170209
function sendBytes(bytes) {
171-
ws_queue = bytes;
210+
queue = bytes;
172211

173-
if (ws_cooldown_timer)
212+
if (cooldownTimer)
174213
return;
175214

176215
sendBytesQueue()
177-
ws_cooldown_timer = setTimeout(() => {
178-
ws_cooldown_timer = null;
216+
cooldownTimer = setTimeout(() => {
217+
cooldownTimer = null;
179218
sendBytesQueue();
180219
}, 15);
181220
}
182221

183222
function sendBytesQueue() {
184-
if (!ws_queue || connection.readyState != 1)
223+
if (!queue || connection.readyState != 1)
185224
return;
186225

187-
const data = Uint8Array.from(ws_queue);
226+
const data = Uint8Array.from(queue);
188227
connection.send(data.buffer);
189228
console.log('Sent bytes: ' + data);
190229

191-
ws_queue = null;
230+
queue = null;
192231
}
193232

194233
function sendText(text) {
@@ -204,12 +243,9 @@ function sendText(text) {
204243
window.onload = () => {
205244
openWebsocketConnection();
206245

246+
setupSpinners();
247+
207248
settingsOffcanvas.addEventListener('hidden.bs.offcanvas', function () {
208249
sendConfig();
209250
});
210-
211-
$('#alarmDuration').TouchSpin({ min: 1, max: 1439, postfix: 'minutes' });
212-
$('#timeZone').TouchSpin({ min: -23, max: 23, prefix: 'GMT+' });
213-
$('#sunsetDuration').TouchSpin({ min: 1, max: 1439, postfix: 'minutes' });
214-
$('#sunsetOffset').TouchSpin({ min: -1439, max: 1439, postfix: 'minutes' });
215251
};

0 commit comments

Comments
 (0)