chat.js 14 KB

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