<!DOCTYPE html>
|
<html>
|
<head>
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
<meta name="viewport" content="width=device-width, user-scalable=no" />
|
<title>Wyniki brydżowe</title>
|
<link rel="stylesheet" href="bridge.css" type="text/css" />
|
</head>
|
<body>
|
|
<p>
|
<button onclick="addResult()">Dodaj wynik</button>
|
<button onclick="addPlayer()">Dodaj gracza</button>
|
<button onclick="clearAll()">Wyczyść</button>
|
</p>
|
|
<div id="results">
|
</div>
|
|
<div id="overlay">
|
</div>
|
|
<script type="text/javascript">
|
function ResultsTable(id, container) {
|
this.id = id;
|
this.players = [];
|
this.results = [];
|
this.store = window.sessionStorage;
|
|
container.appendChild(this._createTable());
|
}
|
ResultsTable.prototype._createTable = function() {
|
this.table = document.createElement('table');
|
this.table.className = 'results';
|
this.thead = document.createElement('thead');
|
this.tbody = document.createElement('tbody');
|
this.tplayers = document.createElement('tr');
|
this.tresults = document.createElement('tr');
|
|
this.table.appendChild(this.thead);
|
this.table.appendChild(this.tbody);
|
this.thead.appendChild(this.tplayers);
|
this.thead.appendChild(this.tresults);
|
|
return this.table;
|
};
|
ResultsTable.prototype.load = function() {
|
if(this.store) {
|
this.players = JSON.parse(this.store.getItem(this.id+'_players'));
|
this.results = JSON.parse(this.store.getItem(this.id+'_results'));
|
this.refresh();
|
}
|
};
|
ResultsTable.prototype.save = function() {
|
if(this.store) {
|
this.store.setItem(this.id+'_players', JSON.stringify(this.players));
|
this.store.setItem(this.id+'_results', JSON.stringify(this.results));
|
}
|
};
|
ResultsTable.prototype.clear = function() {
|
this.players = [];
|
this.results = [];
|
this.refresh();
|
if(this.store) {
|
this.store.removeItem(this.id+'_players');
|
this.store.removeItem(this.id+'_results');
|
}
|
};
|
ResultsTable.prototype.addPlayer = function(name) {
|
var results = (this.results[0] && this.results[0].length) || 0;
|
results = new Array(results);
|
for(var i = 0; i < results.length; i++) {
|
results[i] = 'X';
|
}
|
|
this.players.push(name);
|
this.results.push(results);
|
this.refresh();
|
};
|
ResultsTable.prototype._createPlayerTh = function(name) {
|
var th = document.createElement('th');
|
th.innerText = name;
|
th.onclick = function(self) { return function() {
|
var originalName = th.innerText;
|
var result = prompt('Podaj nowe inicjały gracza. Wyczyść pole, by go usunąć.', name);
|
|
if(result == null) {
|
return;
|
}
|
|
if(result) {
|
self.players[self.players.indexOf(originalName)] = result;
|
th.innerText = result;
|
}
|
else
|
{
|
var index = self.players.indexOf(originalName);
|
self.players = self.players.splice(index, 1);
|
self.results = self.results.splice(index, 1);
|
self.refresh();
|
}
|
} }(this);
|
return th;
|
};
|
ResultsTable.prototype.addResult = function(result) {
|
for(var i = 0; i < result.length; i++) {
|
this.results[i].push(result[i]);
|
}
|
this.refresh();
|
};
|
ResultsTable.prototype.refresh = function() {
|
this.save();
|
|
while(this.tplayers.lastChild) {
|
this.tplayers.removeChild(this.tplayers.lastChild);
|
}
|
for(var i = 0; i < this.players.length; i++) {
|
this.tplayers.appendChild(this._createPlayerTh(this.players[i]));
|
}
|
|
while(this.tresults.lastChild) {
|
this.tresults.removeChild(this.tresults.lastChild);
|
}
|
for(var i = 0; i < this.results.length; i++) {
|
var td = document.createElement('th');
|
td.innerText = this.results[i].reduce(function(a, b) { return (parseInt(a, 10) || 0)+(parseInt(b, 10) || 0); }, 0);
|
this.tresults.appendChild(td);
|
}
|
|
while(this.tbody.lastChild) {
|
this.tbody.removeChild(this.tbody.lastChild);
|
}
|
if(this.results.length > 0) {
|
for(var i = 0; i < this.results[0].length; i++) {
|
var tr = document.createElement('tr');
|
|
for(var j = 0; j < this.results.length; j++) {
|
var td = document.createElement('td');
|
td.innerText = this.results[j][i];
|
td.onclick = function(j, i, self) { return function() {
|
var result = prompt('Podaj nową wartość:', self.results[j][i]);
|
if(result != null) {
|
self.results[j][i] = result;
|
self.refresh();
|
}
|
} }(j, i, this);
|
tr.appendChild(td);
|
}
|
|
this.tbody.appendChild(tr);
|
}
|
}
|
};
|
|
var resultsTable = new ResultsTable('html', document.getElementById('results'));
|
resultsTable.load();
|
|
function addPlayer() {
|
var result = prompt('Podaj inicjały gracza');
|
if(result) {
|
resultsTable.addPlayer(result);
|
}
|
}
|
|
function addResult() {
|
if(resultsTable.players.length < 4) {
|
alert('Dodaj przynajmniej 4 graczy.');
|
return;
|
}
|
window.open('compute.html');
|
}
|
|
function clearAll() {
|
resultsTable.clear();
|
}
|
|
function saveResult(result, p1, p2, p3, p4) {
|
var lista = document.getElementById('overlay');
|
|
if(p1 == null || p2 == null || p3 == null || p4 == null) {
|
while(lista.lastChild) {
|
lista.removeChild(lista.lastChild);
|
}
|
|
var selectable = [];
|
lista.appendChild(document.createElement('br'));
|
for(var i = 0; i < resultsTable.players.length; i++) {
|
if(p1 == i || p2 == i || p3 == i || p4 == i) {
|
continue;
|
}
|
selectable.push(i);
|
}
|
if(p3 == null && p4 == null && selectable.length == 2) {
|
return saveResult(result, p1, p2, selectable[0], selectable[1]);
|
}
|
|
var p = document.createElement('p');
|
if(p1 == null) {
|
p.appendChild(document.createTextNode('Rozgrywający 1:'));
|
} else if(p2 == null) {
|
p.appendChild(document.createTextNode('Rozgrywający 2:'));
|
} else if(p3 == null) {
|
p.appendChild(document.createTextNode('Przeciwnik 1:'));
|
} else {
|
p.appendChild(document.createTextNode('Przeciwnik 2:'));
|
}
|
lista.appendChild(p);
|
for(var j = 0; j < selectable.length; j++) {
|
var i = selectable[j];
|
var button = document.createElement('button');
|
button.innerText = resultsTable.players[i];
|
button.onclick = (function (j) { return function() {
|
if(p1 == null) {
|
saveResult(result, j, null, null, null);
|
} else if(p2 == null) {
|
saveResult(result, p1, j, null, null);
|
} else if(p3 == null) {
|
saveResult(result, p1, p2, j, null);
|
} else {
|
saveResult(result, p1, p2, p3, j);
|
}
|
} })(i);
|
lista.appendChild(button);
|
}
|
|
button = document.createElement('button');
|
button.innerText = 'Anuluj';
|
button.onclick = function() {
|
lista.style.display = 'none';
|
}
|
|
lista.appendChild(button);
|
lista.style.display = 'block';
|
} else {
|
lista.style.display = 'none';
|
|
var results = [];
|
for(var i = 0; i < resultsTable.players.length; i++) {
|
if(p1 == i || p2 == i) {
|
if(result.dla == 0) {
|
results.push(result.IMP);
|
} else {
|
results.push('-');
|
}
|
} else if(p3 == i || p4 == i) {
|
if(result.dla != 0) {
|
results.push(result.IMP);
|
} else {
|
results.push('-');
|
}
|
} else {
|
results.push('X');
|
}
|
}
|
|
resultsTable.addResult(results);
|
}
|
}
|
|
function handler(event) {
|
var result = event.data;
|
|
setTimeout(function() {
|
saveResult(result, null, null, null, null);
|
}, 1);
|
}
|
|
window.addEventListener('message', handler, false);
|
</script>
|
|
</body>
|
</html>
|