chat.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Online User Chat and In-Game Chat
  2. var scrollSensitivity = 0.2;
  3. var scroll_left = 5;
  4. var scroll_left_chat = 5;
  5. var scroll_right = -10000;
  6. var currentChat = null;
  7. var user_count = 0;
  8. function barHover(evt) { document.getElementById("Right-Bar").setAttribute("fill","skyblue"); }
  9. function barHoverOut(evt) { document.getElementById("Right-Bar").setAttribute("fill","lightblue"); }
  10. function onlineHover(evt) { document.getElementById("Left-Bar").setAttribute("fill","skyblue"); }
  11. function onlineHoverOut(evt) { document.getElementById("Left-Bar").setAttribute("fill","lightblue"); }
  12. function onlineHoverColor(evt) {
  13. onlineHover(evt);
  14. var name = evt.target.getAttribute("xmlns:data");
  15. if (null != name) document.getElementById(name).setAttribute("fill","#FFF687");
  16. }
  17. function onlineHoverOutColor(evt) {
  18. onlineHoverOut(evt);
  19. var name = evt.target.getAttribute("xmlns:data");
  20. if (null != name) document.getElementById(name).setAttribute("fill","#DBEBED");
  21. }
  22. function mouseWheelHandler(e) {
  23. var leftBar = document.getElementById("Left-Bar");
  24. var rightBar = document.getElementById("Right-Bar");
  25. var leftFill = leftBar.getAttribute("fill");
  26. var rightFill = rightBar.getAttribute("fill");
  27. var leftActive = leftFill == "skyblue";
  28. var rightActive = rightFill == "skyblue";
  29. if (!leftActive && !rightActive) return;
  30. var evt = e;
  31. var scroll_dy = evt.detail ? evt.detail * scrollSensitivity : evt.wheelDelta * scrollSensitivity;
  32. var ori = leftActive ? (currentChat == null ? scroll_left : scroll_left ) : scroll_right;
  33. var scroll = parseFloat(scroll_dy) + parseFloat(ori);
  34. var selectedBar = leftActive ? (currentChat == null ? "Online-List" : currentChat) : "Chat";
  35. var selectedClip = leftActive ? (currentChat == null ? "Clip-Path-Left" : "Clip-Path-Left-Chat") : "Clip-Path-Right";
  36. var selectedBarShift = leftActive ? 0 : 857;
  37. var limit = parseFloat(document.getElementById(selectedBar).getBBox().height) - 400;
  38. if (scroll > 5) scroll = 5;
  39. if (scroll < -limit) scroll = -limit;
  40. document.getElementById(selectedClip).setAttribute("transform", "translate(0,"+parseFloat(-scroll)+")");
  41. document.getElementById(selectedBar).setAttribute("transform", "translate("+selectedBarShift+","+(parseFloat(95+scroll))+")");
  42. if (leftActive) scroll_left = scroll; else scroll_right = scroll;
  43. return true;
  44. }
  45. function chatMessage(chatName, id, me, string) {
  46. var i=0;
  47. var colors=['#FDFDFD','#DFF1F4'];
  48. var x1 = 7;
  49. var y1 = 0;
  50. var container = chatName == "Chat" ? "Right-Bar" : "Left-Bar";
  51. var hover = chatName == "Chat" ? "barHover" : "onlineHover";
  52. var translate_y = parseFloat(document.getElementById(chatName).getBBox().height);
  53. var x2 = 205;
  54. var textElement = chatText(container,id,me,string);
  55. var dy = translate_y == 0 ? 0 : translate_y + 10;
  56. var html = "<g xmlns='http://www.w3.org/2000/svg' " +
  57. "id='Message-"+id+"' transform='translate(0,"+dy+")'></g>";
  58. var messageElement = svg(html);
  59. messageElement.appendChild(textElement);
  60. document.getElementById(chatName).appendChild(messageElement);
  61. create_multiline(textElement);
  62. var y2 = textElement.getBBox().height + 5;
  63. var box = "<path xmlns:data='"+container+"' xmlns='http://www.w3.org/2000/svg' d='M"+x1+","+y1+
  64. " L"+x2+","+y1+
  65. ((me == document.user) ?
  66. (" L"+x2+","+parseFloat(y2-7)+
  67. " L"+parseFloat(x2+7)+","+y2+
  68. " L"+x1+","+y2)
  69. :
  70. (" L"+x2+","+y2+
  71. " L"+0+","+y2+
  72. " L"+x1+","+parseFloat(y2-7)))
  73. + " L"+x1+","+y1+"' fill='"+colors[me==document.user?1:0]+"'></path>";
  74. var boxElement = svg(box);
  75. textElement.setAttribute("xmlns:data",container)
  76. messageElement.insertBefore(boxElement,textElement);
  77. boxElement.setAttribute("mouseover",hover+"(evt);");
  78. boxElement.setAttribute("mouseout",hover+"Out(evt);");
  79. textElement.setAttribute("mouseover",hover+"(evt);");
  80. textElement.setAttribute("mouseout",hover+"Out(evt);");
  81. messageElement.setAttribute("onmouseover",hover+"(evt);");
  82. messageElement.setAttribute("onmouseout",hover+"Out(evt);");
  83. }
  84. function chatText(container, id, me, string) {
  85. var i = 0;
  86. var colors=['#3B5998'];
  87. var html = "<text xmlns:data='"+container+"' id='ChatText-"+id+"' width='180' " +
  88. " xmlns='http://www.w3.org/2000/svg' "+
  89. " font-family='Exo 2' font-size='16' font-weight='normal' fill='"+colors[i]+"'>" +
  90. string + "</text>";
  91. return svg(html);
  92. }
  93. function showOnlineList(evt)
  94. {
  95. document.getElementById("onlineChatEdit").style.display = 'none';
  96. if (null != currentChat) { document.getElementById(currentChat).style.display = 'none'; }
  97. document.getElementById("Online-List").style.display = '';
  98. currentChat = null;
  99. scroll_left = 5;
  100. onlineHover();
  101. mouseWheelHandler({'detail':scroll_left,'wheelDelta':scroll_left});
  102. onlineHoverOut();
  103. }
  104. if (!String.prototype.encodeHTML) {
  105. String.prototype.encodeHTML = function () {
  106. return this.replace(/&/g, '&amp;')
  107. .replace(/</g, '&lt;')
  108. .replace(/>/g, '&gt;')
  109. .replace(/"/g, '&quot;')
  110. .replace(/'/g, '&apos;'); // "
  111. };
  112. }
  113. function addOnlineUser(name,full_name,insertMode) {
  114. var listElement = document.getElementById("Online-List");
  115. var clickEvent = ' onclick="openChat(evt);" ';
  116. var events = ' onmouseover="onlineHover(evt);" onmouseout="onlineHoverOut(evt);" ' + clickEvent;
  117. var eventsColor = ' onmouseover="onlineHoverColor(evt);" onmouseout="onlineHoverOutColor(evt);" ' + clickEvent;
  118. var color = insertMode == "insertTop" ? "red" : "green";
  119. var y = (insertMode == "insertTop") ? 0 : listElement.getBBox().height;
  120. var html = '<g xmlns="http://www.w3.org/2000/svg" height="60" transform="translate(0, '+y+')">' +
  121. '<g xmlns:data="'+name+'" fill="#DBEBED" '+eventsColor+'>' +
  122. ' <rect cursor="pointer" xmlns:data="'+name+'" fill="#DBEBED" id="'+name+'" x="10" y="0" width="196" height="48" ' +'></rect></g>' +
  123. '<text xmlns:data="'+name+'" '+eventsColor+' '+
  124. 'font-family="Exo 2" font-size="18" cursor="pointer" font-weight="normal" line-spacing="18"'+
  125. ' fill="#3B5998">' +
  126. '<tspan xmlns:data="'+name+'" font-weight="normal" fill="'+color+'" x="19" y="22">'+full_name+'</tspan>' +
  127. '<tspan xmlns:data="'+name+'" font-size="14" x="19" y="40">Score: 1043 Pos: 13</tspan></text>'+
  128. '<rect '+
  129. ' x="10" y="48" width="196" height="8"></rect></g>';
  130. var element = svg(html);
  131. if (insertMode == "insertTop") {
  132. var firstElement = listElement.firstElementChild;
  133. if (null == firstElement) listElement.appendChild(element); else {
  134. var first = firstElement.firstElementChild.getAttribute("xmlns:data");
  135. shiftTranslate(first, 1);
  136. listElement.insertBefore(element,firstElement);
  137. }
  138. } else listElement.appendChild(element);
  139. }
  140. function shiftTranslate(name,shiftValue) {
  141. var rect = document.getElementById(name);
  142. if (null == rect) return;
  143. var remove = rect.parentNode.parentNode;
  144. var children = document.getElementById("Online-List").childNodes;
  145. var removeIndex = -1;
  146. for(var i = 0; i<children.length; i++) {
  147. var child = children[i];
  148. if (child == remove) removeIndex = i;
  149. if (removeIndex != -1 && i>=removeIndex)
  150. child.setAttribute("transform","translate(0,"+(parseFloat(i)+parseFloat(shiftValue))
  151. *remove.getBBox().height+")");
  152. }
  153. return rect.parentNode.parentNode;
  154. }
  155. function removeOnlineUser(name) { shiftTranslate(name,-1).remove(); }
  156. function openChat(evt) {
  157. document.getElementById("Online-List").style.display = 'none';
  158. document.getElementById("onlineChatEdit").style.display = '';
  159. var name = evt.target.getAttribute("xmlns:data");
  160. currentChat = "Chat+"+name;
  161. var chatElement = document.getElementById(currentChat);
  162. if (null == chatElement) {
  163. // read from local KVS
  164. var html = '<g xmlns="http://www.w3.org/2000/svg" id="'+currentChat+'" y="0" clip-path="url(#myClip3)" transform="translate(1.000000, 107.000000)"></g>';
  165. document.getElementById("Page-1").appendChild(svg(html));
  166. chatMessage(currentChat,"1","System","You can chat with\n"+name);
  167. } else {
  168. document.getElementById(currentChat).style.display = '';
  169. }
  170. document.getElementById("onlineChatEdit").setAttribute("xmlns:data",currentChat);
  171. scroll_left = -1000000;
  172. onlineHover();
  173. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  174. onlineHoverOut();
  175. }
  176. function create_multiline(target) {
  177. var text_element = target; // evt.target;
  178. var width = 190; //target.getAttribute("width");
  179. var words = text_element.firstChild.data.split('');
  180. // console.log(words);
  181. // var words = [].concat.apply([],lines.map(function(line) { return line.split(' '); }));;
  182. var start_x = 15; //text_element.getAttribute('x');
  183. text_element.firstChild.data = '';
  184. var tspan_element = document.createElementNS(svgNS, "tspan");
  185. tspan_element.setAttribute("x", start_x);
  186. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  187. tspan_element.setAttribute("dy", 18);
  188. var text_node = document.createTextNode(words[0]);
  189. tspan_element.appendChild(text_node);
  190. text_element.appendChild(tspan_element);
  191. for(var i=1; i<words.length; i++) {
  192. if (words[i]=="") continue;
  193. var len = tspan_element.firstChild.data.length;
  194. tspan_element.firstChild.data += words[i];
  195. if (tspan_element.getComputedTextLength() > width || words[i]=="\n") {
  196. if (words[i]=='\n') words[i]="";
  197. tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len);
  198. var tspan_element = document.createElementNS(svgNS, "tspan");
  199. tspan_element.setAttribute("x", start_x);
  200. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  201. tspan_element.setAttribute("dy", 18);
  202. text_node = document.createTextNode(words[i]);
  203. tspan_element.appendChild(text_node);
  204. text_element.appendChild(tspan_element);
  205. }
  206. }
  207. }
  208. function initChat()
  209. {
  210. var inGameChat = '<g id="Chat" y="0" clip-path="url(#myClip2)" transform="translate(857.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  211. var onlineList = '<g id="Online-List" y="0" clip-path="url(#myClip1)" transform="translate(1.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  212. var onlineChat = '<g id="Online-Chat" y="0" clip-path="url(#myClip3)" transform="translate(1.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  213. var page = document.getElementById("Kakaranet-12-maxim");
  214. var settings = document.getElementById("Settings");
  215. page.insertBefore(svg(inGameChat),settings);
  216. page.insertBefore(svg(onlineList),settings);
  217. page.insertBefore(svg(onlineChat),settings);
  218. var clipPath1 = svg('<clipPath id="myClip1"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Left" x="0" y="0" width="216" height="400"/></clipPath>');
  219. var clipPath2 = svg('<clipPath id="myClip2"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Right" x="0" y="0" width="216" height="400"/></clipPath>');
  220. var clipPath3 = svg('<clipPath id="myClip3"><rect xmlns="http://www.w3.org/2000/svg" id="Clip-Path-Left-Chat" x="0" y="0" width="216" height="400"/></clipPath>');
  221. document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
  222. document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
  223. document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
  224. document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
  225. document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
  226. document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
  227. document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
  228. document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
  229. document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
  230. document.getElementById("Right-Bar").setAttribute("fill","lightblue");
  231. document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
  232. document.getElementById("Left-Bar").setAttribute("fill","lightblue");
  233. document.getElementById("Left-Bar").setAttribute("xmlns:data","Left-Bar");
  234. document.getElementById("Right-Bar").onmouseover = barHover;
  235. document.getElementById("Right-Bar").onmouseout = barHoverOut;
  236. document.getElementById("Left-Bar").onmouseover = onlineHover;
  237. document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
  238. document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
  239. document.getElementById('onlineChatEdit').onkeydown = chatEditor;
  240. document.getElementById("onlineChatEdit").style.display = 'none';
  241. document.getElementById('edit').setAttribute("contentEditable","true");
  242. document.getElementById('edit').onkeydown = chatEditor;
  243. document.getElementById("edit").style.display = '';
  244. document.getElementById('Page-1').addEventListener("mousewheel", mouseWheelHandler, false);
  245. }
  246. function chatEditor(evt) {
  247. var chatContainer = evt.target.getAttribute("xmlns:data");
  248. if (evt.keyCode == 13 && evt.altKey == false) {
  249. var e = evt.target;
  250. if (e.innerText.trim() != ""){
  251. var text = e.innerText.trim().encodeHTML();
  252. console.log("OutputMessage " + utf8toByteArray(text));
  253. chatMessage(chatContainer,"100",document.user,text);
  254. ws.send(enc(tuple(atom('client'),
  255. tuple(atom('message'),
  256. bin(document.user),
  257. bin(document.names),
  258. bin(chatContainer.substr(5)),
  259. utf8toByteArray(text)))));
  260. e.innerHTML = '';
  261. }
  262. } else if (evt.keyCode == 13 && evt.altKey == true) {
  263. // document.execCommand('insertText',false, '\n');
  264. }
  265. var scroll = -100000;
  266. if (null != currentChat) { left_scroll = scroll; }
  267. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  268. }