Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* global $, Util, nickname:true, showToolbar */
  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 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. if(NicknameHandler.getNickname())
  151. Chat.setChatConversationMode(true);
  152. NicknameHandler.addListener(UIEvents.NICKNAME_CHANGED,
  153. function (nickname) {
  154. Chat.setChatConversationMode(true);
  155. });
  156. $('#nickinput').keydown(function (event) {
  157. if (event.keyCode === 13) {
  158. event.preventDefault();
  159. var val = Util.escapeHtml(this.value);
  160. this.value = '';
  161. if (!NicknameHandler.getNickname()) {
  162. NicknameHandler.setNickname(val);
  163. return;
  164. }
  165. }
  166. });
  167. $('#usermsg').keydown(function (event) {
  168. if (event.keyCode === 13) {
  169. event.preventDefault();
  170. var value = this.value;
  171. $('#usermsg').val('').trigger('autosize.resize');
  172. this.focus();
  173. var command = new CommandsProcessor(value);
  174. if(command.isCommand())
  175. {
  176. command.processCommand();
  177. }
  178. else
  179. {
  180. var message = Util.escapeHtml(value);
  181. xmpp.sendChatMessage(message, NicknameHandler.getNickname());
  182. }
  183. }
  184. });
  185. var onTextAreaResize = function () {
  186. resizeChatConversation();
  187. Chat.scrollChatToBottom();
  188. };
  189. $('#usermsg').autosize({callback: onTextAreaResize});
  190. $("#chatspace").bind("shown",
  191. function () {
  192. unreadMessages = 0;
  193. setVisualNotification(false);
  194. });
  195. addSmileys();
  196. };
  197. /**
  198. * Appends the given message to the chat conversation.
  199. */
  200. my.updateChatConversation = function (from, displayName, message) {
  201. var divClassName = '';
  202. if (xmpp.myJid() === from) {
  203. divClassName = "localuser";
  204. }
  205. else {
  206. divClassName = "remoteuser";
  207. if (!Chat.isVisible()) {
  208. unreadMessages++;
  209. Util.playSoundNotification('chatNotification');
  210. setVisualNotification(true);
  211. }
  212. }
  213. // replace links and smileys
  214. // Strophe already escapes special symbols on sending,
  215. // so we escape here only tags to avoid double &amp;
  216. var escMessage = message.replace(/</g, '&lt;').
  217. replace(/>/g, '&gt;').replace(/\n/g, '<br/>');
  218. var escDisplayName = Util.escapeHtml(displayName);
  219. message = Replacement.processReplacements(escMessage);
  220. var messageContainer =
  221. '<div class="chatmessage">'+
  222. '<img src="../images/chatArrow.svg" class="chatArrow">' +
  223. '<div class="username ' + divClassName +'">' + escDisplayName +
  224. '</div>' + '<div class="timestamp">' + getCurrentTime() +
  225. '</div>' + '<div class="usermessage">' + message + '</div>' +
  226. '</div>';
  227. $('#chatconversation').append(messageContainer);
  228. $('#chatconversation').animate(
  229. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  230. };
  231. /**
  232. * Appends error message to the conversation
  233. * @param errorMessage the received error message.
  234. * @param originalText the original message.
  235. */
  236. my.chatAddError = function(errorMessage, originalText)
  237. {
  238. errorMessage = Util.escapeHtml(errorMessage);
  239. originalText = Util.escapeHtml(originalText);
  240. $('#chatconversation').append(
  241. '<div class="errorMessage"><b>Error: </b>' + 'Your message' +
  242. (originalText? (' \"'+ originalText + '\"') : "") +
  243. ' was not sent.' +
  244. (errorMessage? (' Reason: ' + errorMessage) : '') + '</div>');
  245. $('#chatconversation').animate(
  246. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  247. };
  248. /**
  249. * Sets the subject to the UI
  250. * @param subject the subject
  251. */
  252. my.chatSetSubject = function(subject)
  253. {
  254. if(subject)
  255. subject = subject.trim();
  256. $('#subject').html(Replacement.linkify(Util.escapeHtml(subject)));
  257. if(subject === "")
  258. {
  259. $("#subject").css({display: "none"});
  260. }
  261. else
  262. {
  263. $("#subject").css({display: "block"});
  264. }
  265. };
  266. /**
  267. * Sets the chat conversation mode.
  268. */
  269. my.setChatConversationMode = function (isConversationMode) {
  270. if (isConversationMode) {
  271. $('#nickname').css({visibility: 'hidden'});
  272. $('#chatconversation').css({visibility: 'visible'});
  273. $('#usermsg').css({visibility: 'visible'});
  274. $('#smileysarea').css({visibility: 'visible'});
  275. $('#usermsg').focus();
  276. }
  277. };
  278. /**
  279. * Resizes the chat area.
  280. */
  281. my.resizeChat = function () {
  282. var chatSize = require("../SidePanelToggler").getPanelSize();
  283. $('#chatspace').width(chatSize[0]);
  284. $('#chatspace').height(chatSize[1]);
  285. resizeChatConversation();
  286. };
  287. /**
  288. * Indicates if the chat is currently visible.
  289. */
  290. my.isVisible = function () {
  291. return $('#chatspace').is(":visible");
  292. };
  293. /**
  294. * Shows and hides the window with the smileys
  295. */
  296. my.toggleSmileys = toggleSmileys;
  297. /**
  298. * Scrolls chat to the bottom.
  299. */
  300. my.scrollChatToBottom = function() {
  301. setTimeout(function () {
  302. $('#chatconversation').scrollTop(
  303. $('#chatconversation')[0].scrollHeight);
  304. }, 5);
  305. };
  306. return my;
  307. }(Chat || {}));
  308. module.exports = Chat;