Obliczenia brydżowe (HTML)
Jacek Kowalski
2015-08-16 450db08cf003e4524e956b4cee7c95fcee4a21d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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.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;
        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);
        }
    }
};