-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.html
444 lines (418 loc) · 12.8 KB
/
client.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<!-- extract:style.html -->
<style>
html, body {
font-family: Arial;
font-size: 14px;
background: #fff;
padding: 3px;
color: #000;
margin: 0;
width: 100%;
line-height: 2em;
box-sizing: border-box;
}
body.disabled {
background-color: #ccc;
}
section {
width: 350px;
margin: 0 auto;
}
fieldset>legend {
font-weight: bolder;
}
label>span,
div>span {
display: inline-block;
margin-right: 2px;
}
div.h {
margin-left: 22px;
}
div>span {
width: 52px;
text-align: center;
}
label>span {
width: 50px;
height: 25px;
border: 1px solid black;
vertical-align: bottom;
}
header, footer {
text-align: center;
}
footer {
color: #888;
font-size: 0.75rem;
}
#l a {
margin-left:5px;
}
.help {
font-size: 0.8rem;
font-style: italic;
line-height: 1em;
margin: 10px auto;
}
</style>
<!-- endextract -->
</head>
<body>
<!-- extract:body.html -->
<header>
<h1>WiFibonacci Clock Settings</h1><!-- a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,r,s,t -->
</header>
<section>
<form>
<fieldset>
<legend>General</legend>
<input type="radio" name="m" id="m0" onclick="zm(this)" value="0"><label for="m0">Clock</label><br>
<input type="radio" name="m" id="m1" onclick="zm(this)" value="1"><label for="m1">Rainbow Cycle</label><br>
<input type="radio" name="m" id="m2" onclick="zm(this)" value="2"><label for="m2">Rainbow</label><br>
<input type="radio" name="m" id="m3" onclick="zm(this)" value="3"><label for="m3">Random</label><br>
<input type="radio" name="m" id="m4" onclick="zm(this)" value="4"><label for="m4">Pulse</label><br>
<input type="radio" name="m" id="m5" onclick="zm(this)" value="5"><label for="m5">Constant Light</label><br>
Brightness: <input name="b" type="range" min="1" max="255" onchange="y(this,'b')"> <input type="number" name="c" min="1" max="255" oninput="y(this,'b')">
</fieldset>
<fieldset>
<legend>Clock</legend>
Date and time:<br>
<input type="datetime-local" name="t" oninput="z(this,'t')"> <input type="button" onclick="x(t)" value="now"><br>
Palettes:<br>
<div class="h"><span>off</span><span>hours</span><span>minutes</span><span>both</span></div>
<div id="l"></div>
Upload palette:<br>
<input type="file" name="i"> <input type="button" onclick="q(i,'c',1)" value="upload">
</fieldset>
<fieldset>
<legend>Rainbow</legend>
Delay (ms): <input name="r" type="range" min="1" max="100" onchange="y(this,'r')"> <input type="number" name="s" min="1" max="100" oninput="y(this,'r')">
</fieldset>
<fieldset>
<legend>Random</legend>
Delay (ms): <input name="j" type="range" min="1" max="9999" onchange="y(this,'j')"> <input type="number" name="k" min="1" max="9999" oninput="y(this,'j')"><br>
Ease (ms): <input name="g" type="range" min="1" max="100" onchange="y(this,'g')"> <input type="number" name="h" min="1" max="100" oninput="y(this,'g')">
</fieldset>
<fieldset>
<legend>Pulse</legend>
Color: <input name="p" type="color" oninput="z(this,'p')"><br>
Delay (ms): <input name="d" type="range" min="1" max="100" onchange="y(this,'p')"> <input type="number" name="e" min="1" max="100" oninput="y(this,'p')">
</fieldset>
<fieldset>
<legend>Constant light</legend>
Color: <input name="f" type="color" oninput="z(this,'f')">
</fieldset>
<fieldset>
<legend>Settings</legend>
<input type="button" onclick="dl()" value="Download settings"> <input type="button" onclick="rl(this)" value="Restore factory settings"><br>
Load file:<br>
<input type="file" name="n"> <input type="button" onclick="q(n,'')" value="load">
</fieldset>
</form>
</section>
<section class="help">
If your WiFibonacci Clock briks, do a hard reset by maintaining the mode button pushed and then push the reset button.<br>
Warning: this will delete all settings and palettes.
</section>
<footer>
© 2017 Clément Ronzon
</footer>
<!-- endextract -->
<script>
/* extract:script.js */
/** Keep in mind that anything defined inside this extracted section will live in the scope of _w.onmessage function. Remember to publish whatever needs to be published. **/
/*
* Downloads the current settings as a JSON file
*/
window.dl = function () {
data = {
mode: _f.m.value * 1,
brightness: _f.b.value * 1,
palette: n(sa('input[name=l]:checked')[0]) * 1,
flashLightColor: _f.f.value.replace(/#/,''),
rainbowDelay: _f.r.value * 1,
pulse: {
color: _f.p.value.replace(/#/,''),
delay: _f.d.value * 1
},
random: {
ease: _f.g.value * 1,
delay: _f.j.value * 1
}
};
var a = document.createElement('a');
a.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data)));
a.setAttribute('download', 'settings.json');
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
/*
* Selects the palette at a given index
* @param {number} i Index of the palette to be selected
*/
window.m = function (i) {
var l = _f.l[i] ? _f.l[i] : _f.l;
l.checked = 1;
};
/*
* Gives the index of current node in the list
* @param {Object} a DOM anchor node
* @returns {string} two digis formatted index
*/
window.n = function (a) {
var li = a.parentNode,
i = 0;
while (li.previousElementSibling) {
li = li.previousElementSibling;
i ++;
}
return v(i);
};
/*
* Loads the settings to the UI
* @param {Object} obj Plane object containing the settings
*/
window.o = function (obj) {
_f.b.value = obj.brightness;
_f.c.value = _f.b.value;
_f.d.value = obj.pulse.delay;
_f.e.value = _f.d.value;
_f.f.value = '#' + obj.flashLightColor;
_f.g.value = obj.random.ease;
_f.h.value = _f.g.value;
_f.j.value = obj.random.delay;
_f.k.value = _f.j.value;
m(obj.palette);
_f.m.value = obj.mode;
_f.p.value = '#' + obj.pulse.color;
_f.r.value = obj.rainbowDelay;
_f.s.value = _f.r.value;
_f.t.value = obj.dateTime;
};
/*
* Creates and add a palette element to the UI
* @param {string[4]} arr Array containing the four colors that define a palette
*/
window.oo = function (arr) {
var rand = Math.random(),
html = '<div><input type="radio" name="l" id="l' + rand + '" onclick="zl(this)"><label for="l' + rand + '">';
for (var i = 0; i < 4; i++) html += '<span style="background-color:#' + arr[i] + '"></span>';
html += arr[4] + '</label>';
if (_f.l) html += '<a href="#" onclick="zd(this)">X</a>';
html += '</div>';
t(s('#l'), html);
};
/*
* Alerts the user that something went wrong
*/
window.p = function () {
alert('Failure');
};
/*
* Reads a file as text and sends its content to the websocket server
* @param {Object} input DOM input node
* @param {string} code Action code
* @param {boolean} sendFileName If true a 'file name' message is sent before the file content
*/
window.q = function (input, code, sendFileName) {
var file = input.files[0],
fr = new FileReader();
fr.onerror = p;
fr.onload = function (e) {
if (sendFileName) {
zz(file.name, 'a');
}
input.value = '';
zz(e.target.result, code);
};
fr.readAsText(file);
};
/*
* Ask for confirmation, sends a 'factory settings' message and reloads the page
*/
window.rl = function (input) {
if (confirm(input.value + '. Are you sure?')) {
zz('0', 'f');
location.reload();
}
};
/*
* Detects if a value is out of boundaries
* @param {Object} input DOM input node
* @returns {boolean} Returns false if the value is not empty and is within boundaries (when applies), false otherwise
*/
window.u = function (input) {
var val = input.value;
return val === '' || input.min && val * 1 < input.min * 1 || input.max && val * 1 > input.max * 1;
};
/*
* Formats a number on two digits (left zero filled)
* @param {string|number} val Value to be formatted
* @returns {string} formatted number
*/
window.v = function (val) {
return w(val, 2);
};
/*
* Formats a number to a fixed lenght string filled with zeros on its left
* @param {string|number} val Value to be formatted
* @param {number} size Size of the resulting string
* @returns {string} formatted number
*/
window.w = function (val, size) {
val += '';
while (val.length < size) val = '0' + val;
return val;
};
/*
* Sends a 'date time' message
* @param {Object} input DOM input node
*/
window.x = function (input) {
var date = new Date(),
hours = v(date.getHours()),
minutes = v(date.getMinutes()),
seconds = v(date.getSeconds()),
year = date.getFullYear(),
month = date.getMonth(),
day = v(date.getDate());
input.value = year + '-' + v(month + 1) + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds; //2009-02-06T01:04:05
zz(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][month] + ' ' + day + ' ' + year + hours + ':' + minutes + ':' + seconds, 't'); //Feb 06 200901:04:05
};
/*
* Sends a four digit formatted input value
* @param {Object} input DOM input node holding the value
* @param {string} code Action code
*/
window.y = function (input, code) {
if (u(input)) return;
zz(w(input.value, 4), code);
};
/*
* Sends a message to the websocket server
* @param {Object} input DOM input node
* @param {string} code Action code
*/
window.z = function (input, code) {
if (u(input)) return;
var val = input.value;
console.log(code + val);
_w.send(code + val);
};
/*
* Helps to send a message by value
* @param {string|number} val Value to be sent
* @param {string} code Action code
*/
window.zz = function (val, code) {
z({value: val}, code);
};
/*
* Sends a 'palette' message
* @param {Object} input DOM input node
*/
window.zl = function (input) {
zz(n(input), 'l');
};
/*
* Sends a 'palette delete' message
* @param {Object} input DOM input node
*/
window.zd = function (input) {
zz(n(input), 'd');
};
/*
* Sends a 'mode' message
* @param {Object} input DOM input node
*/
window.zm = function (input) {
z(input, 'm');
};
/* endextract */
/*
* Fetches the DOM node matching the selector
* @param {string} selector
* @returns {Object} DOM node
*/
function s(selector) {
return document.querySelector(selector);
}
/*
* Fetches all DOM nodes matching the selector
* @param {string} selector
* @returns {Object[]} DOM nodes
*/
function sa(selector) {
return document.querySelectorAll(selector);
}
/*
* Adds html nodes to an existing DOM container
* @param {Object} container DOM node
* @param {string} html the html code
*/
function t(container, html) {
var d = document.createElement('div');
d.innerHTML = html;
while (d.children.length > 0) container.appendChild(d.children[0]);
}
var _f, _w = new WebSocket('ws://' + location.hostname + ':81/', ['arduino']);
/*
* Handles messages comming from the websocket server
* @param {Object} messageEvent the object containing the message
*/
_w.onmessage = function (messageEvent) {
var data = messageEvent.data;
if (data == 'e') { // Server returned failure
p();
} else {
switch (data[0]) { // Check the first char
case '{': // This is a setting JSON
o(JSON.parse(data));
break;
case '[': // This is a palette JSON
oo(JSON.parse(data));
break;
case '<': // This is some HTML
t(s(data.indexOf('<style>') == 0 ? 'head' : 'body'), data);
_f = s('form');
break;
case 'd': // A palette has been deleted
var i = data.slice(1) * 1 + 1;
s('#l div:nth-child(' + i + ')').remove();
break;
default:
eval(data); // This must be some JS
}
}
};
/*
* Handles errors comming from the websocket server
* @param {Object} errorEvent the object containing the message
*/
_w.onerror = function (errorEvent) {
console.log('errorEvent: ');
console.log(errorEvent);
};
/*
* Handles when the websocket server closes
* @param {Object} closeEvent the object containing the message
*/
_w.onclose = function (closeEvent) {
console.log('closeEvent: ');
console.log(closeEvent);
alert('Wifi signal interrupted');
s('body').className = 'disabled';
sa('body input').forEach(function (input) {
input.disabled = 1;
});
};
</script>
</body>
</html>