-
Notifications
You must be signed in to change notification settings - Fork 1
/
site.js
156 lines (135 loc) · 4.19 KB
/
site.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
// load client.js first!
const DEBUG = false
;(function () {
const defaultHost = 'localhost'
const defaultPort = '50001'
const defaultPass = 'control'
const elem = id => document.getElementById(id)
const statusElem = elem('status')
const messageElem = elem('currentMessage')
function setStatus (status) {
switch (status) {
case 'disconnected':
statusElem.className = 'disconnected'
statusElem.innerText = 'Disconnected'
break
case 'badAuth':
statusElem.className = 'disconnected'
statusElem.innerText = 'Bad password... retrying in 3 seconds'
break
case 'badPerm':
statusElem.className = 'disconnected'
statusElem.innerText = 'Controller access not enabled... Please configure ProPresenter'
break
case 'connecting':
statusElem.className = 'connecting'
statusElem.innerText = 'Connecting'
break
case 'connected':
statusElem.className = 'connected'
statusElem.innerText = 'Connected'
break
}
}
let reconnectTimer
function connectFail () {
clearTimeout(reconnectTimer)
reconnectTimer = setTimeout(function () {
setStatus('connecting')
Client.connect()
}, 3000)
}
const callbacks = {
connectFail () {
setStatus('disconnected')
connectFail()
},
authFail () {
setStatus('badAuth')
connectFail()
},
permFail () {
setStatus('badPerm')
connectFail()
},
authSuccess () {
clearTimeout(reconnectTimer)
setStatus('connected')
}
}
let Client
function init () {
clearTimeout(reconnectTimer)
Client && Client.close()
Client = new PP_SDM_CLASS({
...callbacks,
HOST: localStorage.getItem('host'),
PORT: localStorage.getItem('port'),
PASS: localStorage.getItem('pass')
})
setStatus('connecting')
Client.connect()
}
elem('message').addEventListener('keyup', event => {
event.preventDefault()
switch (event.keyCode) {
case 13:
elem('message').value.trim() && elem('send').click()
// es-lint: allow fall through
case 27:
elem('message').value = ''
break
}
})
let shakeLoop
function doShake (duration = 200) {
clearTimeout(shakeLoop)
elem('send').className = 'shake'
shakeLoop = setTimeout(() => (elem('send').className = ''), duration)
}
elem('send').addEventListener('click', () => {
const message = elem('message').value.trim()
if (!message) return false
Client.send(message)
doShake()
messageElem.innerText = message
messageElem.className = 'blink'
})
elem('clear').addEventListener('click', () => {
Client.hide()
messageElem.innerText = ' '
messageElem.className = ''
})
elem('pref_open').addEventListener('click', () => {
elem('preferences').showModal()
})
elem('preferences').showModal = function (allowClose = true, defaultFallback = false) {
elem('pref_addr').value = localStorage.getItem('host') || (defaultFallback ? defaultHost : '')
elem('pref_port').value = localStorage.getItem('port') || (defaultFallback ? defaultPort : '')
elem('pref_pass').value = localStorage.getItem('pass') || (defaultFallback ? defaultPass : '')
allowClose && document.querySelector('.dialogBackground')
.addEventListener('click', elem('preferences').closeModal)
this.setAttribute('open', true)
}
elem('preferences').closeModal = function () {
document.querySelector('.dialogBackground').removeEventListener('click', elem('preferences').closeModal)
elem('preferences').removeAttribute('open')
elem('pref_addr').value = ''
elem('pref_port').value = ''
elem('pref_pass').value = ''
}
elem('pref_save').addEventListener('click', function () {
localStorage.setItem('host', elem('pref_addr').value)
localStorage.setItem('port', elem('pref_port').value)
localStorage.setItem('pass', elem('pref_pass').value)
elem('preferences').closeModal()
Client && Client.close()
setStatus('disconnecting')
init()
})
if (localStorage.getItem('host') && localStorage.getItem('port')) {
init()
} else {
elem('preferences').showModal(false, true)
}
})()