From 1f1a6f97a2e2196f47d918e41e8194612bf489c9 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 9 Jun 2016 11:37:30 +0100 Subject: [PATCH 1/3] Fix percentage recalculation when both votes are zero --- result-app/views/app.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/result-app/views/app.js b/result-app/views/app.js index 78cd935..8b84cdd 100644 --- a/result-app/views/app.js +++ b/result-app/views/app.js @@ -5,15 +5,6 @@ var bg1 = document.getElementById('background-stats-1'); var bg2 = document.getElementById('background-stats-2'); app.controller('statsCtrl', function($scope){ - var animateStats = function(a,b){ - if(a+b>0){ - var percentA = a/(a+b)*100; - var percentB = 100-percentA; - bg1.style.width= percentA+"%"; - bg2.style.width = percentB+"%"; - } - }; - $scope.aPercent = 50; $scope.bPercent = 50; @@ -23,15 +14,16 @@ app.controller('statsCtrl', function($scope){ var a = parseInt(data.a || 0); var b = parseInt(data.b || 0); - animateStats(a, b); + var percentages = getPercentages(a, b); - $scope.$apply(function() { - if(a + b > 0){ - $scope.aPercent = a/(a+b) * 100; - $scope.bPercent = b/(a+b) * 100; - $scope.total = a + b - } - }); + bg1.style.width = percentages.a + "%"; + bg2.style.width = percentages.b + "%"; + + $scope.$apply(function () { + $scope.aPercent = percentages.a; + $scope.bPercent = percentages.b; + $scope.total = a + b; + }); }); }; @@ -43,3 +35,16 @@ app.controller('statsCtrl', function($scope){ init(); }); }); + +function getPercentages(a, b) { + var result = {}; + + if (a + b > 0) { + result.a = Math.round(a / (a + b) * 100); + result.b = 100 - result.a; + } else { + result.a = result.b = 50; + } + + return result; +} \ No newline at end of file From 4b62828661a6e0fb2ece2ae4ea8a933263d674ce Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 9 Jun 2016 11:48:19 +0100 Subject: [PATCH 2/3] Extract vote collection function --- result-app/server.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/result-app/server.js b/result-app/server.js index 0675afe..bd9cfc2 100644 --- a/result-app/server.js +++ b/result-app/server.js @@ -45,17 +45,24 @@ function getVotes(client) { if (err) { console.error("Error performing query: " + err); } else { - var data = result.rows.reduce(function(obj, row) { - obj[row.vote] = row.count; - return obj; - }, {}); - io.sockets.emit("scores", JSON.stringify(data)); + var votes = collectVotesFromResult(result); + io.sockets.emit("scores", JSON.stringify(votes)); } setTimeout(function() {getVotes(client) }, 1000); }); } +function collectVotesFromResult(result) { + var votes = {a: 0, b: 0}; + + result.rows.forEach(function (row) { + votes[row.vote] = parseInt(row.count); + }); + + return votes; +} + app.use(cookieParser()); app.use(bodyParser()); app.use(methodOverride('X-HTTP-Method-Override')); From aa05c12ab77fb2eb8488636633a45f9b6eaac403 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Thu, 9 Jun 2016 12:18:28 +0100 Subject: [PATCH 3/3] Always show total vote count --- result-app/views/index.html | 6 ++++-- result-app/views/stylesheets/style.css | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/result-app/views/index.html b/result-app/views/index.html index 2a97853..0b6713a 100644 --- a/result-app/views/index.html +++ b/result-app/views/index.html @@ -31,8 +31,10 @@ -
- {{total}} votes +
+ No votes yet + {{total}} vote + {{total}} votes
diff --git a/result-app/views/stylesheets/style.css b/result-app/views/stylesheets/style.css index 9acf00d..6842773 100644 --- a/result-app/views/stylesheets/style.css +++ b/result-app/views/stylesheets/style.css @@ -47,6 +47,7 @@ body{ vertical-align:middle; } #result{ + z-index: 3; position: absolute; bottom: 40px; right: 20px;