您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

chat.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 chatSize = (chatspace.is(":visible")) ? [0, 0] : Chat.getChatSize();
  84. var videospaceWidth = window.innerWidth - chatSize[0];
  85. var videospaceHeight = window.innerHeight;
  86. var videoSize
  87. = getVideoSize(null, null, videospaceWidth, videospaceHeight);
  88. var videoWidth = videoSize[0];
  89. var videoHeight = videoSize[1];
  90. var videoPosition = getVideoPosition( videoWidth,
  91. videoHeight,
  92. videospaceWidth,
  93. videospaceHeight);
  94. var horizontalIndent = videoPosition[0];
  95. var verticalIndent = videoPosition[1];
  96. if (chatspace.is(":visible")) {
  97. videospace.animate( {right: chatSize[0],
  98. width: videospaceWidth,
  99. height: videospaceHeight},
  100. {queue: false,
  101. duration: 500});
  102. $('#largeVideoContainer').animate({ width: videospaceWidth,
  103. height: videospaceHeight},
  104. {queue: false,
  105. duration: 500
  106. });
  107. $('#largeVideo').animate({ width: videoWidth,
  108. height: videoHeight,
  109. top: verticalIndent,
  110. bottom: verticalIndent,
  111. left: horizontalIndent,
  112. right: horizontalIndent},
  113. { queue: false,
  114. duration: 500
  115. });
  116. $('#chatspace').hide("slide", { direction: "right",
  117. queue: false,
  118. duration: 500});
  119. }
  120. else {
  121. videospace.animate({right: chatSize[0],
  122. width: videospaceWidth,
  123. height: videospaceHeight},
  124. {queue: false,
  125. duration: 500,
  126. complete: function() {
  127. scrollChatToBottom();
  128. chatspace.trigger('shown');
  129. }
  130. });
  131. $('#largeVideoContainer').animate({ width: videospaceWidth,
  132. height: videospaceHeight},
  133. {queue: false,
  134. duration: 500
  135. });
  136. $('#largeVideo').animate({ width: videoWidth,
  137. height: videoHeight,
  138. top: verticalIndent,
  139. bottom: verticalIndent,
  140. left: horizontalIndent,
  141. right: horizontalIndent},
  142. {queue: false,
  143. duration: 500
  144. });
  145. $('#chatspace').show("slide", { direction: "right",
  146. queue: false,
  147. duration: 500});
  148. }
  149. // Request the focus in the nickname field or the chat input field.
  150. if ($('#nickname').css('visibility') === 'visible')
  151. $('#nickinput').focus();
  152. else {
  153. $('#usermsg').focus();
  154. }
  155. };
  156. /**
  157. * Sets the chat conversation mode.
  158. */
  159. my.setChatConversationMode = function (isConversationMode) {
  160. if (isConversationMode) {
  161. $('#nickname').css({visibility:"hidden"});
  162. $('#chatconversation').css({visibility:'visible'});
  163. $('#usermsg').css({visibility:'visible'});
  164. $('#usermsg').focus();
  165. }
  166. };
  167. /**
  168. * Resizes the chat area.
  169. */
  170. my.resizeChat = function () {
  171. var chatSize = Chat.getChatSize();
  172. $('#chatspace').width(chatSize[0]);
  173. $('#chatspace').height(chatSize[1]);
  174. resizeChatConversation();
  175. };
  176. /**
  177. * Returns the size of the chat.
  178. */
  179. my.getChatSize = function() {
  180. var availableHeight = window.innerHeight;
  181. var availableWidth = window.innerWidth;
  182. var chatWidth = 200;
  183. if (availableWidth*0.2 < 200)
  184. chatWidth = availableWidth*0.2;
  185. return [chatWidth, availableHeight];
  186. };
  187. /**
  188. * Resizes the chat conversation.
  189. */
  190. function resizeChatConversation() {
  191. var usermsgStyleHeight = document.getElementById("usermsg").style.height;
  192. var usermsgHeight = usermsgStyleHeight
  193. .substring(0, usermsgStyleHeight.indexOf('px'));
  194. $('#usermsg').width($('#chatspace').width() - 10);
  195. $('#chatconversation').width($('#chatspace').width() - 10);
  196. $('#chatconversation')
  197. .height(window.innerHeight - 10 - parseInt(usermsgHeight));
  198. };
  199. /**
  200. * Shows/hides a visual notification, indicating that a message has arrived.
  201. */
  202. function setVisualNotification(show) {
  203. var unreadMsgElement = document.getElementById('unreadMessages');
  204. var glower = $('#chatButton');
  205. if (unreadMessages) {
  206. unreadMsgElement.innerHTML = unreadMessages.toString();
  207. showToolbar();
  208. var chatButtonElement
  209. = document.getElementById('chatButton').parentNode;
  210. var leftIndent = (Util.getTextWidth(chatButtonElement)
  211. - Util.getTextWidth(unreadMsgElement))/2;
  212. var topIndent = (Util.getTextHeight(chatButtonElement)
  213. - Util.getTextHeight(unreadMsgElement))/2 - 3;
  214. unreadMsgElement.setAttribute(
  215. 'style',
  216. 'top:' + topIndent
  217. + '; left:' + leftIndent +';');
  218. if (!glower.hasClass('icon-chat-simple')) {
  219. glower.removeClass('icon-chat');
  220. glower.addClass('icon-chat-simple');
  221. }
  222. }
  223. else {
  224. unreadMsgElement.innerHTML = '';
  225. glower.removeClass('icon-chat-simple');
  226. glower.addClass('icon-chat');
  227. }
  228. if (show && !notificationInterval) {
  229. notificationInterval = window.setInterval(function() {
  230. glower.toggleClass('active');
  231. }, 800);
  232. }
  233. else if (!show && notificationInterval) {
  234. window.clearInterval(notificationInterval);
  235. notificationInterval = false;
  236. glower.removeClass('active');
  237. }
  238. }
  239. /**
  240. * Scrolls chat to the bottom.
  241. */
  242. function scrollChatToBottom() {
  243. setTimeout(function() {
  244. $('#chatconversation').scrollTop(
  245. $('#chatconversation')[0].scrollHeight);
  246. }, 5);
  247. }
  248. return my;
  249. }(Chat || {}));