Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit feb689c

Browse files
JDeukerwesleycho
authored andcommitted
fix(progressbar): fix percentage calculation
- Fix percentage calculation when bars are altered Closes #4471 Closes #4588 Fixes #4452
1 parent 0b1e206 commit feb689c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/progressbar/progressbar.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ angular.module('ui.bootstrap.progressbar', [])
2727
});
2828

2929
bar.recalculatePercentage = function() {
30-
bar.percent = +(100 * bar.value / bar.max).toFixed(2);
31-
3230
var totalPercentage = self.bars.reduce(function(total, bar) {
31+
bar.percent = +(100 * bar.value / bar.max).toFixed(2);
3332
return total + bar.percent;
3433
}, 0);
3534

@@ -46,6 +45,9 @@ angular.module('ui.bootstrap.progressbar', [])
4645

4746
this.removeBar = function(bar) {
4847
this.bars.splice(this.bars.indexOf(bar), 1);
48+
this.bars.forEach(function (bar) {
49+
bar.recalculatePercentage();
50+
});
4951
};
5052

5153
$scope.$watch('max', function(max) {

src/progressbar/test/progressbar.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,29 @@ describe('progressbar directive', function() {
315315
}
316316
expect(totalWidth.toFixed(2)).toBe('100.00');
317317
});
318+
319+
it('should not have a total width over 37.65% when removing bar', function() {
320+
$rootScope.objects = [
321+
{ value: 60, type: 'warning' },
322+
{ value: 103 },
323+
{ value: 270, type: 'info' }
324+
];
325+
$rootScope.max = 433;
326+
$rootScope.$digest();
327+
var totalWidth = 0;
328+
for (var i = 0; i < 3; i++) {
329+
totalWidth += parseFloat(getBar(i).css('width'));
330+
}
331+
expect(totalWidth.toFixed(2)).toBe('100.00');
332+
333+
$rootScope.objects.splice(2, 1);
334+
$rootScope.$digest();
335+
totalWidth = 0;
336+
for (i = 0; i < 2; i++) {
337+
totalWidth += parseFloat(getBar(i).css('width'));
338+
}
339+
expect(totalWidth.toFixed(2)).toBe('37.65');
340+
});
318341
});
319342
});
320343
});

0 commit comments

Comments
 (0)