|
|
|
@ -1,14 +1,13 @@
@@ -1,14 +1,13 @@
|
|
|
|
|
class Message { |
|
|
|
|
constructor(model) { |
|
|
|
|
this.author = model.author |
|
|
|
|
this.body = model.body |
|
|
|
|
this.image = "https://robohash.org/" + model.author |
|
|
|
|
this.id = model.id |
|
|
|
|
this.author = model.author; |
|
|
|
|
this.body = model.body; |
|
|
|
|
this.image = model.image || "https://robohash.org/" + model.author; |
|
|
|
|
this.id = model.id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
addNewlines(str) { |
|
|
|
|
return str.replace(/(?:\r\n|\r|\n)/g, '<br />'); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
formatText() { |
|
|
|
|
var linked = autoLink(this.body, { embed: true }); |
|
|
|
@ -21,7 +20,7 @@ class Message {
@@ -21,7 +20,7 @@ class Message {
|
|
|
|
|
body: this.body(), |
|
|
|
|
image: this.image(), |
|
|
|
|
id: this.id |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -38,54 +37,77 @@ class Messaging {
@@ -38,54 +37,77 @@ class Messaging {
|
|
|
|
|
this.maxMessageLength = 500; |
|
|
|
|
this.maxMessageBuffer = 20; |
|
|
|
|
|
|
|
|
|
this.keyUsername = "owncast_username"; |
|
|
|
|
this.keyChatDisplayed = "owncast_chat"; |
|
|
|
|
|
|
|
|
|
this.tagAppContainer = document.querySelector("#app-container"); |
|
|
|
|
|
|
|
|
|
this.tagChatToggle = document.querySelector("#chat-toggle"); |
|
|
|
|
|
|
|
|
|
this.tagUserInfoDisplay = document.querySelector("#user-info-display"); |
|
|
|
|
this.textUserInfoDisplay = document.querySelector("#user-info-display"); |
|
|
|
|
this.tagUserInfoChanger = document.querySelector("#user-info-change"); |
|
|
|
|
|
|
|
|
|
this.tagUsernameDisplay = document.querySelector("#username-display"); |
|
|
|
|
this.imgUsernameAvatar = document.querySelector("#username-avatar"); |
|
|
|
|
|
|
|
|
|
this.tagMessageAuthor = document.querySelector("#self-message-author"); |
|
|
|
|
this.inputMessageAuthor = document.querySelector("#self-message-author"); |
|
|
|
|
|
|
|
|
|
this.tagMessageFormWarning = document.querySelector("#message-form-warning"); |
|
|
|
|
|
|
|
|
|
this.tagAppContainer = document.querySelector("#app-container"); |
|
|
|
|
|
|
|
|
|
this.inputChangeUserName = document.querySelector("#username-change-input"); |
|
|
|
|
this.btnUpdateUserName = document.querySelector("#button-update-username"); |
|
|
|
|
this.btnCancelUpdateUsername = document.querySelector("#button-cancel-change"); |
|
|
|
|
this.btnSubmitMessage = document.querySelector("#button-submit-message"); |
|
|
|
|
|
|
|
|
|
this.formMessageInput = document.querySelector("#inputBody"); |
|
|
|
|
|
|
|
|
|
this.formMessageInput = document.querySelector("#message-body-form"); |
|
|
|
|
} |
|
|
|
|
init() { |
|
|
|
|
this.tagChatToggle.addEventListener("click", this.handleChatToggle); |
|
|
|
|
this.tagUsernameDisplay.addEventListener("click", this.handleShowChangeNameForm); |
|
|
|
|
this.textUserInfoDisplay.addEventListener("click", this.handleShowChangeNameForm); |
|
|
|
|
|
|
|
|
|
this.btnUpdateUserName.addEventListener("click", this.handleUpdateUsername); |
|
|
|
|
this.btnCancelUpdateUsername.addEventListener("click", this.handleHideChangeNameForm); |
|
|
|
|
|
|
|
|
|
this.inputChangeUserName.addEventListener("keydown", this.handleUsernameKeydown); |
|
|
|
|
this.formMessageInput.addEventListener("keydown", this.handleMessageInputKeydown); |
|
|
|
|
this.btnSubmitMessage.addEventListener("click", this.handleSubmitChatButton); |
|
|
|
|
this.initLocalStates(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
handleChatToggle = () => { |
|
|
|
|
this.chatDisplayed = !this.chatDisplayed; |
|
|
|
|
if (this.chatDisplayed) { |
|
|
|
|
this.tagAppContainer.className = "flex no-chat"; |
|
|
|
|
this.chatDisplayed = false; |
|
|
|
|
setLocalStorage(this.keyChatDisplayed, this.chatDisplayed); |
|
|
|
|
} else { |
|
|
|
|
this.tagAppContainer.className = "flex"; |
|
|
|
|
this.chatDisplayed = true; |
|
|
|
|
clearLocalStorage(this.keyChatDisplayed); |
|
|
|
|
} |
|
|
|
|
this.displayChat(); |
|
|
|
|
} |
|
|
|
|
handleShowChangeNameForm = () => { |
|
|
|
|
this.tagUserInfoDisplay.style.display = "none"; |
|
|
|
|
this.textUserInfoDisplay.style.display = "none"; |
|
|
|
|
this.tagUserInfoChanger.style.display = "flex"; |
|
|
|
|
} |
|
|
|
|
handleHideChangeNameForm = () => { |
|
|
|
|
this.tagUserInfoDisplay.style.display = "flex"; |
|
|
|
|
this.textUserInfoDisplay.style.display = "flex"; |
|
|
|
|
this.tagUserInfoChanger.style.display = "none"; |
|
|
|
|
} |
|
|
|
|
handleUpdateUsername = () => { |
|
|
|
@ -95,10 +117,8 @@ class Messaging {
@@ -95,10 +117,8 @@ class Messaging {
|
|
|
|
|
|
|
|
|
|
if (newValue) { |
|
|
|
|
this.userName = newValue; |
|
|
|
|
this.inputChangeUserName.value = newValue; |
|
|
|
|
this.tagMessageAuthor.innerText = newValue; |
|
|
|
|
this.tagUsernameDisplay.innerText = newValue; |
|
|
|
|
this.imgUsernameAvatar.src = this.avatarSource + newValue; |
|
|
|
|
this.updateUsernameFields(newValue); |
|
|
|
|
setLocalStorage(this.keyUsername, newValue); |
|
|
|
|
} |
|
|
|
|
this.handleHideChangeNameForm(); |
|
|
|
|
} |
|
|
|
@ -118,11 +138,9 @@ class Messaging {
@@ -118,11 +138,9 @@ class Messaging {
|
|
|
|
|
|
|
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
|
if (!this.prepNewLine) { |
|
|
|
|
// submit()
|
|
|
|
|
this.submitChat(value); |
|
|
|
|
event.preventDefault(); |
|
|
|
|
// clear out things.
|
|
|
|
|
this.formMessageInput.value = ""; |
|
|
|
|
this.tagMessageFormWarning.innerText = ""; |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
this.prepNewLine = false; |
|
|
|
@ -143,7 +161,30 @@ class Messaging {
@@ -143,7 +161,30 @@ class Messaging {
|
|
|
|
|
this.tagMessageFormWarning.innerText = ""; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleSubmitChatButton = event => { |
|
|
|
|
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); |
|
|
|
|
window.ws.send(messageJSON); |
|
|
|
|
|
|
|
|
|
// clear out things.
|
|
|
|
|
this.formMessageInput.value = ""; |
|
|
|
|
this.tagMessageFormWarning.innerText = ""; |
|
|
|
|
} |
|
|
|
|
} |