chat.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = 10;
  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. if (!String.prototype.entag) {
  116. String.prototype.entag = function () {
  117. return (this[0]!="_"?"_":"")+this.replace(/\./g, '-dot-').replace(/@/g, '-at-'); }; }
  118. if (!String.prototype.detag) {
  119. String.prototype.detag = function () { return this.substr(1).replace(/-at-/g,'@').replace(/-dot-/g,'.').replace(/tag-/,''); }; }
  120. function addOnlineUser(name,full_name,score,insertMode) {
  121. name = name.entag();
  122. var listElement = document.getElementById("Online-List");
  123. var clickEvent = ' onclick="openChat(evt);" ';
  124. var events = ' onmouseover="onlineHover(evt);" onmouseout="onlineHoverOut(evt);" ' + clickEvent;
  125. var eventsColor = ' onmouseover="onlineHoverColor(evt);" onmouseout="onlineHoverOutColor(evt);" ' + clickEvent;
  126. var color = insertMode == "insertTop" ? "red" : "green";
  127. var y = (insertMode == "insertTop") ? 0 : listElement.getBBox().height;
  128. var html = '<g xmlns="http://www.w3.org/2000/svg" height="60" transform="translate(0, '+y+')">' +
  129. '<g xmlns:data="'+name+'" fill="#DBEBED" '+eventsColor+'>' +
  130. ' <rect cursor="pointer" xmlns:data="'+name+'" fill="#DBEBED" id="'+name+'" x="10" y="0" width="196" height="48" ' +'>'+full_name+'</rect></g>' +
  131. '<text xmlns:data="'+name+'" '+eventsColor+' '+
  132. 'font-family="Exo 2" font-size="18" cursor="pointer" font-weight="normal" line-spacing="18"'+
  133. ' fill="#3B5998">' +
  134. '<tspan xmlns:data="'+name+'" font-weight="normal" fill="'+color+'" x="19" y="22">'+utf8decode(full_name)+'</tspan>' +
  135. '<tspan xmlns:data="'+name+'" font-size="14" x="19" y="40">'+i18n("Score")+': '+score+' </tspan></text>'+
  136. '<rect '+
  137. ' x="10" y="48" width="196" height="8"></rect></g>';
  138. var element = svg(html);
  139. if (insertMode == "insertTop") {
  140. var firstElement = listElement.firstElementChild;
  141. if (null == firstElement) listElement.appendChild(element); else {
  142. var first = firstElement.firstElementChild.getAttribute("xmlns:data");
  143. shiftTranslate(first, 1);
  144. listElement.insertBefore(element,firstElement);
  145. }
  146. } else listElement.appendChild(element);
  147. }
  148. function shiftTranslate(name,shiftValue) {
  149. var rect = document.getElementById(name);
  150. if (null == rect) return;
  151. var remove = rect.parentNode.parentNode;
  152. var children = document.getElementById("Online-List").childNodes;
  153. var removeIndex = -1;
  154. for(var i = 0; i<children.length; i++) {
  155. var child = children[i];
  156. if (child == remove) removeIndex = i;
  157. if (removeIndex != -1 && i>=removeIndex)
  158. child.setAttribute("transform","translate(0,"+(parseFloat(i)+parseFloat(shiftValue))
  159. *remove.getBBox().height+")");
  160. }
  161. return rect.parentNode.parentNode;
  162. }
  163. function removeOnlineUser(name) { shiftTranslate(name.entag(),-1).remove(); }
  164. function createChat(chatName) {
  165. 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>';
  166. var settings = document.getElementById("Settings");
  167. document.getElementById("Kakaranet-Scene").insertBefore(svg(html),settings);
  168. document.getElementById(chatName).style.display = 'none';
  169. }
  170. function openChat(evt) {
  171. if (leftFlag) $("#Left-Menu").trigger("click");
  172. document.getElementById("Online-List").style.display = 'none';
  173. document.getElementById("onlineChatEdit").style.display = '';
  174. var name = evt.target.getAttribute("xmlns:data");
  175. var full_name = $("#Online-List").find("#"+name)[0].textContent;
  176. currentChat = "Chat+"+name;
  177. var chatElement = document.getElementById(currentChat);
  178. if (null == chatElement) {
  179. // read from local KVS
  180. createChat(currentChat);
  181. chatMessage(currentChat,"1","System",utf8decode(full_name)+" "+i18n("PrivateChat"));
  182. }
  183. document.getElementById(currentChat).style.display = '';
  184. document.getElementById("onlineChatEdit").setAttribute("xmlns:data",currentChat);
  185. scroll_left = -1000000;
  186. onlineHover();
  187. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  188. onlineHoverOut();
  189. }
  190. function create_multiline(target) {
  191. var text_element = target; // evt.target;
  192. var width = 188; //target.getAttribute("width");
  193. var words = text_element.firstChild.data.split('');
  194. // console.log(words);
  195. // var words = [].concat.apply([],lines.map(function(line) { return line.split(' '); }));;
  196. var start_x = 15; //text_element.getAttribute('x');
  197. text_element.firstChild.data = '';
  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. var text_node = document.createTextNode(words[0]);
  203. tspan_element.appendChild(text_node);
  204. text_element.appendChild(tspan_element);
  205. for(var i=1; i<words.length; i++) {
  206. if (words[i]=="") continue;
  207. var len = tspan_element.firstChild.data.length;
  208. tspan_element.firstChild.data += words[i];
  209. if (tspan_element.getComputedTextLength() > width || words[i]=="\n") {
  210. if (words[i]=='\n') words[i]="";
  211. tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len);
  212. var tspan_element = document.createElementNS(svgNS, "tspan");
  213. tspan_element.setAttribute("x", start_x);
  214. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  215. tspan_element.setAttribute("dy", 18);
  216. text_node = document.createTextNode(words[i]);
  217. tspan_element.appendChild(text_node);
  218. text_element.appendChild(tspan_element);
  219. }
  220. }
  221. }
  222. function initChat()
  223. {
  224. var inGameChat = '<g id="Chat" y="0" clip-path="url(#myClip2)" transform="translate(857.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  225. var onlineList = '<g id="Online-List" x="10" y="0" clip-path="url(#myClip1)" transform="translate(7.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  226. var onlineChat = '<g id="Online-Chat" x="10" y="0" clip-path="url(#myClip3)" transform="translate(7.000000, 107.000000)" xmlns="http://www.w3.org/2000/svg" />';
  227. var page = document.getElementById("Kakaranet-Scene");
  228. var settings = document.getElementById("Settings");
  229. page.insertBefore(svg(inGameChat),settings);
  230. page.insertBefore(svg(onlineList),settings);
  231. page.insertBefore(svg(onlineChat),settings);
  232. 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>');
  233. 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>');
  234. 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>');
  235. document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
  236. document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
  237. document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
  238. document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
  239. document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
  240. document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
  241. document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
  242. document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
  243. document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
  244. document.getElementById("Right-Bar").setAttribute("fill","lightblue");
  245. document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
  246. document.getElementById("Left-Bar").setAttribute("fill","lightblue");
  247. document.getElementById("Left-Bar").setAttribute("xmlns:data","Left-Bar");
  248. document.getElementById("Right-Bar").onmouseover = barHover;
  249. document.getElementById("Right-Bar").onmouseout = barHoverOut;
  250. document.getElementById("Left-Bar").onmouseover = onlineHover;
  251. document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
  252. try {
  253. document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
  254. document.getElementById('onlineChatEdit').onkeydown = chatEditor;
  255. document.getElementById('onlineChatEdit').onfocus = chatEditorClearContent;
  256. document.getElementById("onlineChatEdit").style.display = 'none';
  257. document.getElementById('edit').setAttribute("contentEditable","true");
  258. document.getElementById('edit').onkeydown = chatEditor;
  259. document.getElementById('edit').onfocus = chatEditorClearContent;
  260. document.getElementById("edit").style.display = '';
  261. } catch (e) { console.log("foreignObjects missing from SVG.txt"); }
  262. document.getElementById('Page-1').addEventListener(mousewheelevt, mouseWheelHandler, false);
  263. }
  264. var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
  265. function chatEditorClearContent(evt) {
  266. var e = evt.target;
  267. var chatContainer = e.getAttribute("xmlns:data");
  268. e.innerHTML = '';
  269. }
  270. function chatEditor(evt) {
  271. var chatContainer = evt.target.getAttribute("xmlns:data");
  272. if (evt.keyCode == 13 && evt.altKey == false) {
  273. var e = evt.target;
  274. var text = e.textContent == null?"":e.textContent.trim();
  275. if (text != ""){
  276. text = text.encodeHTML();
  277. if ("Chat" != chatContainer)
  278. {
  279. chatMessage(chatContainer,"100",document.user,text);
  280. ws.send(enc(tuple(atom('client'),
  281. tuple(atom('message'),
  282. bin(document.user),
  283. bin(document.names),
  284. bin(chatContainer.substr(5).detag()),
  285. utf8toByteArray(text)))));
  286. }
  287. else
  288. {
  289. chatMessage(chatContainer,"100",document.user,text);
  290. ws.send(enc(tuple(atom('client'),
  291. tuple(atom('chat'),
  292. scope.gameId,
  293. bin(document.user),
  294. bin(document.names),
  295. utf8toByteArray(text)))));
  296. }
  297. e.innerHTML = '';
  298. }
  299. } else if (evt.keyCode == 13 && evt.altKey == true) {
  300. // document.execCommand('insertText',false, '\n');
  301. }
  302. var scroll = -100000;
  303. if (chatContainer == "Chat") {
  304. right_scroll = scroll;
  305. barHover();
  306. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  307. barHoverOut();
  308. } else {
  309. left_scroll = scroll;
  310. onlineHover();
  311. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  312. onlineHoverOut();
  313. }
  314. }