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

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