;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
*/
-function displayMessage(data) {
- var new_entry = document.createElement("div");
- var new_text = document.createTextNode(data);
+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)) {
+ // if within a reasonable threshold, we scroll
+ if((stream_metabox.scrollHeight - stream_metabox.offsetHeight)
+ - stream_metabox.scrollTop <= 50) {
should_scroll = true;
}
- document.getElementById("main-input").value = "";
- new_entry.setAttribute("class", "stream-entry");
- new_entry.appendChild(new_text);
- 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 ws = new WebSocket("ws://127.0.0.1:8888");
+ var address = "ws://".concat(window.location.hostname, ":", window.location.port);
+ var ws = new WebSocket(address);
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");
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
- sendMessageToServer(ws, input.value);
+ var input_val = input.value;
+ withMaybeScroll(
+ function () {
+ displayMessage("> ".concat(input_val), true);
+ });
+ sendMessageToServer(ws, input_val);
}
}
}
ws.send(data);
}
-
window.onload = function () {
installWebsocket();
+ window.onresize = scrollDown;
}