Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

chat.js 11KB

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