chat.js 16 KB

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