cf340d5d6b1343ff597584d242b7ecfec47d0ee7
[mudsync.git] / data / web-static / js / mudsync.js
1 /*
2 ;;; Mudsync --- Live hackable MUD
3 ;;; Copyright © 2017 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;;
5 ;;; This file is part of Mudsync.
6 ;;;
7 ;;; Mudsync is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or
10 ;;; (at your option) any later version.
11 ;;;
12 ;;; Mudsync is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;;; General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with Mudsync.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 function scrollDown() {
22     var stream_metabox = document.getElementById("stream-metabox");
23     stream_metabox.scrollTop = stream_metabox.scrollHeight;
24 }
25
26 function withMaybeScroll(thunk) {
27     var stream_metabox = document.getElementById("stream-metabox");
28     var should_scroll = false;
29     if(stream_metabox.scrollTop === (stream_metabox.scrollHeight
30                                      - stream_metabox.offsetHeight)) {
31         should_scroll = true;
32     }
33     thunk();
34     if (should_scroll) {
35         stream_metabox.scrollTop = stream_metabox.scrollHeight;
36     }
37 }
38
39
40 function displayMessage(data, self_sent) {
41     var new_entry = document.createElement("div");
42     withMaybeScroll(
43         function () {
44             if (self_sent) {
45                 new_entry.setAttribute("class", "stream-entry self-sent");
46                 document.getElementById("main-input").value = "";
47             } else {
48                 new_entry.setAttribute("class", "stream-entry");
49             }
50             new_entry.innerHTML = data;
51             document.getElementById("stream").appendChild(new_entry);
52         });
53 }
54
55 function installWebsocket() {
56     // TODO: Don't hardcode the websocket path; pull it from the DOM
57     var address = "ws://".concat(window.location.hostname, ":", window.location.port);
58     var ws = new WebSocket(address);
59     ws.onmessage = function(evt) {
60         displayMessage(evt.data, false);
61     };
62     ws.onopen = function() {
63         console.log("connected");
64     };
65     ws.onclose = function () {
66         console.log("closed websocket");
67     };
68     installUIHooks(ws);
69 }
70
71 function installUIHooks(ws) {
72     var input = document.getElementById("main-input");
73     input.onkeypress = function(e) {
74         if (!e) e = window.event;
75         var keyCode = e.keyCode || e.which;
76         if (keyCode == '13') {
77             var input_val = input.value;
78             withMaybeScroll(
79                 function () {
80                     displayMessage("> ".concat(input_val), true);
81                 });
82             sendMessageToServer(ws, input_val);
83         }
84     }
85 }
86
87 function sendMessageToServer(ws, data) {
88     ws.send(data);
89 }
90
91 window.onload = function () {
92     installWebsocket();
93     window.onresize = scrollDown;
94 }