commit | author | age
|
450db0
|
1 |
function ResultsTable(id, container) { |
JK |
2 |
this.id = id; |
|
3 |
this.players = []; |
|
4 |
this.results = []; |
|
5 |
this.store = window.sessionStorage; |
|
6 |
|
|
7 |
container.appendChild(this._createTable()); |
|
8 |
} |
|
9 |
ResultsTable.prototype._createTable = function() { |
|
10 |
this.table = document.createElement('table'); |
|
11 |
this.table.className = 'results'; |
|
12 |
this.thead = document.createElement('thead'); |
|
13 |
this.tbody = document.createElement('tbody'); |
|
14 |
this.tplayers = document.createElement('tr'); |
|
15 |
this.tresults = document.createElement('tr'); |
|
16 |
|
|
17 |
this.table.appendChild(this.thead); |
|
18 |
this.table.appendChild(this.tbody); |
|
19 |
this.thead.appendChild(this.tplayers); |
|
20 |
this.thead.appendChild(this.tresults); |
|
21 |
|
|
22 |
return this.table; |
|
23 |
}; |
|
24 |
ResultsTable.prototype.load = function() { |
|
25 |
if(this.store && this.store.getItem(this.id+'_players')) { |
|
26 |
this.players = JSON.parse(this.store.getItem(this.id+'_players')); |
|
27 |
this.results = JSON.parse(this.store.getItem(this.id+'_results')); |
|
28 |
this.refresh(); |
|
29 |
} |
|
30 |
}; |
|
31 |
ResultsTable.prototype.save = function() { |
|
32 |
if(this.store) { |
|
33 |
this.store.setItem(this.id+'_players', JSON.stringify(this.players)); |
|
34 |
this.store.setItem(this.id+'_results', JSON.stringify(this.results)); |
|
35 |
} |
|
36 |
}; |
|
37 |
ResultsTable.prototype.clear = function() { |
|
38 |
this.players = []; |
|
39 |
this.results = []; |
|
40 |
this.refresh(); |
|
41 |
if(this.store) { |
|
42 |
this.store.removeItem(this.id+'_players'); |
|
43 |
this.store.removeItem(this.id+'_results'); |
|
44 |
} |
|
45 |
}; |
|
46 |
ResultsTable.prototype.addPlayer = function(name) { |
|
47 |
var results = (this.results[0] && this.results[0].length) || 0; |
|
48 |
results = new Array(results); |
|
49 |
for(var i = 0; i < results.length; i++) { |
|
50 |
results[i] = 'X'; |
|
51 |
} |
|
52 |
|
|
53 |
this.players.push(name); |
|
54 |
this.results.push(results); |
|
55 |
this.refresh(); |
|
56 |
}; |
|
57 |
ResultsTable.prototype._createPlayerTh = function(name) { |
|
58 |
var th = document.createElement('th'); |
|
59 |
th.innerText = name; |
|
60 |
th.onclick = function(self) { return function() { |
|
61 |
var originalName = th.innerText; |
|
62 |
var result = prompt('Podaj nowe inicjały gracza. Wyczyść pole, by go usunąć.', name); |
|
63 |
|
|
64 |
if(result == null) { |
|
65 |
return; |
|
66 |
} |
|
67 |
|
|
68 |
if(result) { |
|
69 |
self.players[self.players.indexOf(originalName)] = result; |
|
70 |
th.innerText = result; |
|
71 |
} |
|
72 |
else |
|
73 |
{ |
|
74 |
var index = self.players.indexOf(originalName); |
|
75 |
self.players = self.players.splice(index, 1); |
|
76 |
self.results = self.results.splice(index, 1); |
|
77 |
self.refresh(); |
|
78 |
} |
|
79 |
} }(this); |
|
80 |
return th; |
|
81 |
}; |
|
82 |
ResultsTable.prototype.addResult = function(result) { |
|
83 |
for(var i = 0; i < result.length; i++) { |
|
84 |
this.results[i].push(result[i]); |
|
85 |
} |
|
86 |
this.refresh(); |
|
87 |
}; |
|
88 |
ResultsTable.prototype.refresh = function() { |
|
89 |
this.save(); |
|
90 |
|
|
91 |
while(this.tplayers.lastChild) { |
|
92 |
this.tplayers.removeChild(this.tplayers.lastChild); |
|
93 |
} |
|
94 |
for(var i = 0; i < this.players.length; i++) { |
|
95 |
this.tplayers.appendChild(this._createPlayerTh(this.players[i])); |
|
96 |
} |
|
97 |
|
|
98 |
while(this.tresults.lastChild) { |
|
99 |
this.tresults.removeChild(this.tresults.lastChild); |
|
100 |
} |
|
101 |
for(var i = 0; i < this.results.length; i++) { |
|
102 |
var td = document.createElement('th'); |
|
103 |
td.innerText = this.results[i].reduce(function(a, b) { return (parseInt(a, 10) || 0)+(parseInt(b, 10) || 0); }, 0); |
|
104 |
this.tresults.appendChild(td); |
|
105 |
} |
|
106 |
|
|
107 |
while(this.tbody.lastChild) { |
|
108 |
this.tbody.removeChild(this.tbody.lastChild); |
|
109 |
} |
|
110 |
if(this.results.length > 0) { |
|
111 |
for(var i = 0; i < this.results[0].length; i++) { |
|
112 |
var tr = document.createElement('tr'); |
|
113 |
|
|
114 |
for(var j = 0; j < this.results.length; j++) { |
|
115 |
var td = document.createElement('td'); |
|
116 |
td.innerText = this.results[j][i]; |
|
117 |
td.onclick = function(j, i, self) { return function() { |
|
118 |
var result = prompt('Podaj nową wartość:', self.results[j][i]); |
|
119 |
if(result != null) { |
|
120 |
self.results[j][i] = result; |
|
121 |
self.refresh(); |
|
122 |
} |
|
123 |
} }(j, i, this); |
|
124 |
tr.appendChild(td); |
|
125 |
} |
|
126 |
|
|
127 |
this.tbody.appendChild(tr); |
|
128 |
} |
|
129 |
} |
|
130 |
}; |