add connected/disconnected text
[mudsync.git] / data / web-static / js / mudsync.js
index cf340d5d6b1343ff597584d242b7ecfec47d0ee7..c5b7dca82d47cd928e351035814fda29b01327e1 100644 (file)
 */
 
 function scrollDown() {
-    var stream_metabox = document.getElementById("stream-metabox");
+    var stream_metabox = document.getElementById("stream-metabox");o
     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)) {
+    // if within a reasonable threshold, we scroll
+    if((stream_metabox.scrollHeight - stream_metabox.offsetHeight)
+       - stream_metabox.scrollTop <= 50) {
         should_scroll = true;
     }
     thunk();
@@ -52,6 +53,12 @@ function displayMessage(data, self_sent) {
         });
 }
 
+function setConnectedText(string, to_class) {
+    var stream_metabox = document.getElementById("connection-status");
+    stream_metabox.textContent = "[" + string + "]";
+    stream_metabox.setAttribute("class", to_class);
+}
+
 function installWebsocket() {
     // TODO: Don't hardcode the websocket path; pull it from the DOM
     var address = "ws://".concat(window.location.hostname, ":", window.location.port);
@@ -60,9 +67,16 @@ function installWebsocket() {
         displayMessage(evt.data, false);
     };
     ws.onopen = function() {
+        setConnectedText("connected", "connected");
         console.log("connected");
     };
     ws.onclose = function () {
+        setConnectedText("disconnected", "disconnected");
+        // kludge, we shouldn't be using self_sent like this because it
+        // wipes the input
+        displayMessage(
+            "* You have been disconnected.  Refresh to (hopefully) reconnect.",
+            true);
         console.log("closed websocket");
     };
     installUIHooks(ws);