X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;ds=sidebyside;f=data%2Fweb-static%2Fjs%2Fmudsync.js;h=90f3daa4ef9af9d995cf1c0649a6c19ab1ff0c71;hb=829df0120213f2bf7420b8ae153e761feb675106;hp=f66a9477ca53597bf601502c17ce72b82b159149;hpb=2a761f453a55ff6d896e17efc672300dc1b49c8e;p=mudsync.git diff --git a/data/web-static/js/mudsync.js b/data/web-static/js/mudsync.js index f66a947..90f3daa 100644 --- a/data/web-static/js/mudsync.js +++ b/data/web-static/js/mudsync.js @@ -18,29 +18,61 @@ ;;; along with Mudsync. If not, see . */ -function displayMessage(data) { +function displayMessage(data, self_sent) { var new_entry = document.createElement("div"); - new_entry.setAttribute("class", "stream-entry"); var new_text = document.createTextNode(data); + var stream_metabox = document.getElementById("stream-metabox"); + var should_scroll = false; + if(stream_metabox.scrollTop === (stream_metabox.scrollHeight + - stream_metabox.offsetHeight)) { + should_scroll = true; + } + document.getElementById("main-input").value = ""; + if (self_sent) { + new_entry.setAttribute("class", "stream-entry self-sent"); + } else { + new_entry.setAttribute("class", "stream-entry"); + } new_entry.appendChild(new_text); document.getElementById("stream").appendChild(new_entry); + if (should_scroll) { + stream_metabox.scrollTop = stream_metabox.scrollHeight; + } } function installWebsocket() { // TODO: Don't hardcode the websocket path; pull it from the DOM var ws = new WebSocket("ws://127.0.0.1:8888"); ws.onmessage = function(evt) { - displayMessage(evt.data); + displayMessage(evt.data, false); }; ws.onopen = function() { console.log("connected"); - ws.send("Hello, there!"); }; ws.onclose = function () { console.log("closed websocket"); }; + installUIHooks(ws); } +function installUIHooks(ws) { + var input = document.getElementById("main-input"); + input.onkeypress = function(e) { + if (!e) e = window.event; + var keyCode = e.keyCode || e.which; + if (keyCode == '13') { + var input_val = input.value; + displayMessage("> ".concat(input_val), true); + sendMessageToServer(ws, input_val); + } + } +} + +function sendMessageToServer(ws, data) { + ws.send(data); +} + + window.onload = function () { installWebsocket(); }