2020-06-02 23:56:59 +03:00
|
|
|
class Message {
|
|
|
|
constructor(model) {
|
|
|
|
this.author = model.author
|
|
|
|
this.body = model.body
|
|
|
|
this.image = "https://robohash.org/" + model.author
|
2020-06-03 03:59:40 +03:00
|
|
|
this.id = model.id
|
2020-06-02 23:56:59 +03:00
|
|
|
}
|
|
|
|
|
2020-06-14 08:15:58 +03: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 01:37:36 +03:00
|
|
|
}
|
2020-06-03 03:59:40 +03:00
|
|
|
|
2020-06-02 23:56:59 +03:00
|
|
|
toModel() {
|
|
|
|
return {
|
|
|
|
author: this.author(),
|
|
|
|
body: this.body(),
|
2020-06-03 03:59:40 +03:00
|
|
|
image: this.image(),
|
|
|
|
id: this.id
|
2020-06-02 23:56:59 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 06:15:31 +03: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 08:15:58 +03:00
|
|
|
this.maxMessageBuffer = 20;
|
2020-06-14 06:15:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
this.tagChatToggle = document.querySelector("#chat-toggle");
|
|
|
|
|
|
|
|
this.tagUserInfoDisplay = 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.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.formMessageInput = document.querySelector("#inputBody");
|
|
|
|
|
|
|
|
}
|
|
|
|
init() {
|
|
|
|
this.tagChatToggle.addEventListener("click", this.handleChatToggle);
|
|
|
|
this.tagUsernameDisplay.addEventListener("click", this.handleShowChangeNameForm);
|
|
|
|
|
|
|
|
this.btnUpdateUserName.addEventListener("click", this.handleUpdateUsername);
|
|
|
|
this.btnCancelUpdateUsername.addEventListener("click", this.handleHideChangeNameForm);
|
|
|
|
|
|
|
|
this.inputChangeUserName.addEventListener("keydown", this.handleUsernameKeydown);
|
2020-06-14 08:15:58 +03:00
|
|
|
this.formMessageInput.addEventListener("keydown", this.handleMessageInputKeydown);
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleChatToggle = () => {
|
|
|
|
if (this.chatDisplayed) {
|
|
|
|
this.tagAppContainer.className = "flex no-chat";
|
|
|
|
this.chatDisplayed = false;
|
|
|
|
} else {
|
|
|
|
this.tagAppContainer.className = "flex";
|
|
|
|
this.chatDisplayed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleShowChangeNameForm = () => {
|
|
|
|
this.tagUserInfoDisplay.style.display = "none";
|
|
|
|
this.tagUserInfoChanger.style.display = "flex";
|
|
|
|
}
|
|
|
|
handleHideChangeNameForm = () => {
|
|
|
|
this.tagUserInfoDisplay.style.display = "flex";
|
|
|
|
this.tagUserInfoChanger.style.display = "none";
|
|
|
|
}
|
|
|
|
handleUpdateUsername = () => {
|
|
|
|
var newValue = this.inputChangeUserName.value;
|
|
|
|
newValue = newValue.trim();
|
|
|
|
// do other string cleanup?
|
|
|
|
|
|
|
|
if (newValue) {
|
|
|
|
this.userName = newValue;
|
|
|
|
this.inputChangeUserName.value = newValue;
|
|
|
|
this.tagMessageAuthor.innerText = newValue;
|
|
|
|
this.tagUsernameDisplay.innerText = newValue;
|
|
|
|
this.imgUsernameAvatar.src = this.avatarSource + newValue;
|
|
|
|
}
|
|
|
|
this.handleHideChangeNameForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUsernameKeydown = event => {
|
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
this.handleUpdateUsername();
|
|
|
|
} else if (event.keyCode === 27) { // esc
|
|
|
|
this.handleHideChangeNameForm();
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 08:15:58 +03:00
|
|
|
|
2020-06-14 06:15:31 +03:00
|
|
|
handleMessageInputKeydown = event => {
|
2020-06-14 08:15:58 +03: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 06:15:31 +03:00
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
if (!this.prepNewLine) {
|
2020-06-14 08:15:58 +03:00
|
|
|
// submit()
|
|
|
|
event.preventDefault();
|
|
|
|
// clear out things.
|
|
|
|
this.formMessageInput.value = "";
|
|
|
|
this.tagMessageFormWarning.innerText = "";
|
2020-06-14 06:15:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.prepNewLine = false;
|
|
|
|
} else {
|
|
|
|
this.prepNewLine = false;
|
|
|
|
}
|
|
|
|
if (event.keyCode === 16 || event.keyCode === 17) { // ctrl, shift
|
|
|
|
this.prepNewLine = true;
|
|
|
|
}
|
|
|
|
|
2020-06-14 08:15:58 +03: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 06:15:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-02 23:56:59 +03:00
|
|
|
}
|