if (!String.prototype.encodeHTML) {
String.prototype.encodeHTML = function () {
return this.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, '''); // "
};
}
var transition = {pid: '', port: '8080' };
// BERT Protocol
var players = ["Gabrielo","Mustafa","Alina","Me"];
function statsRow(start_name,i,games) {
var start_score = 200;
var name = template_engine(
'{this.body}',{
x: start_name,
y: 180+25*i,
body: games[i].value[0][0] + " — " + games[i].value[0][1]});
var element1 = svg(name);
document.getElementById('Stat-Right').appendChild(element1);
}
var user_count = 0;
function handle_web_socket(body) {
// console.log(dec(body).value[0][0].value);
switch (dec(body).value[0][0].value) {
case 'roster_item':
var id = dec(body).value[0][1].value;
var names = dec(body).value[0][2].value;
var surnames = dec(body).value[0][3].value;
addOnlineUser(id,names+" "+surnames+ " "+user_count++,'appendChild');
break;
case 'roster_group':
var list = dec(body).value[0][1];
for (var i=0;i';
slot.parentNode.replaceChild(svg(html),slot); }
function rand(lo,hi) { return Math.floor((Math.random()*hi)+lo); }
function loadScene() {
reload("Kakaranet-Scene.svg", "Refined");
//drawSampleCards();
}
function findPlace() {
for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
var e = document.getElementById(slotName(x,y));
if (e.childElementCount == 0) {
var pos = e.attributes['id'].value.split("-")[1].split(",");
console.log("Free");
console.log(pos[0]);
console.log(pos[1]);
return { y: pos[0], x: pos[1] }; } } }
function findCardOnTable(c,v) {
console.log("Find Card " + c + " " + v);
for (var y=1;y<3;y++) for (var x=1;x<15;x++) {
var e = document.getElementById(slotName(x,y));
if (e.childElementCount > 0) {
var value = e.lastChild.textContent;
var col = color.indexOf(e.lastChild.attributes['fill'].value) + 1;
console.log(col + " " + value);
if (c == col && v == value) return { 'x': x, 'y': y }; } }
console.log("Card Not Found");
return ""; }
function loadAppend(file, animation, name) {
loadFile(file, function() {
var slot = document.getElementById(name);
var r = template_engine(localStorage.getItem(file),{'name': animation});
document.getElementById(name).appendChild(svg(r)); }); }
function loadAnimationForButton(a, b) { return loadAppend('templates/ButtonAnimation.svg', a, b); }
// TODO: Rollout Monadic Chain here
loadFile('templates/Card.svg', function() {
loadFile('Kakaranet-Scene.svg', function() {
var a = [{button: "Create", pathes: ["CreateShow", "CreateHide"]},
{button: "Play", pathes: ["PlayShow", "PlayHide"]}];
for (var y=0;y');
var clipPath2 = svg('');
var clipPath3 = svg('');
document.getElementsByTagName('defs').item(0).appendChild(clipPath1);
document.getElementsByTagName('defs').item(0).appendChild(clipPath2);
document.getElementsByTagName('defs').item(0).appendChild(clipPath3);
document.getElementById("Online-List").setAttribute("clip-path","url(#myClip1)");
document.getElementById("Chat").setAttribute("clip-path","url(#myClip2)");
document.getElementById("Online-Chat").setAttribute("clip-path","url(#myClip1)");
document.getElementById("Clip-Path-Left").setAttribute("transform", "translate(0,0)");
document.getElementById("Clip-Path-Right").setAttribute("transform", "translate(0,0)");
document.getElementById("Clip-Path-Left-Chat").setAttribute("transform", "translate(0,0)");
document.getElementById('Player-Statistics').style.display = 'none';
document.getElementById("Right-Bar").setAttribute("fill","skyblue");
document.getElementById("Right-Bar").setAttribute("xmlns:data","Right-Bar");
document.getElementById("Right-Bar").onmouseover = barHover;
document.getElementById("Right-Bar").onmouseout = barHoverOut;
document.getElementById("Left-Bar").onmouseover = onlineHover;
document.getElementById("Left-Bar").onmouseout = onlineHoverOut;
// HTML editors
document.getElementById('onlineChatEdit').setAttribute("contentEditable","true");
document.getElementById('onlineChatEdit').onkeydown = chatEditor;
document.getElementById("onlineChatEdit").style.display = 'none';
document.getElementById('edit').setAttribute("contentEditable","true");
document.getElementById('edit').onkeydown = chatEditor;
document.getElementById('edit').setAttribute("xmlns:data","Chat");
document.getElementById("edit").style.display = '';
// showOnlineList ctor
var onlineListOnClick = ["Online-Users","Online-Users-Pad","Online-Logo",
"722","723","users-online","users-online-text"];
onlineListOnClick.map(
function(x) { document.getElementById(x).onclick = showOnlineList; });
onRightMenuDown();
}, "Refined");
});
var removeChilds = function (e) { var last; while (last = e.lastChild) e.removeChild(last); };
function onPlayerInfo(evt) {
ws.send(enc(tuple(atom('client'),
tuple(atom('stats_action'),bin(document.user),atom('game_okey')))));
}
function onPlayerInfoClose(evt) {
document.getElementById('Player-Statistics').style.display = 'none';
}
function onRightMenu(evt) {
localStorage.clear();
["PlayShow","CreateShow"].map(function (x) {
document.getElementById(x+"-Motion").beginElement(); }); }
function onRightMenuDown(evt) {
["PlayHide","CreateHide"].map(function (x) {
document.getElementById(x+"-Motion").beginElement(); }); }
function slotName1(x,y) { return "1Slot-"+y+","+x; }
function drawSampleCards() {
slotName = slotName1;
for (var i=1;i<15;i++) { place_card(i,rand(1,2),rand(1,4),rand(1,13)); }
slotName = slotNameDef; }
var scrollSensitivity = 0.2;
var scroll_left = 5;
var scroll_left_chat = 5;
var scroll_right = -10000;
function chatMessage(chatName, id, me, string) {
var i=0;
var colors=['#FDFDFD','#DFF1F4'];
var x1 = 7;
var y1 = 0;
var container = chatName == "Chat" ? "Right-Bar" : "Left-Bar";
var hover = chatName == "Chat" ? "barHover" : "onlineHover";
var translate_y = parseFloat(document.getElementById(chatName).getBBox().height);
var x2 = 205;
var textElement = chatText(container,id,me,string);
var dy = translate_y == 0 ? 0 : translate_y + 10;
var html = "";
var messageElement = svg(html);
messageElement.appendChild(textElement);
document.getElementById(chatName).appendChild(messageElement);
create_multiline(textElement);
var y2 = textElement.getBBox().height + 5;
var box = "";
var boxElement = svg(box);
textElement.setAttribute("xmlns:data",container)
messageElement.insertBefore(boxElement,textElement);
boxElement.setAttribute("mouseover",hover+"(evt);");
boxElement.setAttribute("mouseout",hover+"Out(evt);");
textElement.setAttribute("mouseover",hover+"(evt);");
textElement.setAttribute("mouseout",hover+"Out(evt);");
messageElement.setAttribute("onmouseover",hover+"(evt);");
messageElement.setAttribute("onmouseout",hover+"Out(evt);");
// console.log(messageElement);
}
function chatText(container, id, me, string) {
var i = 0;
var colors=['#3B5998'];
var html = "" +
string + "";
// console.log(html);
return svg(html);
}
function mouseWheelHandler(e) {
var leftBar = document.getElementById("Left-Bar");
var rightBar = document.getElementById("Right-Bar");
var leftFill = leftBar.getAttribute("fill");
var rightFill = rightBar.getAttribute("fill");
var leftActive = leftFill == "skyblue";
var rightActive = rightFill == "skyblue";
if (!leftActive && !rightActive) return;
// console.log(leftActive);
var evt = e;
var scroll_dy = evt.detail ? evt.detail * scrollSensitivity : evt.wheelDelta * scrollSensitivity;
var ori = leftActive ? (currentChat == null ? scroll_left : scroll_left ) : scroll_right;
var scroll = parseFloat(scroll_dy) + parseFloat(ori);
var selectedBar = leftActive ? (currentChat == null ? "Online-List" : currentChat) : "Chat";
var selectedClip = leftActive ? (currentChat == null ? "Clip-Path-Left" : "Clip-Path-Left-Chat") : "Clip-Path-Right";
var selectedBarShift = leftActive ? 2 : 857;
var limit = parseFloat(document.getElementById(selectedBar).getBBox().height) - 400;
if (scroll > 5) scroll = 5;
if (scroll < -limit) scroll = -limit;
document.getElementById(selectedClip).setAttribute("transform", "translate(0,"+parseFloat(-scroll)+")");
document.getElementById(selectedBar).setAttribute("transform", "translate("+selectedBarShift+","+(parseFloat(95+scroll))+")");
if (leftActive) scroll_left = scroll; else scroll_right = scroll;
return true;
}
var svgNS = "http://www.w3.org/2000/svg";
function create_multiline(target) {
var text_element = target; // evt.target;
var width = 190; //target.getAttribute("width");
var words = text_element.firstChild.data.split('');
// console.log(words);
// var words = [].concat.apply([],lines.map(function(line) { return line.split(' '); }));;
var start_x = 15; //text_element.getAttribute('x');
text_element.firstChild.data = '';
var tspan_element = document.createElementNS(svgNS, "tspan");
tspan_element.setAttribute("x", start_x);
tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
tspan_element.setAttribute("dy", 18);
var text_node = document.createTextNode(words[0]);
tspan_element.appendChild(text_node);
text_element.appendChild(tspan_element);
for(var i=1; i width || words[i]=="\n") {
if (words[i]=='\n') words[i]="";
tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len);
var tspan_element = document.createElementNS(svgNS, "tspan");
tspan_element.setAttribute("x", start_x);
tspan_element.setAttribute("xmlns:data", text_element.getAttribute("xmlns:data"));
tspan_element.setAttribute("dy", 18);
text_node = document.createTextNode(words[i]);
tspan_element.appendChild(text_node);
text_element.appendChild(tspan_element);
}
}
}
function barHover(evt) { document.getElementById("Right-Bar").setAttribute("fill","skyblue"); }
function barHoverOut(evt) { document.getElementById("Right-Bar").setAttribute("fill","lightblue"); }
function onlineHover(evt) { document.getElementById("Left-Bar").setAttribute("fill","skyblue"); }
function onlineHoverOut(evt) { document.getElementById("Left-Bar").setAttribute("fill","lightblue"); }
function onlineHoverColor(evt) {
onlineHover(evt);
var name = evt.target.getAttribute("xmlns:data");
if (null != name) document.getElementById(name).setAttribute("fill","#FFF687");
}
function onlineHoverOutColor(evt) {
onlineHoverOut(evt);
var name = evt.target.getAttribute("xmlns:data");
if (null != name) document.getElementById(name).setAttribute("fill","#DBEBED");
}
function chatEditor(evt) {
var chatContainer = evt.target.getAttribute("xmlns:data");
if (evt.keyCode == 13 && evt.metaKey == false) {
var e = evt.target; //document.getElementById('edit');
if (e.innerText.trim() != ""){
chatMessage(chatContainer,"100","Maxim",e.innerText.trim().encodeHTML());
e.innerHTML = '';
}
} else if (evt.keyCode == 13 && evt.metaKey == true) {
document.execCommand('insertText',false, '\n');
}
var scroll = -1000000;
if (null != currentChat) left_scroll = scroll;
mouseWheelHandler({'detail':scroll,'wheelDelta':scroll});
}
function shiftTranslate(name,shiftValue) {
var rect = document.getElementById(name);
if (null == rect) return;
var remove = rect.parentNode.parentNode;
var children = document.getElementById("Online-List").childNodes;
var removeIndex = -1;
for(var i = 0; i=removeIndex)
child.setAttribute("transform","translate(0,"+(parseFloat(i)+parseFloat(shiftValue))
*remove.getBBox().height+")");
}
return rect.parentNode.parentNode;
}
function removeOnlineUser(name) {
shiftTranslate(name,-2).remove();
}
function openChat(evt) {
document.getElementById("Online-List").style.display = 'none';
document.getElementById("onlineChatEdit").style.display = '';
var name = evt.target.getAttribute("xmlns:data");
currentChat = "Chat+"+name;
var chatElement = document.getElementById(currentChat);
if (null == chatElement) {
// read from local KVS
var html = '';
document.getElementById("Page-1").appendChild(svg(html));
chatMessage(currentChat,"1","System","You can chat with\n"+name);
} else {
document.getElementById(currentChat).style.display = '';
}
document.getElementById("onlineChatEdit").setAttribute("xmlns:data",currentChat);
scroll_left = -1000000;
onlineHover();
mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
onlineHoverOut();
// document.getElementById("users-online-text").textContent = currentChat;
}
function showOnlineList(evt) {
onlineHover();
mouseWheelHandler({'detail':5,'wheelDelta':5});
onlineHoverOut();
document.getElementById("onlineChatEdit").style.display = 'none';
if (null != currentChat) document.getElementById(currentChat).style.display = 'none';
document.getElementById("Online-List").style.display = '';
currentChat = null;
}
var currentChat = null;
function editorControl(id,left) {
var x = left == "left" ? 0 : 864;
var events = ' onmouseover="barHover(evt);" onmouseout="barHoverOut(evt)" ';
var html = '' +
'Write here some text.
';
var element = svg(html);
return element;
}
function addOnlineUser(name,full_name,insertMode) {
var listElement = document.getElementById("Online-List");
var clickEvent = ' onclick="openChat(evt);" ';
var events = ' onmouseover="onlineHover(evt);" onmouseout="onlineHoverOut(evt);" ' + clickEvent;
var eventsColor = ' onmouseover="onlineHoverColor(evt);" onmouseout="onlineHoverOutColor(evt);" ' + clickEvent;
var color = insertMode == "insertTop" ? "red" : "green";
var y = (insertMode == "insertTop") ? "0" : listElement.getBBox().height;
var html = '' +
'' +
' ' +
'' +
''+full_name+'' +
'Score: 1043 Pos: 13'+
'';
var element = svg(html);
if (insertMode == "insertTop") {
var firstElement = listElement.firstElementChild;
var first = firstElement.firstElementChild.getAttribute("xmlns:data");
shiftTranslate(first,0);
listElement.insertBefore(element,firstElement);
} else listElement.appendChild(element);
}
chatMessage("Chat","1","Maxim2","Joe:\nHello There!".encodeHTML());
chatMessage("Chat","2","Maxim2","Alice:\nYou got new Design. Eh?".encodeHTML());
chatMessage("Chat","3","Maxim","Maxim So:\nThis was made with pure SVG".encodeHTML());
barHover();
mouseWheelHandler({'detail':-100000,'wheelDelta':-100000});
barHoverOut();