svg.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. if (!String.prototype.encodeHTML) {
  2. String.prototype.encodeHTML = function () {
  3. return this.replace(/&/g, '&')
  4. .replace(/</g, '&lt;')
  5. .replace(/>/g, '&gt;')
  6. .replace(/"/g, '&quot;')
  7. .replace(/'/g, '&apos;'); // "
  8. };
  9. }
  10. var transition = {pid: '', port: '8080' };
  11. // BERT Protocol
  12. var players = ["Gabrielo","Mustafa","Alina","Me"];
  13. function statsRow(start_name,i,games) {
  14. var start_score = 200;
  15. var name = template_engine(
  16. '<tspan xmlns="http://www.w3.org/2000/svg" x="{this.x}" y="{this.y}">{this.body}</tspan>',{
  17. x: start_name,
  18. y: 180+25*i,
  19. body: games[i].value[0][0] + " — " + games[i].value[0][1]});
  20. var element1 = svg(name);
  21. document.getElementById('Stat-Right').appendChild(element1);
  22. }
  23. var user_count = 0;
  24. function handle_web_socket(body) {
  25. // console.log(dec(body).value[0][0].value);
  26. switch (dec(body).value[0][0].value) {
  27. case 'roster_item':
  28. var id = dec(body).value[0][1].value;
  29. var names = dec(body).value[0][2].value;
  30. var surnames = dec(body).value[0][3].value;
  31. addOnlineUser(id,names+" "+surnames+ " "+user_count++,'appendChild');
  32. break;
  33. case 'roster_group':
  34. var list = dec(body).value[0][1];
  35. for (var i=0;i<list.length;i++) {
  36. var item = list[i];
  37. var id = item.value[0][0].value;
  38. var names = item.value[0][1].value;
  39. var surnames = item.value[0][2].value;
  40. addOnlineUser(id,names+" "+surnames+ " "+user_count++,'appendChild');
  41. }
  42. break;
  43. case 'roster_end':
  44. onlineHover();
  45. mouseWheelHandler({'detail':5,'wheelDelta':5});
  46. onlineHoverOut();
  47. document.getElementById("Online-List").style.display = '';
  48. break;
  49. case 'online':
  50. var id = dec(body).value[0][1].value;
  51. var name = dec(body).value[0][2].value;
  52. var surname = dec(body).value[0][3].value;
  53. try{removeOnlineUser(id)}catch(e){}
  54. addOnlineUser(id,name+" "+surname,"insertTop");
  55. break;
  56. case 'chat_message':
  57. var from = dec(body).value[0][1].value;
  58. var to = dec(body).value[0][2].value;
  59. var message = dec(body).value[0][3].value;
  60. chatMessage(currentChat,"1",from,from+":\n"+message.encodeHTML());
  61. onlineHover();
  62. mouseWheelHandler({'detail':-10000,'wheelDelta':-10000});
  63. onlineHoverOut();
  64. break;
  65. case 'offline':
  66. var id = dec(body).value[0][1].value;
  67. var name = dec(body).value[0][2].value;
  68. var surname = dec(body).value[0][3].value;
  69. try{removeOnlineUser(id)}catch(e){}
  70. addOnlineUser(id,name+" "+surname,"appendChild");
  71. break;
  72. case 'online_number':
  73. var number = dec(body).value[0][1];
  74. document.getElementById("722").firstElementChild.textContent = number.toString();
  75. break;
  76. case 'stats_event':
  77. document.getElementById('Player-Statistics').style.display = '';
  78. var games = dec(body).value[0][2];
  79. var reveals = dec(body).value[0][3];
  80. var protocol = dec(body).value[0][4];
  81. removeChilds(document.getElementById('Stat-Left'));
  82. removeChilds(document.getElementById('Stat-Right'));
  83. for (var i=0;i<games.length;i++) { statsRow(24, i,games); }
  84. for (var i=0;i<protocol.length;i++) { statsRow(340,i,protocol); }
  85. // for (var i=0;i<reveals.length;i++) { statsRow(340,i,reveals); }
  86. break;
  87. }
  88. switch (dec(body).value[0][2].value) {
  89. case 'okey_game_info':
  90. var a = dec(body).value[0][3][0].value[0][1];
  91. // console.log("Players: " + a.length);
  92. for (var i=0;i<a.length;i++) {
  93. var c = a[i].value[0][3].value;
  94. var v = a[i].value[0][4].value;
  95. setPlayerName(players[i],c + " " + v);
  96. // console.log("Player: " + c + " " + v);
  97. }
  98. break;
  99. case 'okey_game_started':
  100. var a = dec(body).value[0][3][0].value[0][1];
  101. // console.log("Cards: " + a.length);
  102. for (var i=1;i<=a.length;i++) {
  103. var c = a[i-1].value[0][1];
  104. var v = a[i-1].value[0][2];
  105. // console.log("Card: " + c + " " + v);
  106. place_card(i,rand(1,2),c,v);
  107. }
  108. break;
  109. case 'okey_game_player_state': /// these two messages should be combined
  110. var a = dec(body).value[0][3][3].value[0][1];
  111. // console.log("Cards: " + a.length);
  112. for (var i=1;i<=a.length;i++) {
  113. var c = a[i-1].value[0][1];
  114. var v = a[i-1].value[0][2];
  115. // console.log("Card: " + c + " " + v);
  116. place_card(i,1,c,v);
  117. }
  118. break;
  119. case 'okey_tile_discarded':
  120. console.log(String(dec(body)));
  121. var player = dec(body).value[0][3][0].value[0][1];
  122. var tile = dec(body).value[0][3][1].value[0][1];
  123. if (player.value == document.user) {
  124. console.log("Discard: " + player.value);
  125. var p = findCardOnTable(tile.value[0][1],tile.value[0][2]);
  126. empty_card(p.x,p.y);
  127. }
  128. break;
  129. case 'okey_tile_taken':
  130. console.log(String(dec(body)));
  131. var player = dec(body).value[0][3][0].value[0][1];
  132. var taken = dec(body).value[0][3][2].value[0][1];
  133. if (taken.value != 'null' && player.value == document.user) {
  134. console.log("Taken: " + player.value);
  135. var c = taken.value[0][1];
  136. var v = taken.value[0][2];
  137. var pos = findPlace();
  138. // console.log(pos);
  139. place_card(pos.x,pos.y,c,v);
  140. }
  141. break;
  142. default:
  143. console.log(String(dec(body)));
  144. }
  145. }
  146. // SVG DOM and Template Engine
  147. function template_engine(html, data) {
  148. var re = /{([^}]+)?}/g, code = 'var r=[];', cursor = 0;
  149. var add = function(line,js) {
  150. js? (code += 'r.push(' + line + ');') :
  151. (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");' : ''); // "
  152. return add; }
  153. while(match = re.exec(html)) {
  154. add(html.slice(cursor, match.index))(match[1],true);
  155. cursor = match.index + match[0].length; }
  156. add(html.substr(cursor, html.length - cursor));
  157. code += 'return r.join("");';
  158. return new Function(code.replace(/[\r\t\n]/g, '')).apply(data); }
  159. function reload(file, name2) { var name = name2==null?file:name2;
  160. var slot = document.getElementById(name);
  161. if (slot == null) return;
  162. slot.parentNode.replaceChild(svg(localStorage.getItem(file)),slot);}
  163. function reload_cont(cont,name,element) { reload(name,element); if (null != cont) (cont)(); }
  164. function loadFile(name,cont,element) {
  165. if (localStorage.getItem(name) == null) {
  166. var client = new XMLHttpRequest();
  167. client.open('GET', name, true);
  168. client.onload = function() {
  169. localStorage.setItem(name,client.responseText);
  170. reload_cont(cont,name,element);
  171. }
  172. client.send(); }
  173. else reload_cont(cont,name,element); }
  174. function svg(html) {
  175. return new DOMParser().parseFromString(html, "text/xml").firstChild;
  176. }
  177. function svgBulk() {
  178. var res = [];
  179. var node = new DOMParser().parseFromString(html, "text/xml").firstChild;
  180. for (var i=0;i<node.childnode.length;i++) { var child = childNodes.item(i);
  181. if (child.tagName != undefined) res.push(child); } }
  182. // The Card
  183. //var color = ['#CE290F','#3B5998','#48AF5E','#FFEC00'];
  184. var color = ['#DE3F26','#606060','#48AF5E','#FFC800'];
  185. var slotName = slotNameDef;
  186. function slotNameDef(x,y) { return "Slot-"+y+","+x; }
  187. function card(line,pos,col,v) {
  188. return template_engine(
  189. localStorage.getItem("templates/Card.svg"),
  190. { name: slotName(pos,line),
  191. suit: color[col-1],
  192. value: v,
  193. y: (line-1)*69,
  194. x: (pos-1)*42 }); }
  195. // Game Scene
  196. function setPlayerName(e, playerName) {
  197. var dx = (document.getElementById(e).attributes['fill'].value == "#FFFFFF") ? 65 : 20;
  198. document.getElementById(e).setAttribute("y",27);
  199. document.getElementById(e).setAttribute("x",dx);
  200. document.getElementById(e).textContent = playerName;
  201. // console.log("Bounding Box: " + document.getElementById(e).getBBox().width);
  202. document.getElementById(e+"-Pad").setAttribute('width',
  203. document.getElementById(e).getBBox().width + 45); }
  204. function place_card(x,y,c,v) {
  205. var slot = document.getElementById(slotName(x,y));
  206. slot.parentNode.replaceChild(svg(card(y,x,c,v)),slot); }
  207. function empty_card(x,y) { var slot = document.getElementById(slotName(x,y));
  208. var html = '<g xmlns="http://www.w3.org/2000/svg" id="'+slotName(x,y)+'"/>';
  209. slot.parentNode.replaceChild(svg(html),slot); }
  210. function rand(lo,hi) { return Math.floor((Math.random()*hi)+lo); }
  211. function loadScene() {
  212. reload("Kakaranet-Scene.svg", "Refined");
  213. //drawSampleCards();
  214. }
  215. function findPlace() {
  216. for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
  217. var e = document.getElementById(slotName(x,y));
  218. if (e.childElementCount == 0) {
  219. var pos = e.attributes['id'].value.split("-")[1].split(",");
  220. console.log("Free");
  221. console.log(pos[0]);
  222. console.log(pos[1]);
  223. return { y: pos[0], x: pos[1] }; } } }
  224. function findCardOnTable(c,v) {
  225. console.log("Find Card " + c + " " + v);
  226. for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
  227. var e = document.getElementById(slotName(x,y));
  228. if (e.childElementCount > 0) {
  229. var value = e.lastChild.textContent;
  230. var col = color.indexOf(e.lastChild.attributes['fill'].value) + 1;
  231. console.log(col + " " + value);
  232. if (c == col && v == value) return { 'x': x, 'y': y }; } }
  233. console.log("Card Not Found");
  234. return ""; }
  235. function loadAppend(file, animation, name) {
  236. loadFile(file, function() {
  237. var slot = document.getElementById(name);
  238. var r = template_engine(localStorage.getItem(file),{'name': animation});
  239. document.getElementById(name).appendChild(svg(r)); }); }
  240. function loadAnimationForButton(a, b) { return loadAppend('templates/ButtonAnimation.svg', a, b); }
  241. // TODO: Rollout Monadic Chain here
  242. loadFile('templates/Card.svg', function() {
  243. loadFile('Kakaranet-Scene.svg', function() {
  244. var a = [{button: "Create", pathes: ["CreateShow", "CreateHide"]},
  245. {button: "Play", pathes: ["PlayShow", "PlayHide"]}];
  246. for (var y=0;y<a.length;y++) for (var x=0;x<a[y].pathes.length;x++)
  247. loadAnimationForButton(a[y].pathes[x],a[y].button);
  248. document.getElementById("Right-Menu") .setAttribute('onclick', 'onRightMenu(evt)');
  249. document.getElementById("Play") .setAttribute('onclick', 'onRightMenuDown(evt)');
  250. document.getElementById("Create") .setAttribute('onclick', 'onRightMenuDown(evt)');
  251. document.getElementById("Point-Table").setAttribute('onclick', 'onPlayerInfo(evt)');
  252. document.getElementById("Player-Statistics").setAttribute('onclick', 'onPlayerInfoClose(evt)');
  253. for (var i=1;i<16;i++) { empty_card(i,2); empty_card(i,1); }
  254. document.getElementById('Page-1').addEventListener("mousewheel", mouseWheelHandler, false);
  255. // Setup Clipping ViewPorts
  256. 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>');
  257. 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>');
  258. 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>');
  259. document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
  260. document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
  261. document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
  262. document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
  263. document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
  264. document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
  265. document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
  266. document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
  267. document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
  268. document.getElementById('Player-Statistics').style.display = 'none';
  269. document.getElementById("Right-Bar").setAttribute("fill","skyblue");
  270. document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
  271. document.getElementById("Right-Bar").onmouseover = barHover;
  272. document.getElementById("Right-Bar").onmouseout = barHoverOut;
  273. document.getElementById("Left-Bar").onmouseover = onlineHover;
  274. document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
  275. // HTML editors
  276. document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
  277. document.getElementById('onlineChatEdit').onkeydown = chatEditor;
  278. document.getElementById("onlineChatEdit").style.display = 'none';
  279. document.getElementById('edit').setAttribute("contentEditable","true");
  280. document.getElementById('edit').onkeydown = chatEditor;
  281. document.getElementById('edit').setAttribute("xmlns:data","Chat");
  282. document.getElementById("edit").style.display = '';
  283. // showOnlineList ctor
  284. var onlineListOnClick = ["Online-Users","Online-Users-Pad","Online-Logo",
  285. "722","723","users-online","users-online-text"];
  286. onlineListOnClick.map(
  287. function(x) { document.getElementById(x).onclick = showOnlineList; });
  288. onRightMenuDown();
  289. }, "Refined");
  290. });
  291. var removeChilds = function (e) { var last; while (last = e.lastChild) e.removeChild(last); };
  292. function onPlayerInfo(evt) {
  293. ws.send(enc(tuple(atom('client'),
  294. tuple(atom('stats_action'),bin(document.user),atom('game_okey')))));
  295. }
  296. function onPlayerInfoClose(evt) {
  297. document.getElementById('Player-Statistics').style.display = 'none';
  298. }
  299. function onRightMenu(evt) {
  300. localStorage.clear();
  301. ["PlayShow","CreateShow"].map(function (x) {
  302. document.getElementById(x+"-Motion").beginElement(); }); }
  303. function onRightMenuDown(evt) {
  304. ["PlayHide","CreateHide"].map(function (x) {
  305. document.getElementById(x+"-Motion").beginElement(); }); }
  306. function slotName1(x,y) { return "1Slot-"+y+","+x; }
  307. function drawSampleCards() {
  308. slotName = slotName1;
  309. for (var i=1;i<15;i++) { place_card(i,rand(1,2),rand(1,4),rand(1,13)); }
  310. slotName = slotNameDef; }
  311. var scrollSensitivity = 0.2;
  312. var scroll_left = 5;
  313. var scroll_left_chat = 5;
  314. var scroll_right = -10000;
  315. function chatMessage(chatName, id, me, string) {
  316. var i=0;
  317. var colors=['#FDFDFD','#DFF1F4'];
  318. var x1 = 7;
  319. var y1 = 0;
  320. var container = chatName == "Chat" ? "Right-Bar" : "Left-Bar";
  321. var hover = chatName == "Chat" ? "barHover" : "onlineHover";
  322. var translate_y = parseFloat(document.getElementById(chatName).getBBox().height);
  323. var x2 = 205;
  324. var textElement = chatText(container,id,me,string);
  325. var dy = translate_y == 0 ? 0 : translate_y + 10;
  326. var html = "<g xmlns='http://www.w3.org/2000/svg' " +
  327. "id='Message-"+id+"' transform='translate(0,"+dy+")'></g>";
  328. var messageElement = svg(html);
  329. messageElement.appendChild(textElement);
  330. document.getElementById(chatName).appendChild(messageElement);
  331. create_multiline(textElement);
  332. var y2 = textElement.getBBox().height + 5;
  333. var box = "<path xmlns:data='"+container+"' xmlns='http://www.w3.org/2000/svg' d='M"+x1+","+y1+
  334. " L"+x2+","+y1+
  335. ((me == "Maxim") ?
  336. (" L"+x2+","+parseFloat(y2-7)+
  337. " L"+parseFloat(x2+7)+","+y2+
  338. " L"+x1+","+y2)
  339. :
  340. (" L"+x2+","+y2+
  341. " L"+0+","+y2+
  342. " L"+x1+","+parseFloat(y2-7)))
  343. + " L"+x1+","+y1+"' fill='"+colors[me=="Maxim"?1:0]+"'></path>";
  344. var boxElement = svg(box);
  345. textElement.setAttribute("xmlns:data",container)
  346. messageElement.insertBefore(boxElement,textElement);
  347. boxElement.setAttribute("mouseover",hover+"(evt);");
  348. boxElement.setAttribute("mouseout",hover+"Out(evt);");
  349. textElement.setAttribute("mouseover",hover+"(evt);");
  350. textElement.setAttribute("mouseout",hover+"Out(evt);");
  351. messageElement.setAttribute("onmouseover",hover+"(evt);");
  352. messageElement.setAttribute("onmouseout",hover+"Out(evt);");
  353. // console.log(messageElement);
  354. }
  355. function chatText(container, id, me, string) {
  356. var i = 0;
  357. var colors=['#3B5998'];
  358. var html = "<text xmlns:data='"+container+"' id='ChatText-"+id+"' width='180' " +
  359. " xmlns='http://www.w3.org/2000/svg' "+
  360. " font-family='Exo 2' font-size='16' font-weight='normal' fill='"+colors[i]+"'>" +
  361. string + "</text>";
  362. // console.log(html);
  363. return svg(html);
  364. }
  365. function mouseWheelHandler(e) {
  366. var leftBar = document.getElementById("Left-Bar");
  367. var rightBar = document.getElementById("Right-Bar");
  368. var leftFill = leftBar.getAttribute("fill");
  369. var rightFill = rightBar.getAttribute("fill");
  370. var leftActive = leftFill == "skyblue";
  371. var rightActive = rightFill == "skyblue";
  372. if (!leftActive && !rightActive) return;
  373. // console.log(leftActive);
  374. var evt = e;
  375. var scroll_dy = evt.detail ? evt.detail * scrollSensitivity : evt.wheelDelta * scrollSensitivity;
  376. var ori = leftActive ? (currentChat == null ? scroll_left : scroll_left ) : scroll_right;
  377. var scroll = parseFloat(scroll_dy) + parseFloat(ori);
  378. var selectedBar = leftActive ? (currentChat == null ? "Online-List" : currentChat) : "Chat";
  379. var selectedClip = leftActive ? (currentChat == null ? "Clip-Path-Left" : "Clip-Path-Left-Chat") : "Clip-Path-Right";
  380. var selectedBarShift = leftActive ? 2 : 857;
  381. var limit = parseFloat(document.getElementById(selectedBar).getBBox().height) - 400;
  382. if (scroll > 5) scroll = 5;
  383. if (scroll < -limit) scroll = -limit;
  384. document.getElementById(selectedClip).setAttribute("transform", "translate(0,"+parseFloat(-scroll)+")");
  385. document.getElementById(selectedBar).setAttribute("transform", "translate("+selectedBarShift+","+(parseFloat(95+scroll))+")");
  386. if (leftActive) scroll_left = scroll; else scroll_right = scroll;
  387. return true;
  388. }
  389. var svgNS = "http://www.w3.org/2000/svg";
  390. function create_multiline(target) {
  391. var text_element = target; // evt.target;
  392. var width = 190; //target.getAttribute("width");
  393. var words = text_element.firstChild.data.split('');
  394. // console.log(words);
  395. // var words = [].concat.apply([],lines.map(function(line) { return line.split(' '); }));;
  396. var start_x = 15; //text_element.getAttribute('x');
  397. text_element.firstChild.data = '';
  398. var tspan_element = document.createElementNS(svgNS, "tspan");
  399. tspan_element.setAttribute("x", start_x);
  400. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  401. tspan_element.setAttribute("dy", 18);
  402. var text_node = document.createTextNode(words[0]);
  403. tspan_element.appendChild(text_node);
  404. text_element.appendChild(tspan_element);
  405. for(var i=1; i<words.length; i++) {
  406. if (words[i]=="") continue;
  407. var len = tspan_element.firstChild.data.length;
  408. tspan_element.firstChild.data += words[i];
  409. if (tspan_element.getComputedTextLength() > width || words[i]=="\n") {
  410. if (words[i]=='\n') words[i]="";
  411. tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len);
  412. var tspan_element = document.createElementNS(svgNS, "tspan");
  413. tspan_element.setAttribute("x", start_x);
  414. tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
  415. tspan_element.setAttribute("dy", 18);
  416. text_node = document.createTextNode(words[i]);
  417. tspan_element.appendChild(text_node);
  418. text_element.appendChild(tspan_element);
  419. }
  420. }
  421. }
  422. function barHover(evt) { document.getElementById("Right-Bar").setAttribute("fill","skyblue"); }
  423. function barHoverOut(evt) { document.getElementById("Right-Bar").setAttribute("fill","lightblue"); }
  424. function onlineHover(evt) { document.getElementById("Left-Bar").setAttribute("fill","skyblue"); }
  425. function onlineHoverOut(evt) { document.getElementById("Left-Bar").setAttribute("fill","lightblue"); }
  426. function onlineHoverColor(evt) {
  427. onlineHover(evt);
  428. var name = evt.target.getAttribute("xmlns:data");
  429. if (null != name) document.getElementById(name).setAttribute("fill","#FFF687");
  430. }
  431. function onlineHoverOutColor(evt) {
  432. onlineHoverOut(evt);
  433. var name = evt.target.getAttribute("xmlns:data");
  434. if (null != name) document.getElementById(name).setAttribute("fill","#DBEBED");
  435. }
  436. function chatEditor(evt) {
  437. var chatContainer = evt.target.getAttribute("xmlns:data");
  438. if (evt.keyCode == 13 && evt.metaKey == false) {
  439. var e = evt.target; //document.getElementById('edit');
  440. console.log(e);
  441. if (e.innerText.trim() != ""){
  442. var text = e.innerText.trim().encodeHTML();
  443. chatMessage(chatContainer,"100","Maxim",text);
  444. // if (null != currentChat)
  445. ws.send(enc(tuple(atom('client'),
  446. tuple(atom('message'),bin(document.user),bin(chatContainer.substr(5)),bin(text)))));
  447. e.innerHTML = '';
  448. }
  449. } else if (evt.keyCode == 13 && evt.metaKey == true) {
  450. document.execCommand('insertText',false, '\n');
  451. }
  452. var scroll = -1000000;
  453. if (null != currentChat) left_scroll = scroll;
  454. mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
  455. }
  456. function shiftTranslate(name,shiftValue) {
  457. var rect = document.getElementById(name);
  458. if (null == rect) return;
  459. var remove = rect.parentNode.parentNode;
  460. var children = document.getElementById("Online-List").childNodes;
  461. var removeIndex = -1;
  462. for(var i = 0; i<children.length; ++i) {
  463. var child = children[i];
  464. if (child == remove) removeIndex = i;
  465. if (removeIndex != -1 && i>=removeIndex)
  466. child.setAttribute("transform","translate(0,"+(parseFloat(i)+parseFloat(shiftValue))
  467. *remove.getBBox().height+")");
  468. }
  469. return rect.parentNode.parentNode;
  470. }
  471. function removeOnlineUser(name) {
  472. shiftTranslate(name,-2).remove();
  473. }
  474. function openChat(evt) {
  475. document.getElementById("Online-List").style.display = 'none';
  476. document.getElementById("onlineChatEdit").style.display = '';
  477. var name = evt.target.getAttribute("xmlns:data");
  478. currentChat = "Chat+"+name;
  479. var chatElement = document.getElementById(currentChat);
  480. if (null == chatElement) {
  481. // read from local KVS
  482. 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>';
  483. document.getElementById("Page-1").appendChild(svg(html));
  484. chatMessage(currentChat,"1","System","You can chat with\n"+name);
  485. } else {
  486. document.getElementById(currentChat).style.display = '';
  487. }
  488. document.getElementById("onlineChatEdit").setAttribute("xmlns:data",currentChat);
  489. scroll_left = -1000000;
  490. onlineHover();
  491. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  492. onlineHoverOut();
  493. // document.getElementById("users-online-text").textContent = currentChat;
  494. }
  495. function showOnlineList(evt) {
  496. document.getElementById("onlineChatEdit").style.display = 'none';
  497. if (null != currentChat) {
  498. document.getElementById(currentChat).style.display = 'none';
  499. }
  500. document.getElementById("Online-List").style.display = '';
  501. currentChat = null;
  502. scroll_left = 0;
  503. onlineHover();
  504. mouseWheelHandler({'detail':scroll_left,'wheelDelta':scroll_left});
  505. onlineHoverOut();
  506. }
  507. var currentChat = null;
  508. function editorControl(id,left) {
  509. var x = left == "left" ? 0 : 864;
  510. var events = ' onmouseover="barHover(evt);" onmouseout="barHoverOut(evt)" ';
  511. var html = '<foreignObject xmlns="http://www.w3.org/2000/svg" x="'+x+'" y="504" width="198" height="120" ' + events + '>' +
  512. '<div id="'+id+'" style="padding:4px;background-color:#FFF687;color:#3B5998;'+
  513. 'font-family:"Exo 2";font-size:16px;contentEditable="true" '+
  514. 'xmlns="http://www.w3.org/1999/xhtml">Write here some text.</div></foreignObject>';
  515. var element = svg(html);
  516. return element;
  517. }
  518. function addOnlineUser(name,full_name,insertMode) {
  519. var listElement = document.getElementById("Online-List");
  520. var clickEvent = ' onclick="openChat(evt);" ';
  521. var events = ' onmouseover="onlineHover(evt);" onmouseout="onlineHoverOut(evt);" ' + clickEvent;
  522. var eventsColor = ' onmouseover="onlineHoverColor(evt);" onmouseout="onlineHoverOutColor(evt);" ' + clickEvent;
  523. var color = insertMode == "insertTop" ? "red" : "green";
  524. var y = (insertMode == "insertTop") ? "0" : listElement.getBBox().height;
  525. var html = '<g xmlns="http://www.w3.org/2000/svg" height="60" transform="translate(0, '+y+')">' +
  526. '<g xmlns:data="'+name+'" fill="#DBEBED" '+eventsColor+'>' +
  527. ' <rect cursor="pointer" xmlns:data="'+name+'" fill="#DBEBED" id="'+name+'" x="10" y="0" width="196" height="48" ' +'></rect></g>' +
  528. '<text xmlns:data="'+name+'" '+eventsColor+' '+
  529. 'font-family="Exo 2" font-size="18" cursor="pointer" font-weight="normal" line-spacing="18"'+
  530. ' fill="#3B5998">' +
  531. '<tspan xmlns:data="'+name+'" font-weight="normal" fill="'+color+'" x="19" y="22">'+full_name+'</tspan>' +
  532. '<tspan xmlns:data="'+name+'" font-size="14" x="19" y="40">Score: 1043 Pos: 13</tspan></text>'+
  533. '<rect '+
  534. ' x="10" y="48" width="196" height="8"></rect></g>';
  535. var element = svg(html);
  536. if (insertMode == "insertTop") {
  537. var firstElement = listElement.firstElementChild;
  538. var first = firstElement.firstElementChild.getAttribute("xmlns:data");
  539. shiftTranslate(first,0);
  540. listElement.insertBefore(element,firstElement);
  541. } else listElement.appendChild(element);
  542. }
  543. chatMessage("Chat","1","Maxim2","Joe:\nHello There!".encodeHTML());
  544. chatMessage("Chat","2","Maxim2","Alice:\nYou got new Design. Eh?".encodeHTML());
  545. chatMessage("Chat","3","Maxim","Maxim So:\nThis was made with pure SVG".encodeHTML());
  546. barHover();
  547. mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
  548. barHoverOut();