123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /* global APP, $ */
-
- import {processReplacements, linkify} from './Replacement';
- import CommandsProcessor from './Commands';
- import ToolbarToggler from '../../toolbars/ToolbarToggler';
-
- import UIUtil from '../../util/UIUtil';
- import UIEvents from '../../../../service/UI/UIEvents';
-
- var smileys = require("./smileys.json").smileys;
-
- var notificationInterval = false;
- var unreadMessages = 0;
-
-
- /**
- * Shows/hides a visual notification, indicating that a message has arrived.
- */
- function setVisualNotification(show) {
- var unreadMsgElement = document.getElementById('unreadMessages');
-
- var glower = $('#toolbar_button_chat');
-
- if (unreadMessages) {
- unreadMsgElement.innerHTML = unreadMessages.toString();
-
- ToolbarToggler.dockToolbar(true);
-
- var chatButtonElement
- = document.getElementById('toolbar_button_chat');
- var leftIndent = (UIUtil.getTextWidth(chatButtonElement) -
- UIUtil.getTextWidth(unreadMsgElement)) / 2;
- var topIndent = (UIUtil.getTextHeight(chatButtonElement) -
- UIUtil.getTextHeight(unreadMsgElement)) / 2 - 5;
-
- unreadMsgElement.setAttribute(
- 'style',
- 'top:' + topIndent +
- '; left:' + leftIndent + ';');
-
- if (!glower.hasClass('icon-chat-simple')) {
- glower.removeClass('icon-chat');
- glower.addClass('icon-chat-simple');
- }
- }
- else {
- unreadMsgElement.innerHTML = '';
- glower.removeClass('icon-chat-simple');
- glower.addClass('icon-chat');
- }
-
- if (show && !notificationInterval) {
- notificationInterval = window.setInterval(function () {
- glower.toggleClass('active');
- }, 800);
- }
- else if (!show && notificationInterval) {
- window.clearInterval(notificationInterval);
- notificationInterval = false;
- glower.removeClass('active');
- }
- }
-
-
- /**
- * Returns the current time in the format it is shown to the user
- * @returns {string}
- */
- function getCurrentTime(stamp) {
- var now = (stamp? new Date(stamp): new Date());
- var hour = now.getHours();
- var minute = now.getMinutes();
- var second = now.getSeconds();
- if(hour.toString().length === 1) {
- hour = '0'+hour;
- }
- if(minute.toString().length === 1) {
- minute = '0'+minute;
- }
- if(second.toString().length === 1) {
- second = '0'+second;
- }
- return hour+':'+minute+':'+second;
- }
-
- function toggleSmileys() {
- var smileys = $('#smileysContainer');
- if(!smileys.is(':visible')) {
- smileys.show("slide", { direction: "down", duration: 300});
- } else {
- smileys.hide("slide", { direction: "down", duration: 300});
- }
- $('#usermsg').focus();
- }
-
- function addClickFunction(smiley, number) {
- smiley.onclick = function addSmileyToMessage() {
- var usermsg = $('#usermsg');
- var message = usermsg.val();
- message += smileys['smiley' + number];
- usermsg.val(message);
- usermsg.get(0).setSelectionRange(message.length, message.length);
- toggleSmileys();
- usermsg.focus();
- };
- }
-
- /**
- * Adds the smileys container to the chat
- */
- function addSmileys() {
- var smileysContainer = document.createElement('div');
- smileysContainer.id = 'smileysContainer';
- for(var i = 1; i <= 21; i++) {
- var smileyContainer = document.createElement('div');
- smileyContainer.id = 'smiley' + i;
- smileyContainer.className = 'smileyContainer';
- var smiley = document.createElement('img');
- smiley.src = 'images/smileys/smiley' + i + '.svg';
- smiley.className = 'smiley';
- addClickFunction(smiley, i);
- smileyContainer.appendChild(smiley);
- smileysContainer.appendChild(smileyContainer);
- }
-
- $("#chat_container").append(smileysContainer);
- }
-
- /**
- * Resizes the chat conversation.
- */
- function resizeChatConversation() {
- var msgareaHeight = $('#usermsg').outerHeight();
- var chatspace = $('#chat_container');
- var width = chatspace.width();
- var chat = $('#chatconversation');
- var smileys = $('#smileysarea');
-
- smileys.height(msgareaHeight);
- $("#smileys").css('bottom', (msgareaHeight - 26) / 2);
- $('#smileysContainer').css('bottom', msgareaHeight);
- chat.width(width - 10);
- chat.height(window.innerHeight - 15 - msgareaHeight);
- }
-
- /**
- * Chat related user interface.
- */
- var Chat = {
- /**
- * Initializes chat related interface.
- */
- init (eventEmitter) {
- if (APP.settings.getDisplayName()) {
- Chat.setChatConversationMode(true);
- }
-
- $('#nickinput').keydown(function (event) {
- if (event.keyCode === 13) {
- event.preventDefault();
- let val = this.value;
- this.value = '';
- eventEmitter.emit(UIEvents.NICKNAME_CHANGED, val);
- }
- });
-
- var usermsg = $('#usermsg');
- usermsg.keydown(function (event) {
- if (event.keyCode === 13) {
- event.preventDefault();
- var value = this.value;
- usermsg.val('').trigger('autosize.resize');
- this.focus();
- var command = new CommandsProcessor(value, eventEmitter);
- if (command.isCommand()) {
- command.processCommand();
- } else {
- var message = UIUtil.escapeHtml(value);
- eventEmitter.emit(UIEvents.MESSAGE_CREATED, message);
- }
- }
- });
-
- var onTextAreaResize = function () {
- resizeChatConversation();
- Chat.scrollChatToBottom();
- };
- usermsg.autosize({callback: onTextAreaResize});
-
- $("#chat_container").bind("shown",
- function () {
- unreadMessages = 0;
- setVisualNotification(false);
- });
-
- addSmileys();
- },
-
- /**
- * Appends the given message to the chat conversation.
- */
- updateChatConversation (id, displayName, message, stamp) {
- var divClassName = '';
-
- if (APP.conference.isLocalId(id)) {
- divClassName = "localuser";
- } else {
- divClassName = "remoteuser";
-
- if (!Chat.isVisible()) {
- unreadMessages++;
- UIUtil.playSoundNotification('chatNotification');
- setVisualNotification(true);
- }
- }
-
- // replace links and smileys
- // Strophe already escapes special symbols on sending,
- // so we escape here only tags to avoid double &
- var escMessage = message.replace(/</g, '<').
- replace(/>/g, '>').replace(/\n/g, '<br/>');
- var escDisplayName = UIUtil.escapeHtml(displayName);
- message = processReplacements(escMessage);
-
- var messageContainer =
- '<div class="chatmessage">'+
- '<img src="images/chatArrow.svg" class="chatArrow">' +
- '<div class="username ' + divClassName +'">' + escDisplayName +
- '</div>' + '<div class="timestamp">' + getCurrentTime(stamp) +
- '</div>' + '<div class="usermessage">' + message + '</div>' +
- '</div>';
-
- $('#chatconversation').append(messageContainer);
- $('#chatconversation').animate(
- { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
- },
-
- /**
- * Appends error message to the conversation
- * @param errorMessage the received error message.
- * @param originalText the original message.
- */
- chatAddError (errorMessage, originalText) {
- errorMessage = UIUtil.escapeHtml(errorMessage);
- originalText = UIUtil.escapeHtml(originalText);
-
- $('#chatconversation').append(
- '<div class="errorMessage"><b>Error: </b>' + 'Your message' +
- (originalText? (' \"'+ originalText + '\"') : "") +
- ' was not sent.' +
- (errorMessage? (' Reason: ' + errorMessage) : '') + '</div>');
- $('#chatconversation').animate(
- { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
- },
-
- /**
- * Sets the subject to the UI
- * @param subject the subject
- */
- setSubject (subject) {
- if (subject) {
- subject = subject.trim();
- }
- $('#subject').html(linkify(UIUtil.escapeHtml(subject)));
- if (subject) {
- $("#subject").css({display: "block"});
- } else {
- $("#subject").css({display: "none"});
- }
- },
-
- /**
- * Sets the chat conversation mode.
- * @param {boolean} isConversationMode if chat should be in
- * conversation mode or not.
- */
- setChatConversationMode (isConversationMode) {
- $('#chat_container')
- .toggleClass('is-conversation-mode', isConversationMode);
- if (isConversationMode) {
- $('#usermsg').focus();
- }
- },
-
- /**
- * Resizes the chat area.
- */
- resizeChat (width, height) {
- $('#chat_container').width(width).height(height);
-
- resizeChatConversation();
- },
-
- /**
- * Indicates if the chat is currently visible.
- */
- isVisible () {
- return UIUtil.isVisible(
- document.getElementById("chat_container"));
- },
- /**
- * Shows and hides the window with the smileys
- */
- toggleSmileys,
-
- /**
- * Scrolls chat to the bottom.
- */
- scrollChatToBottom () {
- setTimeout(function () {
- $('#chatconversation').scrollTop(
- $('#chatconversation')[0].scrollHeight);
- }, 5);
- }
- };
-
- export default Chat;
|