您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Chat.js 10KB

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