From 2e9177e5f6408d691f3e33bcd784bd9a74bd7a55 Mon Sep 17 00:00:00 2001 From: Steffan Long Date: Tue, 28 Jul 2015 11:37:29 -0700 Subject: [PATCH] fix(progressbar): allow max width of 100% - When stacking bars, ensure that the max width possible is 100% Closes #4027 Fixes #4018 --- src/progressbar/progressbar.js | 9 +++++++++ src/progressbar/test/progressbar.spec.js | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/progressbar/progressbar.js b/src/progressbar/progressbar.js index e045f6ab46..3ab3531dd7 100644 --- a/src/progressbar/progressbar.js +++ b/src/progressbar/progressbar.js @@ -27,6 +27,15 @@ angular.module('ui.bootstrap.progressbar', []) bar.recalculatePercentage = function() { bar.percent = +(100 * bar.value / bar.max).toFixed(2); + + var totalPercentage = 0; + self.bars.forEach(function (bar) { + totalPercentage += bar.percent; + }); + + if (totalPercentage > 100) { + bar.percent -= totalPercentage - 100; + } }; bar.$on('$destroy', function() { diff --git a/src/progressbar/test/progressbar.spec.js b/src/progressbar/test/progressbar.spec.js index c0500fd931..57756630dc 100644 --- a/src/progressbar/test/progressbar.spec.js +++ b/src/progressbar/test/progressbar.spec.js @@ -258,6 +258,21 @@ describe('progressbar directive', function () { $rootScope.$digest(); expect(getBar(0).attr('aria-valuemax')).toBe('300'); }); + + 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'); + }); }); }); });