-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
54 lines (44 loc) · 1.38 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
var doorButton = document.getElementById('door');
var statusDisplay = document.getElementById('status');
var idleTimer;
function openDoor () {
setState('pending');
var script = document.createElement('script');
script.src = 'http://192.168.2.5/letmein';
script.onload = function () {
clearTimeout(timeout);
setState('success');
};
script.onerror = function () {
clearTimeout(timeout);
setState('error');
};
var timeout = setTimeout(function () {
script.onload = null;
script.onerror = null;
setState('error');
}, 2000);
document.getElementsByTagName('head')[0].appendChild(script);
}
function setState (state) {
clearTimeout(idleTimer);
document.body.classList.remove('pending', 'success', 'error', 'idle');
document.body.classList.add(state);
doorButton.disabled = state !== 'idle';
statusDisplay.innerHTML = {
idle: '',
pending: '<div class="dots"><div></div><div></div><div></div></div>',
success: 'Woohoo!',
error: 'Couldn’t open. <p>Are you connected to the co.up network (“coup” or “coup slow”)?</p>'
}[state];
if (state === 'success' || state === 'error') {
idleTimer = setTimeout(function () {
setState('idle');
}, state === 'error' ? 3000 : 1600);
}
}
var hasTouch = 'ontouchstart' in window || navigator.msMaxTouchPoints > 0;
if (!hasTouch) {
document.body.classList.add('mouse');
}
doorButton.addEventListener('click', openDoor);