Obliczenia brydżowe (HTML)
Jacek Kowalski
2015-12-30 0f086d87e349c63380b0687f7704b5ce480a160f
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);
0f086d 102     callback(result);
25cb20 103 };
JK 104 ResultsTable.prototype._editResultCallback = function(value, callback) {
105     var result = prompt('Podaj nową wartość:', value);
0f086d 106     callback(result);
450db0 107 };
JK 108 ResultsTable.prototype.refresh = function() {
109     this.save();
110     
111     while(this.tplayers.lastChild) {
112         this.tplayers.removeChild(this.tplayers.lastChild);
113     }
114     for(var i = 0; i < this.players.length; i++) {
115         this.tplayers.appendChild(this._createPlayerTh(this.players[i]));
116     }
117     
118     while(this.tresults.lastChild) {
119         this.tresults.removeChild(this.tresults.lastChild);
120     }
121     for(var i = 0; i < this.results.length; i++) {
122         var td = document.createElement('th');
25cb20 123         td.innerText = this.results[i].reduce(
JK 124             function(a, b) { return (parseInt(a, 10) || 0)+(parseInt(b, 10) || 0); }, 0
125         );
450db0 126         this.tresults.appendChild(td);
JK 127     }
128     
129     while(this.tbody.lastChild) {
130         this.tbody.removeChild(this.tbody.lastChild);
131     }
132     if(this.results.length > 0) {
133         for(var i = 0; i < this.results[0].length; i++) {
134             var tr = document.createElement('tr');
135             
136             for(var j = 0; j < this.results.length; j++) {
137                 var td = document.createElement('td');
138                 td.innerText = this.results[j][i];
139                 td.onclick = function(j, i, self) { return function() {
25cb20 140                     self.editResultCallback(
JK 141                         self.results[j][i],
142                         function (value) {
0f086d 143                             if(value == null) return;
25cb20 144                             self.results[j][i] = value;
JK 145                             self.refresh();
146                         }
147                     );
450db0 148                 } }(j, i, this);
JK 149                 tr.appendChild(td);
150             }
151             
152             this.tbody.appendChild(tr);
153         }
154     }
25cb20 155 };