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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* global $, Util, connection, nickname:true, getVideoSize,
  2. getVideoPosition, showToolbar */
  3. var Replacement = require("./Replacement");
  4. var CommandsProcessor = require("./Commands");
  5. var ToolbarToggler = require("../../toolbars/ToolbarToggler");
  6. var smileys = require("./smileys.json").smileys;
  7. var notificationInterval = false;
  8. var unreadMessages = 0;
  9. /**
  10. * Shows/hides a visual notification, indicating that a message has arrived.
  11. */
  12. function setVisualNotification(show) {
  13. var unreadMsgElement = document.getElementById('unreadMessages');
  14. var unreadMsgBottomElement
  15. = document.getElementById('bottomUnreadMessages');
  16. var glower = $('#chatButton');
  17. var bottomGlower = $('#chatBottomButton');
  18. if (unreadMessages) {
  19. unreadMsgElement.innerHTML = unreadMessages.toString();
  20. unreadMsgBottomElement.innerHTML = unreadMessages.toString();
  21. ToolbarToggler.dockToolbar(true);
  22. var chatButtonElement
  23. = document.getElementById('chatButton').parentNode;
  24. var leftIndent = (Util.getTextWidth(chatButtonElement) -
  25. Util.getTextWidth(unreadMsgElement)) / 2;
  26. var topIndent = (Util.getTextHeight(chatButtonElement) -
  27. Util.getTextHeight(unreadMsgElement)) / 2 - 3;
  28. unreadMsgElement.setAttribute(
  29. 'style',
  30. 'top:' + topIndent +
  31. '; left:' + leftIndent + ';');
  32. var chatBottomButtonElement
  33. = document.getElementById('chatBottomButton').parentNode;
  34. var bottomLeftIndent = (Util.getTextWidth(chatBottomButtonElement) -
  35. Util.getTextWidth(unreadMsgBottomElement)) / 2;
  36. var bottomTopIndent = (Util.getTextHeight(chatBottomButtonElement) -
  37. Util.getTextHeight(unreadMsgBottomElement)) / 2 - 2;
  38. unreadMsgBottomElement.setAttribute(
  39. 'style',
  40. 'top:' + bottomTopIndent +
  41. '; left:' + bottomLeftIndent + ';');
  42. if (!glower.hasClass('icon-chat-simple')) {
  43. glower.removeClass('icon-chat');
  44. glower.addClass('icon-chat-simple');
  45. }
  46. }
  47. else {
  48. unreadMsgElement.innerHTML = '';
  49. unreadMsgBottomElement.innerHTML = '';
  50. glower.removeClass('icon-chat-simple');
  51. glower.addClass('icon-chat');
  52. }
  53. if (show && !notificationInterval) {
  54. notificationInterval = window.setInterval(function () {
  55. glower.toggleClass('active');
  56. bottomGlower.toggleClass('active glowing');
  57. }, 800);
  58. }
  59. else if (!show && notificationInterval) {
  60. window.clearInterval(notificationInterval);
  61. notificationInterval = false;
  62. glower.removeClass('active');
  63. bottomGlower.removeClass('glowing');
  64. bottomGlower.addClass('active');
  65. }
  66. }
  67. /**
  68. * Returns the current time in the format it is shown to the user
  69. * @returns {string}
  70. */
  71. function getCurrentTime() {
  72. var now = new Date();
  73. var hour = now.getHours();
  74. var minute = now.getMinutes();
  75. var second = now.getSeconds();
  76. if(hour.toString().length === 1) {
  77. hour = '0'+hour;
  78. }
  79. if(minute.toString().length === 1) {
  80. minute = '0'+minute;
  81. }
  82. if(second.toString().length === 1) {
  83. second = '0'+second;
  84. }
  85. return hour+':'+minute+':'+second;
  86. }
  87. function toggleSmileys()
  88. {
  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 () {
  150. var storedDisplayName = window.localStorage.displayname;
  151. if (storedDisplayName) {
  152. nickname = storedDisplayName;
  153. Chat.setChatConversationMode(true);
  154. }
  155. $('#nickinput').keydown(function (event) {
  156. if (event.keyCode === 13) {
  157. event.preventDefault();
  158. var val = Util.escapeHtml(this.value);
  159. this.value = '';
  160. if (!nickname) {
  161. nickname = val;
  162. window.localStorage.displayname = nickname;
  163. connection.emuc.addDisplayNameToPresence(nickname);
  164. connection.emuc.sendPresence();
  165. Chat.setChatConversationMode(true);
  166. return;
  167. }
  168. }
  169. });
  170. $('#usermsg').keydown(function (event) {
  171. if (event.keyCode === 13) {
  172. event.preventDefault();
  173. var value = this.value;
  174. $('#usermsg').val('').trigger('autosize.resize');
  175. this.focus();
  176. var command = new CommandsProcessor(value);
  177. if(command.isCommand())
  178. {
  179. command.processCommand();
  180. }
  181. else
  182. {
  183. var message = Util.escapeHtml(value);
  184. connection.emuc.sendMessage(message, nickname);
  185. }
  186. }
  187. });
  188. var onTextAreaResize = function () {
  189. resizeChatConversation();
  190. Chat.scrollChatToBottom();
  191. };
  192. $('#usermsg').autosize({callback: onTextAreaResize});
  193. $("#chatspace").bind("shown",
  194. function () {
  195. unreadMessages = 0;
  196. setVisualNotification(false);
  197. });
  198. addSmileys();
  199. };
  200. /**
  201. * Appends the given message to the chat conversation.
  202. */
  203. my.updateChatConversation = function (from, displayName, message) {
  204. var divClassName = '';
  205. if (connection.emuc.myroomjid === from) {
  206. divClassName = "localuser";
  207. }
  208. else {
  209. divClassName = "remoteuser";
  210. if (!Chat.isVisible()) {
  211. unreadMessages++;
  212. Util.playSoundNotification('chatNotification');
  213. setVisualNotification(true);
  214. }
  215. }
  216. // replace links and smileys
  217. // Strophe already escapes special symbols on sending,
  218. // so we escape here only tags to avoid double &amp;
  219. var escMessage = message.replace(/</g, '&lt;').
  220. replace(/>/g, '&gt;').replace(/\n/g, '<br/>');
  221. var escDisplayName = Util.escapeHtml(displayName);
  222. message = Replacement.processReplacements(escMessage);
  223. var messageContainer =
  224. '<div class="chatmessage">'+
  225. '<img src="../images/chatArrow.svg" class="chatArrow">' +
  226. '<div class="username ' + divClassName +'">' + escDisplayName +
  227. '</div>' + '<div class="timestamp">' + getCurrentTime() +
  228. '</div>' + '<div class="usermessage">' + message + '</div>' +
  229. '</div>';
  230. $('#chatconversation').append(messageContainer);
  231. $('#chatconversation').animate(
  232. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  233. };
  234. /**
  235. * Appends error message to the conversation
  236. * @param errorMessage the received error message.
  237. * @param originalText the original message.
  238. */
  239. my.chatAddError = function(errorMessage, originalText)
  240. {
  241. errorMessage = Util.escapeHtml(errorMessage);
  242. originalText = Util.escapeHtml(originalText);
  243. $('#chatconversation').append(
  244. '<div class="errorMessage"><b>Error: </b>' + 'Your message' +
  245. (originalText? (' \"'+ originalText + '\"') : "") +
  246. ' was not sent.' +
  247. (errorMessage? (' Reason: ' + errorMessage) : '') + '</div>');
  248. $('#chatconversation').animate(
  249. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  250. };
  251. /**
  252. * Sets the subject to the UI
  253. * @param subject the subject
  254. */
  255. my.chatSetSubject = function(subject)
  256. {
  257. if(subject)
  258. subject = subject.trim();
  259. $('#subject').html(Replacement.linkify(Util.escapeHtml(subject)));
  260. if(subject === "")
  261. {
  262. $("#subject").css({display: "none"});
  263. }
  264. else
  265. {
  266. $("#subject").css({display: "block"});
  267. }
  268. };
  269. /**
  270. * Sets the chat conversation mode.
  271. */
  272. my.setChatConversationMode = function (isConversationMode) {
  273. if (isConversationMode) {
  274. $('#nickname').css({visibility: 'hidden'});
  275. $('#chatconversation').css({visibility: 'visible'});
  276. $('#usermsg').css({visibility: 'visible'});
  277. $('#smileysarea').css({visibility: 'visible'});
  278. $('#usermsg').focus();
  279. }
  280. };
  281. /**
  282. * Resizes the chat area.
  283. */
  284. my.resizeChat = function () {
  285. var chatSize = require("../SidePanelToggler").getPanelSize();
  286. $('#chatspace').width(chatSize[0]);
  287. $('#chatspace').height(chatSize[1]);
  288. resizeChatConversation();
  289. };
  290. /**
  291. * Indicates if the chat is currently visible.
  292. */
  293. my.isVisible = function () {
  294. return $('#chatspace').is(":visible");
  295. };
  296. /**
  297. * Shows and hides the window with the smileys
  298. */
  299. my.toggleSmileys = toggleSmileys;
  300. /**
  301. * Scrolls chat to the bottom.
  302. */
  303. my.scrollChatToBottom = function() {
  304. setTimeout(function () {
  305. $('#chatconversation').scrollTop(
  306. $('#chatconversation')[0].scrollHeight);
  307. }, 5);
  308. };
  309. return my;
  310. }(Chat || {}));
  311. module.exports = Chat;