chat.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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,score,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" ' +'>'+full_name+'</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">'+i18n("Score")+': '+score+' </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. var settings = document.getElementById("Settings");
  161. document.getElementById("Kakaranet-Scene").insertBefore(svg(html),settings);
  162. document.getElementById(chatName).style.display = 'none';
  163. }
  164. function openChat(evt) {
  165. if (leftFlag) $("#Left-Menu").trigger("click");
  166. document.getElementById("Online-List").style.display = 'none';
  167. document.getElementById("onlineChatEdit").style.display = '';
  168. var name = evt.target.getAttribute("xmlns:data");
  169. var full_name = $("#Online-List").find("#"+name)[0].textContent;
  170. currentChat = "Chat+"+name;
  171. var chatElement = document.getElementById(currentChat);
  172. if (null == chatElement) {
  173. // read from local KVS
  174. createChat(currentChat);
  175. chatMessage(currentChat,"1","System",full_name+" "+i18n("PrivateChat"));
  176. }
  177. document.getElementById(currentChat).style.display = '';
  178. document.getElementById("onlineChatEdit").setAttribute("xmlns:data",currentChat);
  179. scroll_left = -1000000;
  180. onlineHover();
  181. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  182. onlineHoverOut();
  183. }
  184. function create_multiline(target) {
  185. var text_element = target; // evt.target;
  186. var width = 190; //target.getAttribute("width");
  187. var words = text_element.firstChild.data.split('');
  188. // console.log(words);
  189. // var words = [].concat.apply([],lines.map(function(line) { return line.split(' '); }));;
  190. var start_x = 15; //text_element.getAttribute('x');
  191. text_element.firstChild.data = '';
  192. var tspan_element = document.createElementNS(svgNS, "tspan");
  193. tspan_element.setAttribute("x", start_x);
  194. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  195. tspan_element.setAttribute("dy", 18);
  196. var text_node = document.createTextNode(words[0]);
  197. tspan_element.appendChild(text_node);
  198. text_element.appendChild(tspan_element);
  199. for(var i=1; i<words.length; i++) {
  200. if (words[i]=="") continue;
  201. var len = tspan_element.firstChild.data.length;
  202. tspan_element.firstChild.data += words[i];
  203. if (tspan_element.getComputedTextLength() > width || words[i]=="\n") {
  204. if (words[i]=='\n') words[i]="";
  205. tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len);
  206. var tspan_element = document.createElementNS(svgNS, "tspan");
  207. tspan_element.setAttribute("x", start_x);
  208. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  209. tspan_element.setAttribute("dy", 18);
  210. text_node = document.createTextNode(words[i]);
  211. tspan_element.appendChild(text_node);
  212. text_element.appendChild(tspan_element);
  213. }
  214. }
  215. }
  216. function initChat()
  217. {
  218. var inGameChat = '<g id="Chat" y="0" clip-path="url(#myClip2)" transform="translate(857.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  219. 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" />';
  220. 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" />';
  221. var page = document.getElementById("Kakaranet-Scene");
  222. var settings = document.getElementById("Settings");
  223. page.insertBefore(svg(inGameChat),settings);
  224. page.insertBefore(svg(onlineList),settings);
  225. page.insertBefore(svg(onlineChat),settings);
  226. 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>');
  227. 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>');
  228. 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>');
  229. document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
  230. document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
  231. document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
  232. document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
  233. document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
  234. document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
  235. document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
  236. document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
  237. document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
  238. document.getElementById("Right-Bar").setAttribute("fill","lightblue");
  239. document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
  240. document.getElementById("Left-Bar").setAttribute("fill","lightblue");
  241. document.getElementById("Left-Bar").setAttribute("xmlns:data","Left-Bar");
  242. document.getElementById("Right-Bar").onmouseover = barHover;
  243. document.getElementById("Right-Bar").onmouseout = barHoverOut;
  244. document.getElementById("Left-Bar").onmouseover = onlineHover;
  245. document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
  246. document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
  247. document.getElementById('onlineChatEdit').onkeydown = chatEditor;
  248. document.getElementById('onlineChatEdit').onfocus = chatEditorClearContent;
  249. document.getElementById("onlineChatEdit").style.display = 'none';
  250. document.getElementById('edit').setAttribute("contentEditable","true");
  251. document.getElementById('edit').onkeydown = chatEditor;
  252. document.getElementById('edit').onfocus = chatEditorClearContent;
  253. document.getElementById("edit").style.display = '';
  254. document.getElementById('Page-1').addEventListener(mousewheelevt, mouseWheelHandler, false);
  255. }
  256. var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
  257. function chatEditorClearContent(evt) {
  258. var e = evt.target;
  259. var chatContainer = e.getAttribute("xmlns:data");
  260. e.innerHTML = '';
  261. }
  262. function chatEditor(evt) {
  263. var chatContainer = evt.target.getAttribute("xmlns:data");
  264. if (evt.keyCode == 13 && evt.altKey == false) {
  265. var e = evt.target;
  266. var text = e.textContent == null?"":e.textContent.trim();
  267. if (text != ""){
  268. text = text.encodeHTML();
  269. if ("Chat" != chatContainer)
  270. {
  271. chatMessage(chatContainer,"100",document.user,text);
  272. ws.send(enc(tuple(atom('client'),
  273. tuple(atom('message'),
  274. bin(document.user),
  275. bin(document.names),
  276. bin(chatContainer.substr(5)),
  277. utf8toByteArray(text)))));
  278. }
  279. else
  280. {
  281. chatMessage(chatContainer,"100",document.user,text);
  282. ws.send(enc(tuple(atom('client'),
  283. tuple(atom('chat'),
  284. scope.gameId,
  285. bin(document.user),
  286. bin(document.names),
  287. utf8toByteArray(text)))));
  288. }
  289. e.innerHTML = '';
  290. }
  291. } else if (evt.keyCode == 13 && evt.altKey == true) {
  292. // document.execCommand('insertText',false, '\n');
  293. }
  294. var scroll = -100000;
  295. if (chatContainer == "Chat") {
  296. right_scroll = scroll;
  297. barHover();
  298. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  299. barHoverOut();
  300. } else {
  301. left_scroll = scroll;
  302. onlineHover();
  303. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  304. onlineHoverOut();
  305. }
  306. }