summaryrefslogtreecommitdiffhomepage
path: root/html/whiteboard.js
blob: c6fb6570cb155432d14606202f1728b0ce27c28b (plain)
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
// started on main page load
function init() {
	init_board();
}

class AdjustingTimer {
	constructor() {
		this.update_counter = 0; // counting seconds since last counter reset
	}

	fast_mode() {
		if (this.update_counter < 5*60)
			return true;
		return false;
	}

	// private method
	// returns current interval in ms
	current_update_interval()
	{
	 if (this.fast_mode())
	  return 2000; // 2s
	 else
	  return 5 * 60000; // 5min
	};

	// private
	count() {
		this.update_counter += this.current_update_interval() / 1000;
	};

	// private
	on_timeout() {
		this.m_fn();
		this.count();
		var _this = this;
		this.update_timer = setTimeout(function(){_this.on_timeout();}, this.current_update_interval());
	};

	// to be called once on startup
	start(fn) {
		this.m_fn = fn;
		var _this = this;
		this.update_timer = setTimeout(function(){_this.on_timeout();}, this.current_update_interval());
	};

	// to be called on activity:
	// * changes from remote
	// * changes by ourselves
	// * local activity, e.g. mouse move, or key presses
	reset() {
		if (!this.fast_mode()) {
			this.update_counter = 0;
			clearTimeout(this.update_timer);
			var _this = this;
			this.update_timer = setTimeout(function(){_this.on_timeout();}, this.current_update_interval());
		} else {
			this.update_counter = 0;
		}
	};
}

var timer = new AdjustingTimer();

function showQRWindow()
{
 document.getElementById("qrwindow").style.display = 'block';
}

function hideQRWindow()
{
 document.getElementById("qrwindow").style.display = 'none';
}

function init_board() {
	var xhr = new XMLHttpRequest();
	
        const searchParams = (new URL(document.location)).searchParams;
        if (!searchParams.has('id')) {
                redirect_to_new_page();
                return;
        }

	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

                                var file = new Blob([this.response]);
				reader = new FileReader();
				reader.onload = function() {
					var board = document.getElementById("board");
					board.innerHTML = reader.result;

					// Initialization done. Now we can start modifying.
					board.addEventListener("input", function() {on_modify(); });

					// Initialization done. Now we can start modifying.
					document.addEventListener("mousemove", function() {timer.reset(); });
					
					timer.start(checkupdate);
				}
				
			        reader.readAsBinaryString(file);

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("getfile"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
        
        document.getElementById("qrwindow").onclick = function() {
         hideQRWindow();
        }

        document.onkeydown = function(evt) {
         if (evt.key == "Escape") {
          hideQRWindow();
         }
        }
}

function get_id()
{
        const searchParams = (new URL(document.location)).searchParams;
        return searchParams.get('id');
}

function on_new_page()
{
        redirect_to_new_page();
}

function redirect_to_new_page()
{
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

                                var id = this.responseText;
                                //alert("location=" + document.location.href);
                                var new_location = document.location.href;
                                var pos = new_location.search("\\?");
                                if (pos >= 0)
                                        new_location = new_location.substring(0, pos);
                                new_location += '?id=' + id;

                                window.location.href = new_location;
				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("newid"));
	requestElement.appendChild(commandElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

// local change done
function on_modify()
{
	timer.reset();

	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("modify"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	var dataElement = xmlDocument.createElement("data");
	dataElement.appendChild(document.createTextNode(document.getElementById("board").value));
	requestElement.appendChild(dataElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

// checksum of string
function checksum32(s) {
	var result = 0;
	for (var i = 0; i < s.length; i++) {
		result = ((((result >>> 1) | ((result & 1) << 31)) | 0) ^ (s.charCodeAt(i) & 0xFF)) | 0;
	}
	return (result & 0x7FFFFFFF) | 0;
}

// gets called by regular polling
function checkupdate() {
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				// no change if response is text/plain
				if (this.getResponseHeader("Content-Type") == "application/octet-stream") {
					timer.reset();
					var file = new Blob([this.response]);
					reader = new FileReader();
					reader.onload = function() {
						var board = document.getElementById("board");
						board.value = reader.result;
					}
					
					reader.readAsBinaryString(file);
				}

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("checkupdate"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("id");
	idElement.appendChild(document.createTextNode(get_id()));
	requestElement.appendChild(idElement);

	var checksumElement = xmlDocument.createElement("checksum");
	checksumElement.appendChild(document.createTextNode(checksum32(document.getElementById("board").value)));
	requestElement.appendChild(checksumElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);

	//set_status("Please wait while server prepares " + filename + " ...");
}

function on_qrcode()
{
	var xhr = new XMLHttpRequest();
	
	// run on data received back
	xhr.onreadystatechange = function() {
				if (this.readyState == 3) {
					//set_status("Please wait while downloading " + filename + " ...");
					return;
				}
                                if (this.readyState != 4) {
					return;
				}
				if (this.status != 200) {
					//set_status("Server Error while retrieving " + filename + ", status: " + this.status + " " + this.statusText);
                                        return;
                                }

				if (this.getResponseHeader("Content-Type") == "image/png") {
					var blob = new Blob([this.response], {type: 'image/png'});
                                        var url = URL.createObjectURL(blob);
					var img = document.getElementById("qrcode");
                                        img.src = url;
                                        showQRWindow();
				}

				//set_status(""); // OK
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
	
	var requestElement = xmlDocument.getElementsByTagName("request")[0];

	var commandElement = xmlDocument.createElement("command");
	commandElement.appendChild(document.createTextNode("qrcode"));
	requestElement.appendChild(commandElement);

	var idElement = xmlDocument.createElement("url");
	idElement.appendChild(document.createTextNode(document.location));
	requestElement.appendChild(idElement);

	xhr.open("POST", "whiteboard.fcgi", true);
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.responseType = 'blob';
	xhr.send(xmlDocument);
}