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

Chat.js 9.5KB

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