From 25cb20db10b00bf033b166f78f90f7d78a4f2590 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Sat, 29 Aug 2015 20:51:07 +0000 Subject: [PATCH] Use callbacks for editing data in ResultsTable --- index.html | 132 ------------------------------------------- 1 files changed, 1 insertions(+), 131 deletions(-) diff --git a/index.html b/index.html index f6f0e9c..a620866 100644 --- a/index.html +++ b/index.html @@ -20,138 +20,8 @@ <div id="overlay"> </div> +<script type="text/javascript" src="bridge_results_table.js"></script> <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(); -- Gitblit v1.9.1