From 7e3542e60e9fcf7f30ff873b2bc14f2c8f583806 Mon Sep 17 00:00:00 2001 From: JDeuker Date: Fri, 25 Sep 2015 09:26:28 +0200 Subject: [PATCH 1/2] fix(progressbar): fix percentage calculation - Fix percentage calculation when bars are altered --- src/progressbar/progressbar.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/progressbar/progressbar.js b/src/progressbar/progressbar.js index 240b5d5088..4592759389 100644 --- a/src/progressbar/progressbar.js +++ b/src/progressbar/progressbar.js @@ -25,11 +25,10 @@ angular.module('ui.bootstrap.progressbar', []) bar.$watch('value', function(value) { bar.recalculatePercentage(); }); - + bar.recalculatePercentage = function() { - bar.percent = +(100 * bar.value / bar.max).toFixed(2); - var totalPercentage = self.bars.reduce(function(total, bar) { + bar.percent = +(100 * bar.value / bar.max).toFixed(2); return total + bar.percent; }, 0); @@ -46,6 +45,9 @@ angular.module('ui.bootstrap.progressbar', []) this.removeBar = function(bar) { this.bars.splice(this.bars.indexOf(bar), 1); + this.bars.forEach(function (bar) { + bar.recalculatePercentage(); + }); }; $scope.$watch('max', function(max) { From f7047e31739742b6480bd0a014c964bdeaff2a13 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sun, 11 Oct 2015 15:19:39 -0700 Subject: [PATCH 2/2] chore(progressbar): add test --- src/progressbar/progressbar.js | 2 +- src/progressbar/test/progressbar.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/progressbar/progressbar.js b/src/progressbar/progressbar.js index 4592759389..366aadba95 100644 --- a/src/progressbar/progressbar.js +++ b/src/progressbar/progressbar.js @@ -25,7 +25,7 @@ angular.module('ui.bootstrap.progressbar', []) bar.$watch('value', function(value) { bar.recalculatePercentage(); }); - + bar.recalculatePercentage = function() { var totalPercentage = self.bars.reduce(function(total, bar) { bar.percent = +(100 * bar.value / bar.max).toFixed(2); diff --git a/src/progressbar/test/progressbar.spec.js b/src/progressbar/test/progressbar.spec.js index 1ad71a92c1..38ffe518f2 100644 --- a/src/progressbar/test/progressbar.spec.js +++ b/src/progressbar/test/progressbar.spec.js @@ -315,6 +315,30 @@ describe('progressbar directive', function() { } expect(totalWidth.toFixed(2)).toBe('100.00'); }); + + it('should not have a total width over 100%', function() { + $rootScope.objects = [ + { value: 60, type: 'warning' }, + { value: 103 }, + { value: 270, type: 'info' } + ]; + $rootScope.max = 433; + $rootScope.$digest(); + var totalWidth = 0; + for (var i = 0; i < 3; i++) { + totalWidth += parseFloat(getBar(i).css('width')); + } + expect(totalWidth.toFixed(2)).toBe('100.00'); + + $rootScope.objects.splice(2, 1); + $rootScope.max = 163; + $rootScope.$digest(); + totalWidth = 0; + for (i = 0; i < 2; i++) { + totalWidth += parseFloat(getBar(i).css('width')); + } + expect(totalWidth.toFixed(2)).toBe('100.00'); + }); }); }); });