function ResultsTable(id, container, editPlayerCallback, editResultCallback) { this.id = id; this.players = []; this.results = []; this.store = window.sessionStorage; if(!editPlayerCallback) { editPlayerCallback = this._editPlayerCallback; } this.editPlayerCallback = editPlayerCallback; if(!editResultCallback) { editResultCallback = this._editResultCallback; } this.editResultCallback = editResultCallback; 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.store.getItem(this.id+'_players')) { 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; self.editPlayerCallback( originalName, function (result) { if(result == null) { return; } if(result) { self.players[self.players.indexOf(originalName)] = result; } else { var index = self.players.indexOf(originalName); self.players.splice(index, 1); 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._editPlayerCallback = function(name, callback) { var result = prompt('Podaj nowe inicjały gracza. Wyczyść pole, by go usunąć.', name); if(result) { callback(result); } }; ResultsTable.prototype._editResultCallback = function(value, callback) { var result = prompt('Podaj nową wartość:', value); if(result) { callback(result); } }; 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() { self.editResultCallback( self.results[j][i], function (value) { self.results[j][i] = value; self.refresh(); } ); } }(j, i, this); tr.appendChild(td); } this.tbody.appendChild(tr); } } };