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 12KB

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