From 450db08cf003e4524e956b4cee7c95fcee4a21d9 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 16 Aug 2015 22:00:37 +0000
Subject: [PATCH] Extract JS objects to separate files
---
compute.html | 209 -----------------
index.html | 132 ----------
bridge_compute.js | 207 +++++++++++++++++
bridge_results_table.js | 130 ++++++++++
4 files changed, 339 insertions(+), 339 deletions(-)
diff --git a/bridge_compute.js b/bridge_compute.js
new file mode 100644
index 0000000..155d9e8
--- /dev/null
+++ b/bridge_compute.js
@@ -0,0 +1,207 @@
+BrydzLicz = {
+ lewy: [
+ [0, 20, 40, 60, 80, 100, 120, 140], // młodsze
+ [0, 30, 60, 90, 120, 150, 180, 210], // starsze
+ [0, 40, 70, 100, 130, 160, 190, 220] // bez atu
+ ],
+
+ wpadki: [
+ [ // przed partią
+ [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650], // normalnie
+ [0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500], // kontra
+ [0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000] // rekontra
+ ],
+ [ // po partii
+ [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300], // normalnie
+ [0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800], // kontra
+ [0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600] // rekontra
+ ]
+ ],
+
+ PC: [
+ [ // MY: przed, WY: przed partią
+ [-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400],
+ // MY: przed, WY: po partii
+ [-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400]
+ ],
+ [ // MY: po, WY: przed partią
+ [-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100],
+ // MY: po, WY: po partii
+ [-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100]
+ ]
+ ],
+
+ IMP_t: [0, 20, 50, 90, 130, 170, 220, 270, 320, 370, 430, 500, 600, 750, 900, 1100, 1300, 1500, 1750, 2000, 2250, 2500, 3000, 3500, 4000, 9999999],
+
+ procenty_punkty: [
+ [0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990, 1020, 1400, 1440, 1520, 1530, 9999999], // przed
+ [0, 50, 90, 120, 150, 180, 210, 500, 600, 630, 660, 690, 720, 810, 1370, 1390, 1430, 1440, 1470, 1700, 2000, 2220, 2230, 9999999] //po
+ ],
+
+ procenty_PC: [0, 6, 10, 16, 21, 25, 31, 35, 9999999],
+
+ procenty_t: [
+ [-1, -1, -1, 50, 44, 26, 8, 0],
+ [83, 74, 65, 56, 47, 29, 11, 0],
+ [86, 77, 68, 59, 50, 32, 14, 0],
+ [89, 80, 71, 62, 53, 35, 17, 0],
+ [92, 83, 74, 65, 56, 38, 20, 2],
+ [95, 86, 77, 68, 59, 41, 23, 5],
+ [98, 89, 80, 71, 62, 44, 26, 8],
+ [100, 92, 83, 74, 65, 47, 29, 11],
+ [100, 95, 86, 77, 68, 50, 32, 14],
+ [100, 98, 89, 80, 71, 53, 35, 17],
+ [100, 100, 92, 83, 74, 56, 38, 20],
+ [100, 100, 95, 86, 77, 59, 41, 23],
+ [100, 100, 98, 89, 80, 62, 44, 26],
+ [100, 100, 100, 92, 83, 65, 47, 29],
+ [100, 100, 100, 95, 86, 68, 50, 32],
+ [100, 100, 100, 98, 89, 71, 53, 35],
+ [100, 100, 100, 100, 92, 74, 56, 38],
+ [100, 100, 100, 100, 95, 77, 59, 41],
+ [100, 100, 100, 100, 98, 80, 62, 44],
+ [100, 100, 100, 100, 100, 83, 65, 47],
+ [100, 100, 100, 100, 100, 86, 68, 50],
+ [100, 100, 100, 100, 100, 89, 71, 53],
+ [100, 100, 100, 100, 100, 100, 95, 90]
+ ],
+
+ IMP: function(punkty) {
+ var i = 0;
+ while( this.IMP_t[i+1] <= punkty ) {
+ i++;
+ }
+ return i;
+ },
+
+ procenty: function(PC, punkty, partia) {
+ var punkty_i = 0;
+ var PC_i = 0;
+
+ while(this.procenty_punkty[partia][punkty_i+1] <= punkty) {
+ punkty_i++;
+ }
+
+ while(this.procenty_PC[PC_i+1] <= PC) {
+ PC_i++;
+ }
+
+ return this.procenty_t[punkty_i][PC_i];
+ },
+
+ policz: function(kontrakt, kolor, kontra, partia, PC, lewy) {
+ var wynik = {};
+
+ var my_partia = (partia / 2) >> 0;
+ var wy_partia = partia % 2;
+
+ // 4 pasy
+ if( kontrakt == 0 ) {
+ wynik.dla = 1;
+ wynik.punkty_przed = 0;
+ wynik.punkty = this.PC[my_partia][wy_partia][PC];
+ wynik.IMP = this.IMP(wynik.punkty);
+ wynik.procent = 100-this.procenty(PC, 0, my_partia);
+
+ return wynik;
+ }
+
+
+ // Wynik w punktach
+ var wynik_punkty = 0;
+ // Punkty za lewy kontraktowe
+ var wynik_lewy = 0;
+ // Wynik w punktach, uwzględniając ilość PC
+ var wynik_punkty_PC = 0;
+
+ // Ilość nadróbek
+ var nadrobki = lewy - 6 - kontrakt;
+
+ // Mnożnik przy kontrze
+ var mnoznik = kontra+1;
+ if( mnoznik == 3 ) {
+ mnoznik = 4;
+ }
+
+ // Kontrakt ugrany
+ if( nadrobki >= 0 ) {
+ // "Nadróbki: jak za lewy"
+ if( mnoznik == 1 ) {
+ wynik_lewy = this.lewy[kolor][kontrakt];
+ wynik_punkty = this.lewy[kolor][lewy - 6];
+ }
+ // Kontra lub rekontra
+ else
+ {
+ wynik_lewy = this.lewy[kolor][kontrakt] * mnoznik;
+ wynik_punkty = wynik_lewy;
+
+ // Za wygranie z kontrą
+ wynik_punkty += 25 * mnoznik;
+
+ // Za nadróbki
+ wynik_punkty += nadrobki * (my_partia == 0 ? 50 : 100) * mnoznik;
+ }
+
+ if(kontrakt == 6) {
+ // Za szlemika
+ wynik_punkty += (my_partia == 0 ? 500 : 750);
+ }
+ else if(kontrakt == 7) {
+ // Za szlema
+ wynik_punkty += (my_partia == 0 ? 1000 : 1500);
+ }
+
+ if(wynik_lewy >= 100) {
+ // Za partię
+ wynik_punkty += (my_partia == 0 ? 300 : 500);
+ }
+ else
+ {
+ // Za częściówkę
+ wynik_punkty += 50;
+ }
+ }
+ // Wpadka
+ else
+ {
+ // Punkty za wpadkę
+ wynik_punkty = -1 * this.wpadki[my_partia][kontra][-1 * nadrobki];
+ }
+
+ // Uwzględniamy ilość punktów, którą trzeba było zdobyć
+ wynik_punkty_PC = wynik_punkty - this.PC[my_partia][wy_partia][PC];
+
+ // Zapis dla przeciwników
+ if( wynik_punkty_PC < 0 ) {
+ wynik.dla = 1;
+ wynik.punkty_przed = -1 * wynik_punkty;
+ wynik.punkty = -1 * wynik_punkty_PC;
+ wynik.IMP = this.IMP(wynik.punkty);
+ if( wynik_punkty < 0 ) {
+ wynik.procent = this.procenty(40 - PC, -1 * wynik_punkty, wy_partia);
+ }
+ else
+ {
+ wynik.procent = 100 - this.procenty(PC, wynik_punkty, my_partia);
+ }
+ }
+ // Zapis dla rozgrywających
+ else
+ {
+ wynik.dla = 0;
+ wynik.punkty_przed = wynik_punkty;
+ wynik.punkty = wynik_punkty_PC;
+ wynik.IMP = this.IMP(wynik.punkty);
+ if( wynik_punkty > 0 ) {
+ wynik.procent = this.procenty(PC, wynik_punkty, my_partia);
+ }
+ else
+ {
+ wynik.procent = 100 - this.procenty(40 - PC, -1 * wynik_punkty, wy_partia);
+ }
+ }
+
+ return wynik;
+ }
+};
\ No newline at end of file
diff --git a/bridge_results_table.js b/bridge_results_table.js
new file mode 100644
index 0000000..4c83ac6
--- /dev/null
+++ b/bridge_results_table.js
@@ -0,0 +1,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);
+ }
+ }
+};
\ No newline at end of file
diff --git a/compute.html b/compute.html
index 74baa6d..3a33f38 100644
--- a/compute.html
+++ b/compute.html
@@ -49,215 +49,8 @@
<p id="result"></p>
</form>
+<script type="text/javascript" src="bridge_compute.js"></script>
<script type="text/javascript">
-BrydzLicz = {
- lewy: [
- [0, 20, 40, 60, 80, 100, 120, 140], // młodsze
- [0, 30, 60, 90, 120, 150, 180, 210], // starsze
- [0, 40, 70, 100, 130, 160, 190, 220] // bez atu
- ],
-
- wpadki: [
- [ // przed partią
- [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650], // normalnie
- [0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500], // kontra
- [0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000] // rekontra
- ],
- [ // po partii
- [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300], // normalnie
- [0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800], // kontra
- [0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600] // rekontra
- ]
- ],
-
- PC: [
- [ // MY: przed, WY: przed partią
- [-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400],
- // MY: przed, WY: po partii
- [-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400]
- ],
- [ // MY: po, WY: przed partią
- [-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100],
- // MY: po, WY: po partii
- [-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100]
- ]
- ],
-
- IMP_t: [0, 20, 50, 90, 130, 170, 220, 270, 320, 370, 430, 500, 600, 750, 900, 1100, 1300, 1500, 1750, 2000, 2250, 2500, 3000, 3500, 4000, 9999999],
-
- procenty_punkty: [
- [0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990, 1020, 1400, 1440, 1520, 1530, 9999999], // przed
- [0, 50, 90, 120, 150, 180, 210, 500, 600, 360, 660, 690, 720, 810, 1370, 1390, 1430, 1440, 1470, 1700, 2000, 2220, 2230, 9999999] //po
- ],
-
- procenty_PC: [0, 6, 10, 16, 21, 25, 31, 35, 9999999],
-
- procenty_t: [
- [-1, -1, -1, 50, 44, 26, 8, 0],
- [83, 74, 65, 56, 47, 29, 11, 0],
- [86, 77, 68, 59, 50, 32, 14, 0],
- [89, 80, 71, 62, 53, 35, 17, 0],
- [92, 83, 74, 65, 56, 38, 20, 2],
- [95, 86, 77, 68, 59, 41, 23, 5],
- [98, 89, 80, 71, 62, 44, 26, 8],
- [100, 92, 83, 74, 65, 47, 29, 11],
- [100, 95, 86, 77, 68, 50, 32, 14],
- [100, 98, 89, 80, 71, 53, 35, 17],
- [100, 100, 92, 83, 74, 56, 38, 20],
- [100, 100, 95, 86, 77, 59, 41, 23],
- [100, 100, 98, 89, 80, 62, 44, 26],
- [100, 100, 100, 92, 83, 65, 47, 29],
- [100, 100, 100, 95, 86, 68, 50, 32],
- [100, 100, 100, 98, 89, 71, 53, 35],
- [100, 100, 100, 100, 92, 74, 56, 38],
- [100, 100, 100, 100, 95, 77, 59, 41],
- [100, 100, 100, 100, 98, 80, 62, 44],
- [100, 100, 100, 100, 100, 83, 65, 47],
- [100, 100, 100, 100, 100, 86, 68, 50],
- [100, 100, 100, 100, 100, 89, 71, 53],
- [100, 100, 100, 100, 100, 100, 95, 90]
- ],
-
- IMP: function(punkty) {
- var i = 0;
- while( this.IMP_t[i+1] <= punkty ) {
- i++;
- }
- return i;
- },
-
- procenty: function(PC, punkty, partia) {
- var punkty_i = 0;
- var PC_i = 0;
-
- while(this.procenty_punkty[partia][punkty_i+1] <= punkty) {
- punkty_i++;
- }
-
- while(this.procenty_PC[PC_i+1] <= PC) {
- PC_i++;
- }
-
- return this.procenty_t[punkty_i][PC_i];
- },
-
- policz: function(kontrakt, kolor, kontra, partia, PC, lewy) {
- var wynik = {};
-
- var my_partia = (partia / 2) >> 0;
- var wy_partia = partia % 2;
-
- // 4 pasy
- if( kontrakt == 0 ) {
- wynik.dla = 1;
- wynik.punkty_przed = 0;
- wynik.punkty = this.PC[my_partia][wy_partia][PC];
- wynik.IMP = this.IMP(wynik.punkty);
- wynik.procent = 100-this.procenty(PC, 0, my_partia);
-
- return wynik;
- }
-
-
- // Wynik w punktach
- var wynik_punkty = 0;
- // Punkty za lewy kontraktowe
- var wynik_lewy = 0;
- // Wynik w punktach, uwzględniając ilość PC
- var wynik_punkty_PC = 0;
-
- // Ilość nadróbek
- var nadrobki = lewy - 6 - kontrakt;
-
- // Mnożnik przy kontrze
- var mnoznik = kontra+1;
- if( mnoznik == 3 ) {
- mnoznik = 4;
- }
-
- // Kontrakt ugrany
- if( nadrobki >= 0 ) {
- // "Nadróbki: jak za lewy"
- if( mnoznik == 1 ) {
- wynik_lewy = this.lewy[kolor][kontrakt];
- wynik_punkty = this.lewy[kolor][lewy - 6];
- }
- // Kontra lub rekontra
- else
- {
- wynik_lewy = this.lewy[kolor][kontrakt] * mnoznik;
- wynik_punkty = wynik_lewy;
-
- // Za wygranie z kontrą
- wynik_punkty += 25 * mnoznik;
-
- // Za nadróbki
- wynik_punkty += nadrobki * (my_partia == 0 ? 50 : 100) * mnoznik;
- }
-
- if(kontrakt == 6) {
- // Za szlemika
- wynik_punkty += (my_partia == 0 ? 500 : 750);
- }
- else if(kontrakt == 7) {
- // Za szlema
- wynik_punkty += (my_partia == 0 ? 1000 : 1500);
- }
-
- if(wynik_lewy >= 100) {
- // Za partię
- wynik_punkty += (my_partia == 0 ? 300 : 500);
- }
- else
- {
- // Za częściówkę
- wynik_punkty += 50;
- }
- }
- // Wpadka
- else
- {
- // Punkty za wpadkę
- wynik_punkty = -1 * this.wpadki[my_partia][kontra][-1 * nadrobki];
- }
-
- // Uwzględniamy ilość punktów, którą trzeba było zdobyć
- wynik_punkty_PC = wynik_punkty - this.PC[my_partia][wy_partia][PC];
-
- // Zapis dla przeciwników
- if( wynik_punkty_PC < 0 ) {
- wynik.dla = 1;
- wynik.punkty_przed = -1 * wynik_punkty;
- wynik.punkty = -1 * wynik_punkty_PC;
- wynik.IMP = this.IMP(wynik.punkty);
- if( wynik_punkty < 0 ) {
- wynik.procent = this.procenty(40 - PC, -1 * wynik_punkty, wy_partia);
- }
- else
- {
- wynik.procent = 100 - this.procenty(PC, wynik_punkty, my_partia);
- }
- }
- // Zapis dla rozgrywających
- else
- {
- wynik.dla = 0;
- wynik.punkty_przed = wynik_punkty;
- wynik.punkty = wynik_punkty_PC;
- wynik.IMP = this.IMP(wynik.punkty);
- if( wynik_punkty > 0 ) {
- wynik.procent = this.procenty(PC, wynik_punkty, my_partia);
- }
- else
- {
- wynik.procent = 100 - this.procenty(40 - PC, -1 * wynik_punkty, wy_partia);
- }
- }
-
- return wynik;
- }
-};
-
var zapis;
function compute() {
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