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

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