選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

chat.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 message = Util.escapeHtml(this.value);
  36. $('#usermsg').val('').trigger('autosize.resize');
  37. this.focus();
  38. connection.emuc.sendMessage(message, nickname);
  39. }
  40. });
  41. var onTextAreaResize = function () {
  42. resizeChatConversation();
  43. scrollChatToBottom();
  44. };
  45. $('#usermsg').autosize({callback: onTextAreaResize});
  46. $("#chatspace").bind("shown",
  47. function () {
  48. unreadMessages = 0;
  49. setVisualNotification(false);
  50. });
  51. };
  52. /**
  53. * Appends the given message to the chat conversation.
  54. */
  55. my.updateChatConversation = function (from, displayName, message) {
  56. var divClassName = '';
  57. if (connection.emuc.myroomjid === from) {
  58. divClassName = "localuser";
  59. }
  60. else {
  61. divClassName = "remoteuser";
  62. if (!$('#chatspace').is(":visible")) {
  63. unreadMessages++;
  64. Util.playSoundNotification('chatNotification');
  65. setVisualNotification(true);
  66. }
  67. }
  68. //replace links and smileys
  69. var escMessage = Util.escapeHtml(message);
  70. var escDisplayName = Util.escapeHtml(displayName);
  71. message = processReplacements(escMessage);
  72. $('#chatconversation').append('<div class="' + divClassName + '"><b>' +
  73. escDisplayName + ': </b>' +
  74. message + '</div>');
  75. $('#chatconversation').animate(
  76. { scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  77. };
  78. /**
  79. * Opens / closes the chat area.
  80. */
  81. my.toggleChat = function () {
  82. var chatspace = $('#chatspace');
  83. var videospace = $('#videospace');
  84. var chatSize = (chatspace.is(":visible")) ? [0, 0] : Chat.getChatSize();
  85. var videospaceWidth = window.innerWidth - chatSize[0];
  86. var videospaceHeight = window.innerHeight;
  87. var videoSize
  88. = getVideoSize(null, null, videospaceWidth, videospaceHeight);
  89. var videoWidth = videoSize[0];
  90. var videoHeight = videoSize[1];
  91. var videoPosition = getVideoPosition(videoWidth,
  92. videoHeight,
  93. videospaceWidth,
  94. videospaceHeight);
  95. var horizontalIndent = videoPosition[0];
  96. var verticalIndent = videoPosition[1];
  97. if (chatspace.is(":visible")) {
  98. videospace.animate({right: chatSize[0],
  99. width: videospaceWidth,
  100. height: videospaceHeight},
  101. {queue: false,
  102. duration: 500});
  103. $('#largeVideoContainer').animate({ width: videospaceWidth,
  104. height: videospaceHeight},
  105. {queue: false,
  106. duration: 500
  107. });
  108. $('#largeVideo').animate({ width: videoWidth,
  109. height: videoHeight,
  110. top: verticalIndent,
  111. bottom: verticalIndent,
  112. left: horizontalIndent,
  113. right: horizontalIndent},
  114. { queue: false,
  115. duration: 500
  116. });
  117. $('#chatspace').hide("slide", { direction: "right",
  118. queue: false,
  119. duration: 500});
  120. }
  121. else {
  122. videospace.animate({right: chatSize[0],
  123. width: videospaceWidth,
  124. height: videospaceHeight},
  125. {queue: false,
  126. duration: 500,
  127. complete: function () {
  128. scrollChatToBottom();
  129. chatspace.trigger('shown');
  130. }
  131. });
  132. $('#largeVideoContainer').animate({ width: videospaceWidth,
  133. height: videospaceHeight},
  134. {queue: false,
  135. duration: 500
  136. });
  137. $('#largeVideo').animate({ width: videoWidth,
  138. height: videoHeight,
  139. top: verticalIndent,
  140. bottom: verticalIndent,
  141. left: horizontalIndent,
  142. right: horizontalIndent},
  143. {queue: false,
  144. duration: 500
  145. });
  146. $('#chatspace').show("slide", { direction: "right",
  147. queue: false,
  148. duration: 500});
  149. }
  150. // Request the focus in the nickname field or the chat input field.
  151. if ($('#nickname').css('visibility') === 'visible')
  152. $('#nickinput').focus();
  153. else {
  154. $('#usermsg').focus();
  155. }
  156. };
  157. /**
  158. * Sets the chat conversation mode.
  159. */
  160. my.setChatConversationMode = function (isConversationMode) {
  161. if (isConversationMode) {
  162. $('#nickname').css({visibility: 'hidden'});
  163. $('#chatconversation').css({visibility: 'visible'});
  164. $('#usermsg').css({visibility: 'visible'});
  165. $('#usermsg').focus();
  166. }
  167. };
  168. /**
  169. * Resizes the chat area.
  170. */
  171. my.resizeChat = function () {
  172. var chatSize = Chat.getChatSize();
  173. $('#chatspace').width(chatSize[0]);
  174. $('#chatspace').height(chatSize[1]);
  175. resizeChatConversation();
  176. };
  177. /**
  178. * Returns the size of the chat.
  179. */
  180. my.getChatSize = function () {
  181. var availableHeight = window.innerHeight;
  182. var availableWidth = window.innerWidth;
  183. var chatWidth = 200;
  184. if (availableWidth * 0.2 < 200)
  185. chatWidth = availableWidth * 0.2;
  186. return [chatWidth, availableHeight];
  187. };
  188. /**
  189. * Resizes the chat conversation.
  190. */
  191. function resizeChatConversation() {
  192. var usermsgStyleHeight = document.getElementById("usermsg").style.height;
  193. var usermsgHeight = usermsgStyleHeight
  194. .substring(0, usermsgStyleHeight.indexOf('px'));
  195. $('#usermsg').width($('#chatspace').width() - 10);
  196. $('#chatconversation').width($('#chatspace').width() - 10);
  197. $('#chatconversation')
  198. .height(window.innerHeight - 10 - parseInt(usermsgHeight));
  199. }
  200. /**
  201. * Shows/hides a visual notification, indicating that a message has arrived.
  202. */
  203. function setVisualNotification(show) {
  204. var unreadMsgElement = document.getElementById('unreadMessages');
  205. var glower = $('#chatButton');
  206. if (unreadMessages) {
  207. unreadMsgElement.innerHTML = unreadMessages.toString();
  208. showToolbar();
  209. var chatButtonElement
  210. = document.getElementById('chatButton').parentNode;
  211. var leftIndent = (Util.getTextWidth(chatButtonElement) -
  212. Util.getTextWidth(unreadMsgElement)) / 2;
  213. var topIndent = (Util.getTextHeight(chatButtonElement) -
  214. Util.getTextHeight(unreadMsgElement)) / 2 - 3;
  215. unreadMsgElement.setAttribute(
  216. 'style',
  217. 'top:' + topIndent +
  218. '; left:' + leftIndent + ';');
  219. if (!glower.hasClass('icon-chat-simple')) {
  220. glower.removeClass('icon-chat');
  221. glower.addClass('icon-chat-simple');
  222. }
  223. }
  224. else {
  225. unreadMsgElement.innerHTML = '';
  226. glower.removeClass('icon-chat-simple');
  227. glower.addClass('icon-chat');
  228. }
  229. if (show && !notificationInterval) {
  230. notificationInterval = window.setInterval(function () {
  231. glower.toggleClass('active');
  232. }, 800);
  233. }
  234. else if (!show && notificationInterval) {
  235. window.clearInterval(notificationInterval);
  236. notificationInterval = false;
  237. glower.removeClass('active');
  238. }
  239. }
  240. /**
  241. * Scrolls chat to the bottom.
  242. */
  243. function scrollChatToBottom() {
  244. setTimeout(function () {
  245. $('#chatconversation').scrollTop(
  246. $('#chatconversation')[0].scrollHeight);
  247. }, 5);
  248. }
  249. return my;
  250. }(Chat || {}));