Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Chat.js 10KB

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