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