2020-06-15 01:44:38 +02:00
|
|
|
const SocketMessageTypes = {
|
|
|
|
CHAT: "CHAT",
|
|
|
|
PING: "PING"
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:56:59 +02:00
|
|
|
class Message {
|
|
|
|
constructor(model) {
|
2020-06-14 10:10:26 +02:00
|
|
|
this.author = model.author;
|
|
|
|
this.body = model.body;
|
|
|
|
this.image = model.image || "https://robohash.org/" + model.author;
|
|
|
|
this.id = model.id;
|
2020-06-15 01:44:38 +02:00
|
|
|
this.type = model.type;
|
2020-06-02 22:56:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 07:15:58 +02:00
|
|
|
addNewlines(str) {
|
|
|
|
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
|
|
|
|
}
|
|
|
|
formatText() {
|
|
|
|
var linked = autoLink(this.body, { embed: true });
|
|
|
|
return this.addNewlines(linked);
|
2020-06-03 00:37:36 +02:00
|
|
|
}
|
2020-06-15 22:41:37 +02:00
|
|
|
userColor() {
|
2020-06-15 22:41:37 +02:00
|
|
|
return messageBubbleColorForString(this.author);
|
2020-06-15 22:41:37 +02:00
|
|
|
}
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// convert newlines to <br>s
|
|
|
|
|
|
|
|
class Messaging {
|
|
|
|
constructor() {
|
|
|
|
this.chatDisplayed = false;
|
|
|
|
this.username = "";
|
|
|
|
this.avatarSource = "https://robohash.org/";
|
|
|
|
|
|
|
|
this.messageCharCount = 0;
|
|
|
|
this.maxMessageLength = 500;
|
2020-06-14 07:15:58 +02:00
|
|
|
this.maxMessageBuffer = 20;
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 08:38:09 +02:00
|
|
|
this.keyUsername = "owncast_username";
|
|
|
|
this.keyChatDisplayed = "owncast_chat";
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 10:10:26 +02:00
|
|
|
this.tagAppContainer = document.querySelector("#app-container");
|
|
|
|
|
2020-06-14 05:15:31 +02:00
|
|
|
this.tagChatToggle = document.querySelector("#chat-toggle");
|
|
|
|
|
2020-06-14 10:10:26 +02:00
|
|
|
this.textUserInfoDisplay = document.querySelector("#user-info-display");
|
2020-06-14 05:15:31 +02:00
|
|
|
this.tagUserInfoChanger = document.querySelector("#user-info-change");
|
|
|
|
|
|
|
|
this.tagUsernameDisplay = document.querySelector("#username-display");
|
|
|
|
this.imgUsernameAvatar = document.querySelector("#username-avatar");
|
|
|
|
|
2020-06-14 08:38:09 +02:00
|
|
|
this.inputMessageAuthor = document.querySelector("#self-message-author");
|
2020-06-14 05:15:31 +02:00
|
|
|
|
|
|
|
this.tagMessageFormWarning = document.querySelector("#message-form-warning");
|
|
|
|
|
|
|
|
|
|
|
|
this.inputChangeUserName = document.querySelector("#username-change-input");
|
|
|
|
this.btnUpdateUserName = document.querySelector("#button-update-username");
|
|
|
|
this.btnCancelUpdateUsername = document.querySelector("#button-cancel-change");
|
2020-06-14 09:24:26 +02:00
|
|
|
this.btnSubmitMessage = document.querySelector("#button-submit-message");
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 09:24:26 +02:00
|
|
|
this.formMessageInput = document.querySelector("#message-body-form");
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
|
|
|
init() {
|
2020-06-14 22:04:04 +02:00
|
|
|
this.tagChatToggle.addEventListener("click", this.handleChatToggle.bind(this));
|
|
|
|
this.textUserInfoDisplay.addEventListener("click", this.handleShowChangeNameForm.bind(this));
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 22:04:04 +02:00
|
|
|
this.btnUpdateUserName.addEventListener("click", this.handleUpdateUsername.bind(this));
|
|
|
|
this.btnCancelUpdateUsername.addEventListener("click", this.handleHideChangeNameForm.bind(this));
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 22:04:04 +02:00
|
|
|
this.inputChangeUserName.addEventListener("keydown", this.handleUsernameKeydown.bind(this));
|
|
|
|
this.formMessageInput.addEventListener("keydown", this.handleMessageInputKeydown.bind(this));
|
|
|
|
this.btnSubmitMessage.addEventListener("click", this.handleSubmitChatButton.bind(this));
|
2020-06-16 02:40:12 +02:00
|
|
|
this.initLocalStates();
|
|
|
|
|
|
|
|
if (hasTouchScreen()) {
|
|
|
|
this.tagAppContainer.classList.add("touch-screen");
|
|
|
|
window.onorientationchange = this.handleOrientationChange.bind(this);
|
|
|
|
this.handleOrientationChange();
|
|
|
|
|
2020-06-15 09:15:23 +02:00
|
|
|
this.formMessageInput.addEventListener("focus", this.handleKeyboardAppear.bind(this));
|
|
|
|
this.formMessageInput.addEventListener("blur", this.handleKeyboardOut.bind(this));
|
|
|
|
}
|
2020-06-14 10:10:26 +02:00
|
|
|
|
2020-06-14 08:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initLocalStates() {
|
|
|
|
this.username = getLocalStorage(this.keyUsername) || "User" + (Math.floor(Math.random() * 42) + 1);
|
|
|
|
this.updateUsernameFields(this.username);
|
|
|
|
|
|
|
|
this.chatDisplayed = getLocalStorage(this.keyChatDisplayed) || false;
|
|
|
|
this.displayChat();
|
|
|
|
}
|
|
|
|
updateUsernameFields(username) {
|
|
|
|
this.tagUsernameDisplay.innerText = username;
|
|
|
|
this.inputChangeUserName.value = username;
|
|
|
|
this.inputMessageAuthor.value = username;
|
|
|
|
this.imgUsernameAvatar.src = this.avatarSource + username;
|
|
|
|
}
|
|
|
|
displayChat() {
|
|
|
|
this.tagAppContainer.className = this.chatDisplayed ? "flex" : "flex no-chat";
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 02:40:12 +02:00
|
|
|
handleOrientationChange() {
|
|
|
|
console.log("====orientation change 123", event, window.screen.orientation, window.orientation, window.matchMedia("(orientation: landscape)"))
|
|
|
|
mobileVHhack();
|
|
|
|
// if small landscape, hide chat
|
|
|
|
// var mql = window.matchMedia("(orientation: landscape)"); // what it _was_
|
|
|
|
|
|
|
|
var isPortrait = Math.abs(window.orientation % 180) === 0;
|
|
|
|
|
|
|
|
if(!isPortrait) {
|
|
|
|
if (document.body.clientWidth < 1024) {
|
|
|
|
this.tagAppContainer.classList.add("no-chat");
|
|
|
|
this.tagAppContainer.classList.add("landscape");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.chatDisplayed) this.tagAppContainer.classList.remove("no-chat");
|
|
|
|
this.tagAppContainer.classList.remove("landscape");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:15:23 +02:00
|
|
|
handleKeyboardAppear() {
|
2020-06-16 01:23:39 +02:00
|
|
|
setTimeout(() => {this.tagAppContainer.classList.add("message-input-focus");}, 50);
|
2020-06-16 00:55:57 +02:00
|
|
|
mobileVHhack();
|
2020-06-15 09:15:23 +02:00
|
|
|
}
|
|
|
|
handleKeyboardOut() {
|
2020-06-16 01:23:39 +02:00
|
|
|
setTimeout(() => {this.tagAppContainer.classList.remove("message-input-focus");}, 50);
|
2020-06-16 00:55:57 +02:00
|
|
|
mobileVHhack();
|
2020-06-15 09:15:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 22:04:04 +02:00
|
|
|
handleChatToggle() {
|
2020-06-14 08:38:09 +02:00
|
|
|
this.chatDisplayed = !this.chatDisplayed;
|
2020-06-14 10:10:26 +02:00
|
|
|
if (this.chatDisplayed) {
|
|
|
|
setLocalStorage(this.keyChatDisplayed, this.chatDisplayed);
|
|
|
|
} else {
|
|
|
|
clearLocalStorage(this.keyChatDisplayed);
|
|
|
|
}
|
2020-06-14 08:38:09 +02:00
|
|
|
this.displayChat();
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
2020-06-14 22:04:04 +02:00
|
|
|
handleShowChangeNameForm() {
|
2020-06-14 10:10:26 +02:00
|
|
|
this.textUserInfoDisplay.style.display = "none";
|
2020-06-14 05:15:31 +02:00
|
|
|
this.tagUserInfoChanger.style.display = "flex";
|
|
|
|
}
|
2020-06-14 22:04:04 +02:00
|
|
|
handleHideChangeNameForm() {
|
2020-06-14 10:10:26 +02:00
|
|
|
this.textUserInfoDisplay.style.display = "flex";
|
2020-06-14 05:15:31 +02:00
|
|
|
this.tagUserInfoChanger.style.display = "none";
|
|
|
|
}
|
2020-06-14 22:04:04 +02:00
|
|
|
handleUpdateUsername() {
|
2020-06-14 05:15:31 +02:00
|
|
|
var newValue = this.inputChangeUserName.value;
|
|
|
|
newValue = newValue.trim();
|
|
|
|
// do other string cleanup?
|
|
|
|
|
|
|
|
if (newValue) {
|
2020-06-15 03:28:21 +02:00
|
|
|
this.username = newValue;
|
2020-06-14 08:38:09 +02:00
|
|
|
this.updateUsernameFields(newValue);
|
|
|
|
setLocalStorage(this.keyUsername, newValue);
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
|
|
|
this.handleHideChangeNameForm();
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:04:04 +02:00
|
|
|
handleUsernameKeydown(event) {
|
2020-06-14 05:15:31 +02:00
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
this.handleUpdateUsername();
|
|
|
|
} else if (event.keyCode === 27) { // esc
|
|
|
|
this.handleHideChangeNameForm();
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 07:15:58 +02:00
|
|
|
|
2020-06-14 22:04:04 +02:00
|
|
|
handleMessageInputKeydown(event) {
|
2020-06-14 07:15:58 +02:00
|
|
|
var okCodes = [37,38,39,40,16,91,18,46,8];
|
|
|
|
var value = this.formMessageInput.value.trim();
|
|
|
|
var numCharsLeft = this.maxMessageLength - value.length;
|
|
|
|
|
2020-06-14 05:15:31 +02:00
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
if (!this.prepNewLine) {
|
2020-06-14 09:24:26 +02:00
|
|
|
this.submitChat(value);
|
2020-06-14 07:15:58 +02:00
|
|
|
event.preventDefault();
|
2020-06-14 09:24:26 +02:00
|
|
|
|
2020-06-14 05:15:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prepNewLine = false;
|
|
|
|
} else {
|
|
|
|
this.prepNewLine = false;
|
|
|
|
}
|
|
|
|
if (event.keyCode === 16 || event.keyCode === 17) { // ctrl, shift
|
|
|
|
this.prepNewLine = true;
|
|
|
|
}
|
|
|
|
|
2020-06-14 07:15:58 +02:00
|
|
|
if (numCharsLeft <= this.maxMessageBuffer) {
|
|
|
|
this.tagMessageFormWarning.innerText = numCharsLeft + " chars left";
|
|
|
|
if (numCharsLeft <= 0 && !okCodes.includes(event.keyCode)) {
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.tagMessageFormWarning.innerText = "";
|
|
|
|
}
|
2020-06-14 05:15:31 +02:00
|
|
|
}
|
2020-06-14 22:04:04 +02:00
|
|
|
handleSubmitChatButton(event) {
|
2020-06-14 09:24:26 +02:00
|
|
|
var value = this.formMessageInput.value.trim();
|
|
|
|
if (value) {
|
|
|
|
this.submitChat(value);
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
submitChat(content) {
|
|
|
|
if (!content) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var message = new Message({
|
|
|
|
body: content,
|
|
|
|
author: this.username,
|
|
|
|
id: uuidv4(),
|
|
|
|
});
|
|
|
|
const messageJSON = JSON.stringify(message);
|
2020-06-16 00:45:55 +02:00
|
|
|
if (window && window.ws) {
|
|
|
|
window.ws.send(messageJSON);
|
|
|
|
}
|
2020-06-14 05:15:31 +02:00
|
|
|
|
2020-06-14 09:24:26 +02:00
|
|
|
// clear out things.
|
|
|
|
this.formMessageInput.value = "";
|
|
|
|
this.tagMessageFormWarning.innerText = "";
|
|
|
|
}
|
2020-06-02 22:56:59 +02:00
|
|
|
}
|