1var DEFAULT_TITLE = "ppt-control"
2var preloaded = false;
3var preload = [];
4
5function imageRefresh(id) {
6 img = document.getElementById(id);
7 var d = new Date;
8 var http = img.src;
9 if (http.indexOf("?t=") != -1) { http = http.split("?t=")[0]; }
10 img.src = http + '?t=' + d.getTime();
11}
12
13function startWebsocket() {
14 ws = new WebSocket("ws://" + window.location.host + ":5678/");
15 ws.onclose = function(){
16 //websocket = null;
17 setTimeout(function(){startWebsocket()}, 10000);
18 }
19 return ws;
20}
21
22var websocket = startWebsocket();
23
24var prev = document.querySelector('#prev'),
25 next = document.querySelector('#next'),
26 first = document.querySelector('#first'),
27 last = document.querySelector('#last'),
28 black = document.querySelector('#black'),
29 white = document.querySelector('#white'),
30 slide_label = document.querySelector('#slide_label'),
31 current = document.querySelector('#current'),
32 total = document.querySelector('#total'),
33 users = document.querySelector('.users'),
34 current_img = document.querySelector('#current_img'),
35 next_img = document.querySelector('#next_img'),
36 current_div = document.querySelector('#current_div'),
37 next_div = document.querySelector('#next_div'),
38 controls_container = document.querySelector('#controls_container'),
39 controls_container_inner = document.querySelector('#controls_container_inner'),
40 show_current = document.querySelector('#show_current'),
41 show_next = document.querySelector('#show_next'),
42 shortcuts = document.querySelector('#shortcuts');
43
44prev.onclick = function (event) {
45 websocket.send(JSON.stringify({action: 'prev'}));
46}
47
48next.onclick = function (event) {
49 websocket.send(JSON.stringify({action: 'next'}));
50}
51
52first.onclick = function (event) {
53 websocket.send(JSON.stringify({action: 'first'}));
54}
55
56last.onclick = function (event) {
57 websocket.send(JSON.stringify({action: 'last'}));
58}
59
60black.onclick = function (event) {
61 websocket.send(JSON.stringify({action: 'black'}));
62}
63
64white.onclick = function (event) {
65 websocket.send(JSON.stringify({action: 'white'}));
66}
67
68current.onblur = function (event) {
69 websocket.send(JSON.stringify({action: 'goto', value: current.value}));
70}
71
72current.addEventListener('keyup',function(e){
73 if (e.which == 13) this.blur();
74});
75
76current_img.onclick = function (event) {
77 next.click()
78}
79
80next_img.onclick = function (event) {
81 next.click()
82}
83
84
85function sync_current() {
86 if (show_current.checked) {
87 current_div.style.display = "block";
88 slide_label.style.display = "none";
89 next_div.style.width = "25%";
90 } else {
91 current_div.style.display = "none";
92 slide_label.style.display = "inline";
93 next_div.style.width = "calc(100% - 20px)";
94 }
95 set_control_width();
96 saveSettings();
97}
98show_current.onclick = sync_current;
99
100function sync_next() {
101 if (show_next.checked) {
102 next_div.style.display = "block";
103 current_div.style.width = "70%";
104 } else {
105 next_div.style.display = "none";
106 current_div.style.width = "calc(100% - 20px)";
107 }
108 set_control_width();
109 saveSettings();
110}
111show_next.onclick = sync_next;
112
113function sync_shortcuts() {
114 saveSettings();
115}
116shortcuts.onclick = sync_shortcuts;
117
118function set_control_width() {
119 var width = window.innerWidth
120 || document.documentElement.clientWidth
121 || document.body.clientWidth;
122 if (show_current.checked && show_next.checked && width > 800) {
123 controls_container_inner.style.width = "70%"
124 } else {
125 controls_container_inner.style.width = "100%"
126 }
127}
128
129
130document.addEventListener('keydown', function (e) {
131 if (shortcuts.checked) {
132 switch (e.key) {
133 case "Left":
134 case "ArrowLeft":
135 case "Up":
136 case "ArrowUp":
137 case "k":
138 case "K":
139 prev.click();
140 break;
141 case " ":
142 case "Spacebar":
143 case "Enter":
144 case "Right":
145 case "ArrowRight":
146 case "Down":
147 case "ArrowDown":
148 case "j":
149 case "J":
150 next.click();
151 break;
152 case "b":
153 case "B":
154 black.click();
155 case "w":
156 case "W":
157 white.click();
158 default:
159 return
160 }
161 }
162});
163
164function sleep(ms) {
165 return new Promise(resolve => setTimeout(resolve, ms));
166}
167
168function disconnect() {
169 document.title = DEFAULT_TITLE;
170 current_img.src = "/black.jpg";
171 next_img.src = "/black.jpg";
172 users.textContent = "Connection to PowerPoint failed";
173}
174
175function update_current() {
176}
177
178function update_next() {
179}
180
181websocket.onmessage = function (event) {
182 data = JSON.parse(event.data);
183 switch (data.type) {
184 case 'state':
185 if (data.connected == "0" || data.connected == 0) {
186 console.log("Disconnected");
187 disconnect();
188 break;
189 }
190 var d = new Date;
191 if (show_current.checked) {
192 switch (data.visible) {
193 case 3:
194 current_img.src = "/black.jpg";
195 break;
196 case 4:
197 current_img.src = "/white.jpg";
198 break;
199 default:
200 current_img.src = "/cache/" + data.current + ".jpg?t=" + d.getTime();
201 //current_img.src = "/cache/" + data.current + ".jpg";
202 break;
203 }
204 }
205 if (data.current == data.total + 1) {
206 next_img.src = "/cache/" + (data.total + 1) + ".jpg?t=" + d.getTime();
207 //next_img.src = "/cache/" + (data.total + 1) + ".jpg";
208 } else {
209 next_img.src = "/cache/" + (data.current + 1) + ".jpg?t=" + d.getTime();
210 //next_img.src = "/cache/" + (data.current + 1) + ".jpg";
211 }
212
213 if (document.activeElement != current) {
214 current.value = data.current;
215 }
216 total.textContent = data.total;
217 document.title = data.name;
218 break;
219 default:
220 console.error(
221 "Unsupported event", data);
222 }
223 if (preloaded == false && ! isNaN(total.textContent)) {
224 image = document.getElementById("preload_img");
225 for (let i=1; i<=Number(total.textContent); i++) {
226 image.src = "/cache/" + i + ".jpg";
227 preload.push(image);
228 console.log("Preloaded " + total.textContent);
229 //sleep(0.5)
230 }
231 preloaded = true;
232 }
233
234};