chat.js 15 KB

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