From: Christopher Allan Webber Date: Sat, 28 Jan 2017 18:18:23 +0000 (-0600) Subject: Some scrolling tweaks X-Git-Tag: fosdem-2017~67 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=614367c005e6ac1d547595c78fa194d44668a98d;p=mudsync.git Some scrolling tweaks --- diff --git a/data/web-static/js/mudsync.js b/data/web-static/js/mudsync.js index 631fa89..cf340d5 100644 --- a/data/web-static/js/mudsync.js +++ b/data/web-static/js/mudsync.js @@ -18,27 +18,40 @@ ;;; along with Mudsync. If not, see . */ -function displayMessage(data, self_sent) { - var new_entry = document.createElement("div"); +function scrollDown() { + var stream_metabox = document.getElementById("stream-metabox"); + stream_metabox.scrollTop = stream_metabox.scrollHeight; +} + +function withMaybeScroll(thunk) { var stream_metabox = document.getElementById("stream-metabox"); var should_scroll = false; if(stream_metabox.scrollTop === (stream_metabox.scrollHeight - stream_metabox.offsetHeight)) { should_scroll = true; } - if (self_sent) { - new_entry.setAttribute("class", "stream-entry self-sent"); - document.getElementById("main-input").value = ""; - } else { - new_entry.setAttribute("class", "stream-entry"); - } - new_entry.innerHTML = data; - document.getElementById("stream").appendChild(new_entry); + thunk(); if (should_scroll) { stream_metabox.scrollTop = stream_metabox.scrollHeight; } } + +function displayMessage(data, self_sent) { + var new_entry = document.createElement("div"); + withMaybeScroll( + function () { + if (self_sent) { + new_entry.setAttribute("class", "stream-entry self-sent"); + document.getElementById("main-input").value = ""; + } else { + new_entry.setAttribute("class", "stream-entry"); + } + new_entry.innerHTML = data; + document.getElementById("stream").appendChild(new_entry); + }); +} + function installWebsocket() { // TODO: Don't hardcode the websocket path; pull it from the DOM var address = "ws://".concat(window.location.hostname, ":", window.location.port); @@ -62,7 +75,10 @@ function installUIHooks(ws) { var keyCode = e.keyCode || e.which; if (keyCode == '13') { var input_val = input.value; - displayMessage("> ".concat(input_val), true); + withMaybeScroll( + function () { + displayMessage("> ".concat(input_val), true); + }); sendMessageToServer(ws, input_val); } } @@ -72,7 +88,7 @@ function sendMessageToServer(ws, data) { ws.send(data); } - window.onload = function () { installWebsocket(); + window.onresize = scrollDown; }