MovieNight/static/js/chat.js

178 lines
4.3 KiB
JavaScript
Raw Normal View History

/// <reference path="./both.js" />
function setPlaying(title, link) {
if (title !== "") {
$('#playing').text(title);
document.title = "Movie Night | " + title;
}
$('#playing').removeAttr('href');
if (link !== "") {
$('#playing').attr('href', link);
}
}
2019-03-12 04:15:42 +01:00
function startGo() {
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch("/static/main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
2019-03-12 04:15:42 +01:00
}).catch((err) => {
console.error(err);
});
}
function getWsUri() {
port = window.location.port;
if (port != "") {
port = ":" + port;
}
return "ws://" + window.location.hostname + port + "/ws";
}
let maxMessageCount = 0
2019-03-12 04:15:42 +01:00
function appendMessages(msg) {
let msgs = $("#messages").find('div');
// let's just say that if the max count is less than 1, then the count is infinite
// the server side should take care of chaking max count ranges
if (msgs.length > maxMessageCount) {
msgs.first().remove();
}
2019-03-12 04:15:42 +01:00
$("#messages").append(msg).scrollTop(9e6);
}
function purgeChat() {
$('#messages').empty()
}
inChat = false
2019-03-12 04:15:42 +01:00
function openChat() {
console.log("chat opening");
$("#joinbox").css("display", "none");
$("#chat").css("display", "grid");
$("#hidden").css("display", "")
$("#msg").val("");
$("#msg").focus();
inChat = true;
2019-03-12 04:15:42 +01:00
}
function closeChat() {
console.log("chat closing");
$("#joinbox").css("display", "");
$("#chat").css("display", "none");
$("#hidden").css("display", "none")
setNotifyBox("That name was already used!");
inChat = false;
2019-03-12 04:15:42 +01:00
}
function join() {
let name = $("#name").val();
if (name.length < 3 || name.length > 36) {
setNotifyBox("Please input a name between 3 and 36 characters");
2019-03-12 04:15:42 +01:00
return;
}
if (!sendMessage($("#name").val())) {
setNotifyBox("could not join");
return;
}
setNotifyBox();
2019-03-12 04:15:42 +01:00
openChat();
}
function websocketSend(data) {
ws.send(data);
2019-03-12 04:15:42 +01:00
}
function sendChat() {
sendMessage($("#msg").val());
$("#msg").val("");
}
function updateSuggestionCss(m) {
if ($("#suggestions").children().length > 0) {
$("#suggestions").css("bottom", $("#msg").outerHeight(true) - 1 + "px");
}
}
function setNotifyBox(msg = "Please hover to view options") {
$("#notifiyBox").html(msg);
}
// Button Wrapper Functions
function auth() {
let pass = prompt("pass please")
if (pass != "") {
sendMessage("/auth " + pass);
}
}
2019-03-16 22:59:10 +01:00
function help() {
sendMessage("/help")
}
// Get the websocket setup in a function so it can be recalled
function setupWebSocket() {
ws = new WebSocket(getWsUri());
ws.onmessage = (m) => recieveMessage(m.data);
ws.onopen = (e) => console.log("Websocket Open:", e);
ws.onclose = () => closeChat();
ws.onerror = (e) => console.log("Websocket Error:", e);
}
2019-03-16 21:01:54 +01:00
function setupEvents() {
$("#name").on({
keypress: (e) => {
if (e.originalEvent.keyCode == 13) {
$("#join").trigger("click");
}
}
});
$("#msg").on({
keypress: (e) => {
if (e.originalEvent.keyCode == 13 && !e.originalEvent.shiftKey) {
$("#send").trigger("click");
e.preventDefault();
}
},
keydown: (e) => {
if (processMessageKey(e)) {
e.preventDefault();
}
},
input: () => processMessage(),
});
$("#hiddenColorPicker").on({
change: () => sendMessage("/color " + $("#hiddenColorPicker").val()),
});
$("#send").on({
click: () => $("#msg").focus(),
});
var suggestionObserver = new MutationObserver(
(mutations) => mutations.forEach(updateSuggestionCss)
).observe($("#suggestions")[0], { childList: true });
}
window.addEventListener("onresize", updateSuggestionCss);
window.addEventListener("load", () => {
setNotifyBox();
setupWebSocket();
startGo();
setupEvents();
// Make sure name is focused on start
$("#name").focus();
});