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

Fix calculation of progressbar on removed bar #4588

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/progressbar/progressbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ angular.module('ui.bootstrap.progressbar', [])
});

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);

Expand All @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions src/progressbar/test/progressbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
});
Expand Down