2020-06-15 02:44:38 +03:00
|
|
|
const SocketMessageTypes = {
|
2020-07-05 08:34:00 +03:00
|
|
|
CHAT: 'CHAT',
|
|
|
|
PING: 'PING'
|
2020-06-15 02:44:38 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 23:56:59 +03:00
|
|
|
class Message {
|
|
|
|
constructor(model) {
|
2020-06-14 11:10:26 +03:00
|
|
|
this.author = model.author;
|
|
|
|
this.body = model.body;
|
2020-07-05 08:34:00 +03:00
|
|
|
this.image = model.image || generateAvatar(model.author);
|
2020-06-14 11:10:26 +03:00
|
|
|
this.id = model.id;
|
2020-06-15 02:44:38 +03:00
|
|
|
this.type = model.type;
|
2020-06-02 23:56:59 +03:00
|
|
|
}
|
|
|
|
|
2020-06-14 08:15:58 +03:00
|
|
|
formatText() {
|
2020-06-21 09:41:13 +03:00
|
|
|
var markdownToHTML = new showdown.Converter({
|
|
|
|
emoji: true,
|
|
|
|
openLinksInNewWindow: true,
|
|
|
|
tables: false,
|
|
|
|
strikethrough: false,
|
|
|
|
simplifiedAutoLink: false,
|
|
|
|
}).makeHtml(this.body);
|
2020-06-19 23:40:36 +03:00
|
|
|
var linked = autoLink(markdownToHTML, { embed: true });
|
2020-06-18 10:06:10 +03:00
|
|
|
return addNewlines(linked);
|
2020-06-03 01:37:36 +03:00
|
|
|
}
|
2020-06-15 23:41:37 +03:00
|
|
|
userColor() {
|
2020-06-15 23:41:37 +03:00
|
|
|
return messageBubbleColorForString(this.author);
|
2020-06-15 23:41:37 +03:00
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Messaging {
|
|
|
|
constructor() {
|
|
|
|
this.chatDisplayed = false;
|
2020-07-05 08:34:00 +03:00
|
|
|
this.username = '';
|
2020-06-14 06:15:31 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.keyUsername = 'owncast_username';
|
|
|
|
this.keyUserAvatar = 'owncast_avatar';
|
|
|
|
this.keyChatDisplayed = 'owncast_chat';
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer = document.querySelector('#app-container');
|
|
|
|
this.tagChatToggle = document.querySelector('#chat-toggle');
|
|
|
|
this.tagUserInfoChanger = document.querySelector('#user-info-change');
|
|
|
|
this.tagUsernameDisplay = document.querySelector('#username-display');
|
|
|
|
this.tagMessageFormWarning = document.querySelector('#message-form-warning');
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.inputMessageAuthor = document.querySelector('#self-message-author');
|
|
|
|
this.inputChangeUserName = document.querySelector('#username-change-input');
|
2020-06-18 10:06:10 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.btnUpdateUserName = document.querySelector('#button-update-username');
|
|
|
|
this.btnCancelUpdateUsername = document.querySelector('#button-cancel-change');
|
|
|
|
this.btnSubmitMessage = document.querySelector('#button-submit-message');
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.formMessageInput = document.querySelector('#message-body-form');
|
2020-06-18 10:06:10 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.imgUsernameAvatar = document.querySelector('#username-avatar');
|
|
|
|
this.textUserInfoDisplay = document.querySelector('#user-info-display');
|
2020-06-18 10:06:10 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.scrollableMessagesContainer = document.querySelector('#messages-container');
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
|
|
|
init() {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagChatToggle.addEventListener('click', this.handleChatToggle.bind(this));
|
|
|
|
this.textUserInfoDisplay.addEventListener('click', this.handleShowChangeNameForm.bind(this));
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-07-05 08:34:00 +03:00
|
|
|
this.btnUpdateUserName.addEventListener('click', this.handleUpdateUsername.bind(this));
|
|
|
|
this.btnCancelUpdateUsername.addEventListener('click', this.handleHideChangeNameForm.bind(this));
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-07-05 08:34:00 +03: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 03:40:12 +03:00
|
|
|
this.initLocalStates();
|
|
|
|
|
|
|
|
if (hasTouchScreen()) {
|
2020-06-18 10:06:10 +03:00
|
|
|
this.scrollableMessagesContainer = document.body;
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer.classList.add('touch-screen');
|
|
|
|
window.app.layout = 'touch';
|
2020-06-16 03:40:12 +03:00
|
|
|
window.onorientationchange = this.handleOrientationChange.bind(this);
|
|
|
|
this.handleOrientationChange();
|
2020-06-16 12:09:29 +03:00
|
|
|
} else {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer.classList.add('desktop');
|
|
|
|
window.app.layout = 'desktop';
|
2020-06-29 03:15:53 +03:00
|
|
|
|
2020-06-15 10:15:23 +03:00
|
|
|
}
|
2020-06-14 09:38:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
initLocalStates() {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.username = getLocalStorage(this.keyUsername) || generateUsername();
|
|
|
|
console.log("=== initt state", getLocalStorage(this.keyUserAvatar))
|
|
|
|
this.imgUsernameAvatar.src = getLocalStorage(this.keyUserAvatar) || generateAvatar(this.username);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("===",this.imgUsernameAvatar.src)
|
|
|
|
|
2020-06-14 09:38:09 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
displayChat() {
|
2020-06-16 09:32:26 +03:00
|
|
|
if (this.chatDisplayed) {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer.classList.add('chat');
|
|
|
|
this.tagAppContainer.classList.remove('no-chat');
|
2020-06-18 10:06:10 +03:00
|
|
|
jumpToBottom(this.scrollableMessagesContainer);
|
2020-06-16 09:32:26 +03:00
|
|
|
} else {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer.classList.add('no-chat');
|
|
|
|
this.tagAppContainer.classList.remove('chat');
|
2020-06-16 09:32:26 +03:00
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 03:40:12 +03:00
|
|
|
handleOrientationChange() {
|
|
|
|
var isPortrait = Math.abs(window.orientation % 180) === 0;
|
|
|
|
|
|
|
|
if(!isPortrait) {
|
|
|
|
if (document.body.clientWidth < 1024) {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagAppContainer.classList.add('no-chat');
|
|
|
|
this.tagAppContainer.classList.add('landscape');
|
2020-06-16 03:40:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-05 08:34:00 +03:00
|
|
|
if (this.chatDisplayed) this.tagAppContainer.classList.remove('no-chat');
|
|
|
|
this.tagAppContainer.classList.remove('landscape');
|
2020-06-16 03:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:04:04 +03:00
|
|
|
handleChatToggle() {
|
2020-06-14 09:38:09 +03:00
|
|
|
this.chatDisplayed = !this.chatDisplayed;
|
2020-06-14 11:10:26 +03:00
|
|
|
if (this.chatDisplayed) {
|
|
|
|
setLocalStorage(this.keyChatDisplayed, this.chatDisplayed);
|
|
|
|
} else {
|
|
|
|
clearLocalStorage(this.keyChatDisplayed);
|
|
|
|
}
|
2020-06-14 09:38:09 +03:00
|
|
|
this.displayChat();
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
2020-06-14 23:04:04 +03:00
|
|
|
handleShowChangeNameForm() {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.textUserInfoDisplay.style.display = 'none';
|
|
|
|
this.tagUserInfoChanger.style.display = 'flex';
|
2020-06-30 12:36:10 +03:00
|
|
|
if (document.body.clientWidth < 640) {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagChatToggle.style.display = 'none';
|
2020-06-30 12:36:10 +03:00
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
2020-06-14 23:04:04 +03:00
|
|
|
handleHideChangeNameForm() {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.textUserInfoDisplay.style.display = 'flex';
|
|
|
|
this.tagUserInfoChanger.style.display = 'none';
|
2020-06-30 12:36:10 +03:00
|
|
|
if (document.body.clientWidth < 640) {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagChatToggle.style.display = 'inline-block';
|
2020-06-30 12:36:10 +03:00
|
|
|
|
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
2020-06-14 23:04:04 +03:00
|
|
|
handleUpdateUsername() {
|
2020-06-14 06:15:31 +03:00
|
|
|
var newValue = this.inputChangeUserName.value;
|
|
|
|
newValue = newValue.trim();
|
|
|
|
// do other string cleanup?
|
|
|
|
|
|
|
|
if (newValue) {
|
2020-06-15 04:28:21 +03:00
|
|
|
this.username = newValue;
|
2020-06-14 09:38:09 +03:00
|
|
|
this.updateUsernameFields(newValue);
|
2020-07-05 08:34:00 +03:00
|
|
|
this.imgUsernameAvatar.src = generateAvatar(newValue);
|
2020-06-14 09:38:09 +03:00
|
|
|
setLocalStorage(this.keyUsername, newValue);
|
2020-07-05 08:34:00 +03:00
|
|
|
setLocalStorage(this.keyUserAvatar, this.imgUsernameAvatar.src);
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
|
|
|
this.handleHideChangeNameForm();
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:04:04 +03:00
|
|
|
handleUsernameKeydown(event) {
|
2020-06-14 06:15:31 +03:00
|
|
|
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 23:04:04 +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 10:24:26 +03:00
|
|
|
this.submitChat(value);
|
2020-06-14 08:15:58 +03:00
|
|
|
event.preventDefault();
|
2020-06-14 10:24:26 +03:00
|
|
|
|
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) {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagMessageFormWarning.innerText = `${numCharsLeft} chars left`;
|
2020-06-14 08:15:58 +03:00
|
|
|
if (numCharsLeft <= 0 && !okCodes.includes(event.keyCode)) {
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-05 08:34:00 +03:00
|
|
|
this.tagMessageFormWarning.innerText = '';
|
2020-06-14 08:15:58 +03:00
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
}
|
2020-06-14 23:04:04 +03:00
|
|
|
handleSubmitChatButton(event) {
|
2020-06-14 10:24:26 +03: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,
|
2020-07-05 08:34:00 +03:00
|
|
|
image: this.imgUsernameAvatar.src,
|
2020-06-14 10:24:26 +03:00
|
|
|
});
|
|
|
|
const messageJSON = JSON.stringify(message);
|
2020-06-16 01:45:55 +03:00
|
|
|
if (window && window.ws) {
|
|
|
|
window.ws.send(messageJSON);
|
|
|
|
}
|
2020-06-14 06:15:31 +03:00
|
|
|
|
2020-06-14 10:24:26 +03:00
|
|
|
// clear out things.
|
2020-07-05 08:34:00 +03:00
|
|
|
this.formMessageInput.value = '';
|
|
|
|
this.tagMessageFormWarning.innerText = '';
|
2020-06-14 10:24:26 +03:00
|
|
|
}
|
2020-06-02 23:56:59 +03:00
|
|
|
}
|