ChatWindow.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * $Id: ChatWindow.js,v 1.13 2013/12/13 12:53:01 gaudenz Exp $
  3. * Copyright (c) 2006-2010, JGraph Ltd
  4. */
  5. function ChatWindow(editorUi, title, content, x, y, width, height, realtime)
  6. {
  7. this.editorUi = editorUi;
  8. this.doc = realtime.doc;
  9. this.rtModel = realtime.rt;
  10. this.chatHistory = realtime.chatHistory;
  11. // Supports old chat map if it exists
  12. this.chatMap = realtime.chatMap;
  13. this.configCollabInfo();
  14. var mainDiv = document.createElement('div');
  15. mainDiv.id = 'mainDiv';
  16. var chatDiv = document.createElement('div');
  17. chatDiv.style.padding = '3px';
  18. mainDiv.appendChild(chatDiv);
  19. var chatLineDiv = document.createElement('div');
  20. chatLineDiv.style.paddingLeft = '3px';
  21. chatLineDiv.style.paddingRight = '15px';
  22. if (editorUi.editor.graph.isEnabled())
  23. {
  24. mainDiv.appendChild(chatLineDiv);
  25. }
  26. this.chatArea = document.createElement('div');
  27. this.chatArea.style.backgroundColor = 'white';
  28. this.chatArea.style.overflowX = 'hidden';
  29. this.chatArea.style.overflowY = 'auto';
  30. this.chatArea.style.width = '98%';
  31. this.chatArea.style.resize = 'none';
  32. chatDiv.appendChild(this.chatArea);
  33. this.chatLineArea = document.createElement('textarea');
  34. this.chatLineArea.style.resize = 'none';
  35. this.chatLineArea.rows = 1;
  36. this.chatLineArea.onkeydown = mxUtils.bind(this, function(evt)
  37. {
  38. var key = evt.keyCode || window.event.keyCode;
  39. if (key == 13 && this.chatLineArea.value != '')
  40. {
  41. this.sendMessage();
  42. }
  43. });
  44. this.sendBtn = document.createElement('button');
  45. this.sendBtn.style.cssFloat = 'right';
  46. this.sendBtn.style.styleFloat = 'right';
  47. mxUtils.write(this.sendBtn, mxResources.get('sendMessage'));
  48. mxEvent.addListener(this.sendBtn, 'click', mxUtils.bind(this, function(evt)
  49. {
  50. if (this.chatLineArea.value != '')
  51. {
  52. this.sendMessage();
  53. }
  54. }));
  55. chatLineDiv.appendChild(this.chatLineArea);
  56. chatLineDiv.appendChild(this.sendBtn);
  57. this.window = new mxWindow(title, mainDiv, x, y, width, height, true, true);
  58. this.window.destroyOnClose = false;
  59. this.window.setMaximizable(true);
  60. this.window.setScrollable(true);
  61. this.window.setResizable(true);
  62. this.window.setClosable(true);
  63. this.window.setVisible(true);
  64. this.handleResize();
  65. this.window.addListener(mxEvent.RESIZE, mxUtils.bind(this, this.handleResize));
  66. this.window.addListener(mxEvent.MAXIMIZE, mxUtils.bind(this, this.handleResize));
  67. this.window.addListener(mxEvent.NORMALIZE, mxUtils.bind(this, this.handleResize));
  68. if (this.chatHistory != null)
  69. {
  70. for (var i = Math.max(0, this.chatHistory.length - this.chatHistoryShow); i < this.chatHistory.length; i++)
  71. {
  72. this.updateChatArea(this.chatHistory.get(i));
  73. }
  74. this.chatHistory.addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, mxUtils.bind(this, function(evt)
  75. {
  76. this.updateChatArea(evt.target.get(evt.index))
  77. }));
  78. }
  79. this.doc.addEventListener(gapi.drive.realtime.EventType.COLLABORATOR_JOINED, mxUtils.bind(this, this.collaboratorListener));
  80. this.doc.addEventListener(gapi.drive.realtime.EventType.COLLABORATOR_LEFT, mxUtils.bind(this, this.collaboratorListener));
  81. if (this.chatMap != null)
  82. {
  83. this.chatMap.addEventListener(gapi.drive.realtime.EventType.VALUE_CHANGED, mxUtils.bind(this, function(evt)
  84. {
  85. var map = evt.target;
  86. this.updateChatArea(map.get(evt.property));
  87. }));
  88. }
  89. };
  90. ChatWindow.prototype.window = null;
  91. ChatWindow.prototype.doc = null;
  92. ChatWindow.prototype.chatHistory = null;
  93. ChatWindow.prototype.chatMap = null;
  94. // Shows the last n entries from the chat history on startup
  95. ChatWindow.prototype.chatHistoryShow = 10;
  96. // 0 means keep history forever
  97. ChatWindow.prototype.chatHistorySize = 0;
  98. ChatWindow.prototype.setChatMap = function(chatMap)
  99. {
  100. this.chatMap = chatMap;
  101. };
  102. ChatWindow.prototype.sendMessage = function()
  103. {
  104. try
  105. {
  106. var msgObj = {timestamp: new Date().getTime(), collaboratorColor : this.collabColor, collaboratorName : this.displayName, text : this.chatLineArea.value, version : 1};
  107. this.chatHistory.push(msgObj);
  108. this.chatLineArea.value = '';
  109. //this.chatLineArea.selectionStart = 0;
  110. }
  111. catch (e)
  112. {
  113. this.editorUi.handleError(e);
  114. }
  115. };
  116. ChatWindow.prototype.updateChatArea = function(newMsgObj)
  117. {
  118. var headingElt = '<span style="color : ' + newMsgObj.collaboratorColor + ';">&#x25BA;</span>' + '<b>' + newMsgObj.collaboratorName + '</b>' + ': ';
  119. var newChatLine = headingElt + this.htmlEscape(newMsgObj.text) + '<br>';
  120. this.chatArea.innerHTML += newChatLine;
  121. this.chatArea.scrollTop = this.chatArea.scrollHeight;
  122. };
  123. ChatWindow.prototype.handleResize = function()
  124. {
  125. var elt = this.window.getElement();
  126. var dy = (this.editorUi.editor.graph.isEnabled()) ? 70 : 40;
  127. this.chatArea.style.height = Math.max(0, elt.offsetHeight - dy) + 'px';
  128. this.chatLineArea.style.width = (elt.offsetWidth - this.sendBtn.offsetWidth - 40) + 'px';
  129. };
  130. ChatWindow.prototype.collaboratorListener = function(evt)
  131. {
  132. var msg = null;
  133. if (evt.collaborator.isMe)
  134. {
  135. return;
  136. }
  137. if (evt.type == gapi.drive.realtime.EventType.COLLABORATOR_JOINED)
  138. {
  139. msg = '<span style="color : ' + evt.collaborator.color + ';">&#x25B2</span>' + '<i>' + mxResources.get('chatJoined', [evt.collaborator.displayName]) + '</i>';
  140. }
  141. else if (evt.type == gapi.drive.realtime.EventType.COLLABORATOR_LEFT)
  142. {
  143. msg = '<span style="color : ' + evt.collaborator.color + ';">&#x25BC</span>' + '<i>' + mxResources.get('chatLeft', [evt.collaborator.displayName]) + '</i>';
  144. }
  145. else
  146. {
  147. return;
  148. }
  149. this.chatArea.innerHTML = this.chatArea.innerHTML + msg + '<br>';
  150. this.chatArea.scrollTop = this.chatArea.scrollHeight;
  151. };
  152. ChatWindow.prototype.configCollabInfo = function()
  153. {
  154. var collaboratorsList = this.doc.getCollaborators();
  155. for ( var i = 0; i < collaboratorsList.length; i++)
  156. {
  157. var collaborator = collaboratorsList[i];
  158. if (collaborator.isMe)
  159. {
  160. this.collabColor = collaborator.color;
  161. this.displayName = collaborator.displayName;
  162. }
  163. }
  164. };
  165. ChatWindow.prototype.destroy = function()
  166. {
  167. this.window.destroy();
  168. };
  169. ChatWindow.prototype.htmlEscape = function(string)
  170. {
  171. return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
  172. };