Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Chat.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. }
  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. },
  172. /**
  173. * Appends the given message to the chat conversation.
  174. */
  175. updateChatConversation (id, displayName, message, stamp) {
  176. var divClassName = '';
  177. if (APP.conference.isLocalId(id)) {
  178. divClassName = "localuser";
  179. } else {
  180. divClassName = "remoteuser";
  181. if (!Chat.isVisible()) {
  182. unreadMessages++;
  183. UIUtil.playSoundNotification('chatNotification');
  184. updateVisualNotification();
  185. }
  186. }
  187. // replace links and smileys
  188. // Strophe already escapes special symbols on sending,
  189. // so we escape here only tags to avoid double &amp;
  190. var escMessage = message.replace(/</g, '&lt;').
  191. replace(/>/g, '&gt;').replace(/\n/g, '<br/>');
  192. var escDisplayName = UIUtil.escapeHtml(displayName);
  193. message = processReplacements(escMessage);
  194. var messageContainer =
  195. '<div class="chatmessage">'+
  196. '<img src="images/chatArrow.svg" class="chatArrow">' +
  197. '<div class="username ' + divClassName +'">' + escDisplayName +
  198. '</div>' + '<div class="timestamp">' + getCurrentTime(stamp) +
  199. '</div>' + '<div class="usermessage">' + message + '</div>' +
  200. '</div>';
  201. $('#chatconversation').append(messageContainer);
  202. $('#chatconversation').animate(
  203. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  204. },
  205. /**
  206. * Appends error message to the conversation
  207. * @param errorMessage the received error message.
  208. * @param originalText the original message.
  209. */
  210. chatAddError (errorMessage, originalText) {
  211. errorMessage = UIUtil.escapeHtml(errorMessage);
  212. originalText = UIUtil.escapeHtml(originalText);
  213. $('#chatconversation').append(
  214. '<div class="errorMessage"><b>Error: </b>' + 'Your message' +
  215. (originalText? (' \"'+ originalText + '\"') : "") +
  216. ' was not sent.' +
  217. (errorMessage? (' Reason: ' + errorMessage) : '') + '</div>');
  218. $('#chatconversation').animate(
  219. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  220. },
  221. /**
  222. * Sets the subject to the UI
  223. * @param subject the subject
  224. */
  225. setSubject (subject) {
  226. if (subject) {
  227. subject = subject.trim();
  228. }
  229. $('#subject').html(linkify(UIUtil.escapeHtml(subject)));
  230. if (subject) {
  231. $("#subject").css({display: "block"});
  232. } else {
  233. $("#subject").css({display: "none"});
  234. }
  235. },
  236. /**
  237. * Sets the chat conversation mode.
  238. * Conversation mode is the normal chat mode, non conversation mode is
  239. * where we ask user to input its display name.
  240. * @param {boolean} isConversationMode if chat should be in
  241. * conversation mode or not.
  242. */
  243. setChatConversationMode (isConversationMode) {
  244. $('#' + CHAT_CONTAINER_ID)
  245. .toggleClass('is-conversation-mode', isConversationMode);
  246. // this is needed when we transition from no conversation mode to
  247. // conversation mode. When user enters his nickname and hits enter,
  248. // to focus on the write area.
  249. if (isConversationMode) {
  250. $('#usermsg').focus();
  251. }
  252. },
  253. /**
  254. * Resizes the chat area.
  255. */
  256. resizeChat (width, height) {
  257. $('#' + CHAT_CONTAINER_ID).width(width).height(height);
  258. resizeChatConversation();
  259. },
  260. /**
  261. * Indicates if the chat is currently visible.
  262. */
  263. isVisible () {
  264. return UIUtil.isVisible(
  265. document.getElementById(CHAT_CONTAINER_ID));
  266. },
  267. /**
  268. * Shows and hides the window with the smileys
  269. */
  270. toggleSmileys,
  271. /**
  272. * Scrolls chat to the bottom.
  273. */
  274. scrollChatToBottom () {
  275. setTimeout(function () {
  276. $('#chatconversation').scrollTop(
  277. $('#chatconversation')[0].scrollHeight);
  278. }, 5);
  279. }
  280. };
  281. export default Chat;