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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Chat related user interface.
  3. */
  4. var Chat = (function (my) {
  5. var notificationInterval = false;
  6. var unreadMessages = 0;
  7. /**
  8. * Initializes chat related interface.
  9. */
  10. my.init = function () {
  11. var storedDisplayName = window.localStorage.displayname;
  12. if (storedDisplayName) {
  13. nickname = storedDisplayName;
  14. Chat.setChatConversationMode(true);
  15. }
  16. $('#nickinput').keydown(function(event) {
  17. if (event.keyCode === 13) {
  18. event.preventDefault();
  19. var val = Util.escapeHtml(this.value);
  20. this.value = '';
  21. if (!nickname) {
  22. nickname = val;
  23. window.localStorage.displayname = nickname;
  24. connection.emuc.addDisplayNameToPresence(nickname);
  25. connection.emuc.sendPresence();
  26. Chat.setChatConversationMode(true);
  27. return;
  28. }
  29. }
  30. });
  31. $('#usermsg').keydown(function(event) {
  32. if (event.keyCode === 13) {
  33. event.preventDefault();
  34. var message = Util.escapeHtml(this.value);
  35. $('#usermsg').val('').trigger('autosize.resize');
  36. this.focus();
  37. connection.emuc.sendMessage(message, nickname);
  38. }
  39. });
  40. var onTextAreaResize = function() {
  41. resizeChatConversation();
  42. scrollChatToBottom();
  43. };
  44. $('#usermsg').autosize({callback: onTextAreaResize});
  45. $("#chatspace").bind("shown",
  46. function() {
  47. unreadMessages = 0;
  48. setVisualNotification(false);
  49. });
  50. };
  51. /**
  52. * Appends the given message to the chat conversation.
  53. */
  54. my.updateChatConversation = function (from, displayName, message) {
  55. var divClassName = '';
  56. if (connection.emuc.myroomjid === from) {
  57. divClassName = "localuser";
  58. }
  59. else {
  60. divClassName = "remoteuser";
  61. if (!$('#chatspace').is(":visible")) {
  62. unreadMessages++;
  63. Util.playSoundNotification('chatNotification');
  64. setVisualNotification(true);
  65. }
  66. }
  67. //replace links and smileys
  68. var escMessage = Util.escapeHtml(message);
  69. var escDisplayName = Util.escapeHtml(displayName);
  70. message = processReplacements(escMessage);
  71. $('#chatconversation').append('<div class="' + divClassName + '"><b>'
  72. + escDisplayName + ': </b>'
  73. + message + '</div>');
  74. $('#chatconversation').animate(
  75. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  76. };
  77. /**
  78. * Opens / closes the chat area.
  79. */
  80. my.toggleChat = function () {
  81. var chatspace = $('#chatspace');
  82. var videospace = $('#videospace');
  83. var onShow = function () {
  84. resizeLarge();
  85. $('#chatspace').show("slide", { direction: "right", duration: 500});
  86. };
  87. var onHide = function () {
  88. $('#chatspace').hide("slide", { direction: "right", duration: 500});
  89. resizeLarge();
  90. };
  91. if (chatspace.is(":visible")) {
  92. videospace.animate( {right: 0},
  93. {queue: false,
  94. duration: 500,
  95. progress: onHide});
  96. }
  97. else {
  98. videospace.animate({right: chatspace.width()},
  99. {queue: false,
  100. duration: 500,
  101. progress: onShow,
  102. complete: function() {
  103. scrollChatToBottom();
  104. chatspace.trigger('shown');
  105. }
  106. });
  107. }
  108. // Request the focus in the nickname field or the chat input field.
  109. if ($('#nickname').css('visibility') === 'visible')
  110. $('#nickinput').focus();
  111. else {
  112. $('#usermsg').focus();
  113. }
  114. };
  115. /**
  116. * Sets the chat conversation mode.
  117. */
  118. my.setChatConversationMode = function (isConversationMode) {
  119. if (isConversationMode) {
  120. $('#nickname').css({visibility:"hidden"});
  121. $('#chatconversation').css({visibility:'visible'});
  122. $('#usermsg').css({visibility:'visible'});
  123. $('#usermsg').focus();
  124. }
  125. };
  126. /**
  127. * Resizes the chat area.
  128. */
  129. my.resizeChat = function () {
  130. var availableHeight = window.innerHeight;
  131. var availableWidth = window.innerWidth;
  132. var chatWidth = 200;
  133. if (availableWidth*0.2 < 200)
  134. chatWidth = availableWidth*0.2;
  135. $('#chatspace').width(chatWidth);
  136. $('#chatspace').height(availableHeight - 40);
  137. resizeChatConversation();
  138. };
  139. /**
  140. * Resizes the chat conversation.
  141. */
  142. function resizeChatConversation() {
  143. var usermsgStyleHeight = document.getElementById("usermsg").style.height;
  144. var usermsgHeight = usermsgStyleHeight
  145. .substring(0, usermsgStyleHeight.indexOf('px'));
  146. $('#chatconversation').width($('#chatspace').width() - 10);
  147. $('#chatconversation')
  148. .height(window.innerHeight - 50 - parseInt(usermsgHeight));
  149. }
  150. /**
  151. * Shows/hides a visual notification, indicating that a message has arrived.
  152. */
  153. function setVisualNotification(show) {
  154. var unreadMsgElement = document.getElementById('unreadMessages');
  155. if (unreadMessages) {
  156. unreadMsgElement.innerHTML = unreadMessages.toString();
  157. var chatButtonElement
  158. = document.getElementById('chat').parentNode;
  159. var leftIndent = (Util.getTextWidth(chatButtonElement)
  160. - Util.getTextWidth(unreadMsgElement) - 5)/2;
  161. var topIndent = (Util.getTextHeight(chatButtonElement)
  162. - Util.getTextHeight(unreadMsgElement))/2 - 2;
  163. unreadMsgElement.setAttribute(
  164. 'style',
  165. 'top:' + topIndent
  166. + '; left:' + leftIndent +';');
  167. }
  168. else
  169. unreadMsgElement.innerHTML = '';
  170. var glower = $('#chat');
  171. if (show && !notificationInterval) {
  172. notificationInterval = window.setInterval(function() {
  173. glower.toggleClass('active');
  174. }, 800);
  175. }
  176. else if (!show && notificationInterval) {
  177. window.clearInterval(notificationInterval);
  178. notificationInterval = false;
  179. glower.removeClass('active');
  180. }
  181. }
  182. /**
  183. * Scrolls chat to the bottom.
  184. */
  185. function scrollChatToBottom() {
  186. setTimeout(function() {
  187. $('#chatconversation').scrollTop(
  188. $('#chatconversation')[0].scrollHeight);
  189. }, 5);
  190. }
  191. return my;
  192. }(Chat || {}));