You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Chat.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* global APP, $ */
  2. import {processReplacements, linkify} from './Replacement';
  3. import CommandsProcessor from './Commands';
  4. import ToolbarToggler from '../../toolbars/ToolbarToggler';
  5. import VideoLayout from "../../videolayout/VideoLayout";
  6. import UIUtil from '../../util/UIUtil';
  7. import UIEvents from '../../../../service/UI/UIEvents';
  8. import { smileys } from './smileys';
  9. let unreadMessages = 0;
  10. const sidePanelsContainerId = 'sideToolbarContainer';
  11. const htmlStr = `
  12. <div id="chat_container" class="sideToolbarContainer__inner">
  13. <div id="nickname">
  14. <span data-i18n="chat.nickname.title"></span>
  15. <form>
  16. <input type='text'
  17. class="input-control" id="nickinput" autofocus
  18. data-i18n="[placeholder]chat.nickname.popover">
  19. </form>
  20. </div>
  21. <div id="chatconversation"></div>
  22. <audio id="chatNotification" src="sounds/incomingMessage.wav"
  23. preload="auto"></audio>
  24. <textarea id="usermsg" autofocus
  25. data-i18n="[placeholder]chat.messagebox"></textarea>
  26. <div id="smileysarea">
  27. <div id="smileys" id="toggle_smileys">
  28. <img src="images/smile.svg"/>
  29. </div>
  30. </div>
  31. </div>`;
  32. function initHTML() {
  33. $(`#${sidePanelsContainerId}`)
  34. .append(htmlStr);
  35. }
  36. /**
  37. * The container id, which is and the element id.
  38. */
  39. var CHAT_CONTAINER_ID = "chat_container";
  40. /**
  41. * Updates visual notification, indicating that a message has arrived.
  42. */
  43. function updateVisualNotification() {
  44. var unreadMsgElement = document.getElementById('unreadMessages');
  45. if (unreadMessages) {
  46. unreadMsgElement.innerHTML = unreadMessages.toString();
  47. ToolbarToggler.dockToolbar(true);
  48. var chatButtonElement
  49. = document.getElementById('toolbar_button_chat');
  50. var leftIndent = (UIUtil.getTextWidth(chatButtonElement) -
  51. UIUtil.getTextWidth(unreadMsgElement)) / 2;
  52. var topIndent = (UIUtil.getTextHeight(chatButtonElement) -
  53. UIUtil.getTextHeight(unreadMsgElement)) / 2 - 5;
  54. unreadMsgElement.setAttribute(
  55. 'style',
  56. 'top:' + topIndent +
  57. '; left:' + leftIndent + ';');
  58. }
  59. else {
  60. unreadMsgElement.innerHTML = '';
  61. }
  62. $(unreadMsgElement).parent()[unreadMessages > 0 ? 'show' : 'hide']();
  63. }
  64. /**
  65. * Returns the current time in the format it is shown to the user
  66. * @returns {string}
  67. */
  68. function getCurrentTime(stamp) {
  69. var now = (stamp? new Date(stamp): new Date());
  70. var hour = now.getHours();
  71. var minute = now.getMinutes();
  72. var second = now.getSeconds();
  73. if(hour.toString().length === 1) {
  74. hour = '0'+hour;
  75. }
  76. if(minute.toString().length === 1) {
  77. minute = '0'+minute;
  78. }
  79. if(second.toString().length === 1) {
  80. second = '0'+second;
  81. }
  82. return hour+':'+minute+':'+second;
  83. }
  84. function toggleSmileys() {
  85. var smileys = $('#smileysContainer');
  86. if(!smileys.is(':visible')) {
  87. smileys.show("slide", { direction: "down", duration: 300});
  88. } else {
  89. smileys.hide("slide", { direction: "down", duration: 300});
  90. }
  91. $('#usermsg').focus();
  92. }
  93. function addClickFunction(smiley, number) {
  94. smiley.onclick = function addSmileyToMessage() {
  95. var usermsg = $('#usermsg');
  96. var message = usermsg.val();
  97. message += smileys['smiley' + number];
  98. usermsg.val(message);
  99. usermsg.get(0).setSelectionRange(message.length, message.length);
  100. toggleSmileys();
  101. usermsg.focus();
  102. };
  103. }
  104. /**
  105. * Adds the smileys container to the chat
  106. */
  107. function addSmileys() {
  108. var smileysContainer = document.createElement('div');
  109. smileysContainer.id = 'smileysContainer';
  110. for(var i = 1; i <= 21; i++) {
  111. var smileyContainer = document.createElement('div');
  112. smileyContainer.id = 'smiley' + i;
  113. smileyContainer.className = 'smileyContainer';
  114. var smiley = document.createElement('img');
  115. smiley.src = 'images/smileys/smiley' + i + '.svg';
  116. smiley.className = 'smiley';
  117. addClickFunction(smiley, i);
  118. smileyContainer.appendChild(smiley);
  119. smileysContainer.appendChild(smileyContainer);
  120. }
  121. $("#chat_container").append(smileysContainer);
  122. }
  123. /**
  124. * Resizes the chat conversation.
  125. */
  126. function resizeChatConversation() {
  127. var msgareaHeight = $('#usermsg').outerHeight();
  128. var chatspace = $('#' + CHAT_CONTAINER_ID);
  129. var width = chatspace.width();
  130. var chat = $('#chatconversation');
  131. var smileys = $('#smileysarea');
  132. smileys.height(msgareaHeight);
  133. $("#smileys").css('bottom', (msgareaHeight - 26) / 2);
  134. $('#smileysContainer').css('bottom', msgareaHeight);
  135. chat.width(width - 10);
  136. chat.height(window.innerHeight - 15 - msgareaHeight);
  137. }
  138. /**
  139. * Focus input after 400 ms
  140. * Found input by id
  141. *
  142. * @param id {string} input id
  143. */
  144. function deferredFocus(id){
  145. setTimeout(function (){
  146. $(`#${id}`).focus();
  147. }, 400);
  148. }
  149. /**
  150. * Chat related user interface.
  151. */
  152. var Chat = {
  153. /**
  154. * Initializes chat related interface.
  155. */
  156. init (eventEmitter) {
  157. initHTML();
  158. if (APP.settings.getDisplayName()) {
  159. Chat.setChatConversationMode(true);
  160. }
  161. $("#toggle_smileys").click(function() {
  162. Chat.toggleSmileys();
  163. });
  164. $('#nickinput').keydown(function (event) {
  165. if (event.keyCode === 13) {
  166. event.preventDefault();
  167. let val = this.value;
  168. this.value = '';
  169. eventEmitter.emit(UIEvents.NICKNAME_CHANGED, val);
  170. deferredFocus('usermsg');
  171. }
  172. });
  173. var usermsg = $('#usermsg');
  174. usermsg.keydown(function (event) {
  175. if (event.keyCode === 13) {
  176. event.preventDefault();
  177. var value = this.value;
  178. usermsg.val('').trigger('autosize.resize');
  179. this.focus();
  180. var command = new CommandsProcessor(value, eventEmitter);
  181. if (command.isCommand()) {
  182. command.processCommand();
  183. } else {
  184. var message = UIUtil.escapeHtml(value);
  185. eventEmitter.emit(UIEvents.MESSAGE_CREATED, message);
  186. }
  187. }
  188. });
  189. var onTextAreaResize = function () {
  190. resizeChatConversation();
  191. Chat.scrollChatToBottom();
  192. };
  193. usermsg.autosize({callback: onTextAreaResize});
  194. eventEmitter.on(UIEvents.SIDE_TOOLBAR_CONTAINER_TOGGLED,
  195. function(containerId, isVisible) {
  196. if (containerId !== CHAT_CONTAINER_ID || !isVisible)
  197. return;
  198. unreadMessages = 0;
  199. updateVisualNotification();
  200. // Undock the toolbar when the chat is shown and if we're in a
  201. // video mode.
  202. if (VideoLayout.isLargeVideoVisible()) {
  203. ToolbarToggler.dockToolbar(false);
  204. }
  205. // if we are in conversation mode focus on the text input
  206. // if we are not, focus on the display name input
  207. if (APP.settings.getDisplayName())
  208. deferredFocus('usermsg');
  209. else
  210. deferredFocus('nickinput');
  211. });
  212. addSmileys();
  213. updateVisualNotification();
  214. },
  215. /**
  216. * Appends the given message to the chat conversation.
  217. */
  218. updateChatConversation (id, displayName, message, stamp) {
  219. var divClassName = '';
  220. if (APP.conference.isLocalId(id)) {
  221. divClassName = "localuser";
  222. } else {
  223. divClassName = "remoteuser";
  224. if (!Chat.isVisible()) {
  225. unreadMessages++;
  226. UIUtil.playSoundNotification('chatNotification');
  227. updateVisualNotification();
  228. }
  229. }
  230. // replace links and smileys
  231. // Strophe already escapes special symbols on sending,
  232. // so we escape here only tags to avoid double &amp;
  233. var escMessage = message.replace(/</g, '&lt;').
  234. replace(/>/g, '&gt;').replace(/\n/g, '<br/>');
  235. var escDisplayName = UIUtil.escapeHtml(displayName);
  236. message = processReplacements(escMessage);
  237. var messageContainer =
  238. '<div class="chatmessage">'+
  239. '<img src="images/chatArrow.svg" class="chatArrow">' +
  240. '<div class="username ' + divClassName +'">' + escDisplayName +
  241. '</div>' + '<div class="timestamp">' + getCurrentTime(stamp) +
  242. '</div>' + '<div class="usermessage">' + message + '</div>' +
  243. '</div>';
  244. $('#chatconversation').append(messageContainer);
  245. $('#chatconversation').animate(
  246. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  247. },
  248. /**
  249. * Appends error message to the conversation
  250. * @param errorMessage the received error message.
  251. * @param originalText the original message.
  252. */
  253. chatAddError (errorMessage, originalText) {
  254. errorMessage = UIUtil.escapeHtml(errorMessage);
  255. originalText = UIUtil.escapeHtml(originalText);
  256. $('#chatconversation').append(
  257. '<div class="errorMessage"><b>Error: </b>' + 'Your message' +
  258. (originalText? (' \"'+ originalText + '\"') : "") +
  259. ' was not sent.' +
  260. (errorMessage? (' Reason: ' + errorMessage) : '') + '</div>');
  261. $('#chatconversation').animate(
  262. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  263. },
  264. /**
  265. * Sets the subject to the UI
  266. * @param subject the subject
  267. */
  268. setSubject (subject) {
  269. let toggleFunction;
  270. if (subject) {
  271. subject = subject.trim();
  272. }
  273. toggleFunction = subject ? UIUtil.showElement : UIUtil.hideElement;
  274. toggleFunction = toggleFunction.bind(UIUtil);
  275. let subjectId = 'subject';
  276. let html = linkify(UIUtil.escapeHtml(subject));
  277. $(`#${subjectId}`).html(html);
  278. toggleFunction(subjectId);
  279. },
  280. /**
  281. * Sets the chat conversation mode.
  282. * Conversation mode is the normal chat mode, non conversation mode is
  283. * where we ask user to input its display name.
  284. * @param {boolean} isConversationMode if chat should be in
  285. * conversation mode or not.
  286. */
  287. setChatConversationMode (isConversationMode) {
  288. $('#' + CHAT_CONTAINER_ID)
  289. .toggleClass('is-conversation-mode', isConversationMode);
  290. },
  291. /**
  292. * Resizes the chat area.
  293. */
  294. resizeChat (width, height) {
  295. $('#' + CHAT_CONTAINER_ID).width(width).height(height);
  296. resizeChatConversation();
  297. },
  298. /**
  299. * Indicates if the chat is currently visible.
  300. */
  301. isVisible () {
  302. return UIUtil.isVisible(
  303. document.getElementById(CHAT_CONTAINER_ID));
  304. },
  305. /**
  306. * Shows and hides the window with the smileys
  307. */
  308. toggleSmileys,
  309. /**
  310. * Scrolls chat to the bottom.
  311. */
  312. scrollChatToBottom () {
  313. setTimeout(function () {
  314. $('#chatconversation').scrollTop(
  315. $('#chatconversation')[0].scrollHeight);
  316. }, 5);
  317. }
  318. };
  319. export default Chat;