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

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