From 25cb20db10b00bf033b166f78f90f7d78a4f2590 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sat, 29 Aug 2015 20:51:07 +0000
Subject: [PATCH] Use callbacks for editing data in ResultsTable

---
 bridge_results_table.js |   78 ++++++++++++++++++++++++++------------
 1 files changed, 53 insertions(+), 25 deletions(-)

diff --git a/bridge_results_table.js b/bridge_results_table.js
index 4c83ac6..5a14698 100644
--- a/bridge_results_table.js
+++ b/bridge_results_table.js
@@ -1,8 +1,17 @@
-function ResultsTable(id, container) {
+function ResultsTable(id, container, editPlayerCallback, editResultCallback) {
 	this.id = id;
 	this.players = [];
 	this.results = [];
 	this.store = window.sessionStorage;
+	
+	if(!editPlayerCallback) {
+		editPlayerCallback = this._editPlayerCallback;
+	}
+	this.editPlayerCallback = editPlayerCallback;
+	if(!editResultCallback) {
+		editResultCallback = this._editResultCallback;
+	}
+	this.editResultCallback = editResultCallback;
 	
 	container.appendChild(this._createTable());
 }
@@ -59,23 +68,26 @@
 	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();
-		}
+		self.editPlayerCallback(
+			originalName,
+			function (result) {
+				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.splice(index, 1);
+					self.results.splice(index, 1);
+					self.refresh();
+				}
+			}
+		);
 	} }(this);
 	return th;
 };
@@ -84,6 +96,18 @@
 		this.results[i].push(result[i]);
 	}
 	this.refresh();
+};
+ResultsTable.prototype._editPlayerCallback = function(name, callback) {
+	var result = prompt('Podaj nowe inicjały gracza. Wyczyść pole, by go usunąć.', name);
+	if(result) {
+		callback(result);
+	}
+};
+ResultsTable.prototype._editResultCallback = function(value, callback) {
+	var result = prompt('Podaj nową wartość:', value);
+	if(result) {
+		callback(result);
+	}
 };
 ResultsTable.prototype.refresh = function() {
 	this.save();
@@ -100,7 +124,9 @@
 	}
 	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);
+		td.innerText = this.results[i].reduce(
+			function(a, b) { return (parseInt(a, 10) || 0)+(parseInt(b, 10) || 0); }, 0
+		);
 		this.tresults.appendChild(td);
 	}
 	
@@ -115,11 +141,13 @@
 				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();
-					}
+					self.editResultCallback(
+						self.results[j][i],
+						function (value) {
+							self.results[j][i] = value;
+							self.refresh();
+						}
+					);
 				} }(j, i, this);
 				tr.appendChild(td);
 			}
@@ -127,4 +155,4 @@
 			this.tbody.appendChild(tr);
 		}
 	}
-};
\ No newline at end of file
+};

--
Gitblit v1.9.1