diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-27 19:42:08 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-27 19:42:08 +0100 |
commit | f44d36b05e43cabde31aeaba5d25fded140345a1 (patch) | |
tree | 1024a76cb1ae671c9445dcc379cb9eddd26922aa /html/whiteboard.js | |
parent | 789e5555ab4c44a1ae779eccf6ccf8340602cf22 (diff) |
Added diff.cpp
Diffstat (limited to 'html/whiteboard.js')
-rw-r--r-- | html/whiteboard.js | 65 |
1 files changed, 49 insertions, 16 deletions
diff --git a/html/whiteboard.js b/html/whiteboard.js index 9610468..83601b8 100644 --- a/html/whiteboard.js +++ b/html/whiteboard.js @@ -30,8 +30,13 @@ function on_getfile(data, rev, pos) if (board.value != data) { board.value = data; } - textAreaSetPos("board", pos); revision = rev; + textAreaSetPos("board", pos); +} + +function on_getpos(pos) +{ + textAreaSetPos("board", pos); } function on_newid(id) @@ -42,13 +47,17 @@ function on_newid(id) function on_qrcode(png) { - var blob = new Blob([png], {type: 'image/png'}); - var url = URL.createObjectURL(blob); + var url = "data:image/png;base64," + png; var img = document.getElementById("qrcode"); img.src = url; showQRWindow(); } +function on_version(version) +{ + document.getElementById("version").textContent = version; +} + function on_modify_ack(rev) { revision = rev; @@ -63,13 +72,17 @@ function on_message(e) { if (type == "getfile") { on_getfile(xmlDocument.getElementsByTagName("data")[0].textContent, parseInt(xmlDocument.getElementsByTagName("revision")[0].textContent), - parseInt(xmlDocument.getElementsByTagName("cursorpos")[0].textContent)); + parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent)); + } else if (type == "getpos") { + on_getpos(parseInt(xmlDocument.getElementsByTagName("pos")[0].textContent)); } else if (type == "modify") { on_modify_ack(parseInt(xmlDocument.getElementsByTagName("revision")[0].textContent)); } else if (type == "newid") { on_newid(xmlDocument.getElementsByTagName("id")[0].textContent); } else if (type == "qrcode") { on_qrcode(xmlDocument.getElementsByTagName("png")[0].textContent); + } else if (type == "version") { + on_version(xmlDocument.getElementsByTagName("version")[0].textContent); } else if (type == "error") { alert(xmlDocument.getElementsByTagName("message")[0].textContent); } else { @@ -88,12 +101,15 @@ function handleSelection() { } } -function init_board() { +function connect_websocket() { + document.getElementById("reconnect").style.display = 'none'; + document.getElementById("connecting").style.display = 'block'; var newlocation = location.origin + location.pathname; newlocation = newlocation.replace(/^http/, 'ws'); if (newlocation.slice(-1) != "/") newlocation += "/"; newlocation += "websocket"; + websocket = new WebSocket(newlocation); websocket.onmessage = function(e) { on_message(e); }; @@ -105,24 +121,37 @@ function init_board() { return; } - var board = document.getElementById("board"); - board.addEventListener("input", function() {on_input(); }); - // Need this workaround (different from direct on_selectionchange) for Chrome. - // Otherwise, callback will not be called on Chrome. - document.addEventListener("selectionchange", handleSelection); - //board.addEventListener("selectionchange", function() {on_selectionchange(); }); - + websocket.send("<request><command>getversion</command></request>"); websocket.send("<request><command>getfile</command><id>" + get_id() + "</id></request>"); + document.getElementById("connecting").style.display = 'none'; }; websocket.onclose = function(e) { alert("Server connection closed."); + document.getElementById("reconnect").style.display = 'inline'; }; websocket.onerror = function(e) { alert("Error: Server connection closed."); + document.getElementById("reconnect").style.display = 'inline'; }; +} + +function on_reconnect_click() { + connect_websocket(); +} + +function init_board() { + connect_websocket(); + + var board = document.getElementById("board"); + board.addEventListener("input", function() {on_input(); }); + // Need this workaround (different from direct on_selectionchange) for Chrome. + // Otherwise, callback will not be called on Chrome. + document.addEventListener("selectionchange", handleSelection); + //board.addEventListener("selectionchange", function() {on_selectionchange(); }); + document.getElementById("qrwindow").onclick = function() { hideQRWindow(); } @@ -173,6 +202,10 @@ function on_input() dataElement.appendChild(document.createTextNode(document.getElementById("board").value)); requestElement.appendChild(dataElement); + var posElement = xmlDocument.createElement("pos"); + posElement.appendChild(document.createTextNode(document.getElementById("board").selectionStart)); + requestElement.appendChild(posElement); + websocket.send(new XMLSerializer().serializeToString(xmlDocument)); } @@ -192,9 +225,9 @@ function on_selectionchange(pos) idElement.appendChild(document.createTextNode(get_id())); requestElement.appendChild(idElement); - var dataElement = xmlDocument.createElement("pos"); - dataElement.appendChild(document.createTextNode(pos)); - requestElement.appendChild(dataElement); + var posElement = xmlDocument.createElement("pos"); + posElement.appendChild(document.createTextNode(pos)); + requestElement.appendChild(posElement); websocket.send(new XMLSerializer().serializeToString(xmlDocument)); } @@ -209,7 +242,7 @@ function textAreaSetPos(id, pos) } // HTML button -function on_qrcode() +function on_qrcode_click() { var parser = new DOMParser(); var xmlDocument = parser.parseFromString("<request></request>", "text/xml"); |