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

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