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

Commit 36e0f0e

Browse files
groupskywesleycho
authored andcommitted
fix(progressbar): use max value on stacked progress bar
Closes #3830 Fixes #3618
1 parent 6235937 commit 36e0f0e

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/progressbar/progressbar.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ angular.module('ui.bootstrap.progressbar', [])
1919

2020
this.bars.push(bar);
2121

22+
bar.max = $scope.max;
23+
2224
bar.$watch('value', function( value ) {
23-
bar.percent = +(100 * value / $scope.max).toFixed(2);
25+
bar.recalculatePercentage();
2426
});
2527

28+
bar.recalculatePercentage = function() {
29+
bar.percent = +(100 * bar.value / bar.max).toFixed(2);
30+
};
31+
2632
bar.$on('$destroy', function() {
2733
element = null;
2834
self.removeBar(bar);
@@ -32,6 +38,13 @@ angular.module('ui.bootstrap.progressbar', [])
3238
this.removeBar = function(bar) {
3339
this.bars.splice(this.bars.indexOf(bar), 1);
3440
};
41+
42+
$scope.$watch('max', function(max) {
43+
self.bars.forEach(function (bar) {
44+
bar.max = $scope.max;
45+
bar.recalculatePercentage();
46+
});
47+
});
3548
}])
3649

3750
.directive('progress', function() {
@@ -41,7 +54,9 @@ angular.module('ui.bootstrap.progressbar', [])
4154
transclude: true,
4255
controller: 'ProgressController',
4356
require: 'progress',
44-
scope: {},
57+
scope: {
58+
max: '=?'
59+
},
4560
templateUrl: 'template/progressbar/progress.html'
4661
};
4762
})
@@ -54,7 +69,6 @@ angular.module('ui.bootstrap.progressbar', [])
5469
require: '^progress',
5570
scope: {
5671
value: '=',
57-
max: '=?',
5872
type: '@'
5973
},
6074
templateUrl: 'template/progressbar/bar.html',

src/progressbar/test/progressbar.spec.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('progressbar directive', function () {
116116
it('transcludes "bar" text', function() {
117117
expect(getBar(0).text()).toBe('22/200');
118118
});
119-
119+
120120
it('adjusts the valuemax when it changes', function() {
121121
expect(getBar(0).attr('aria-valuemax')).toBe('200');
122122
$rootScope.max = 300;
@@ -215,5 +215,49 @@ describe('progressbar directive', function () {
215215
expect(getBar(0)).not.toHaveClass(BAR_CLASS + '-success');
216216
expect(getBar(0)).not.toHaveClass(BAR_CLASS + '-warning');
217217
});
218+
219+
describe('"max" attribute', function() {
220+
beforeEach(inject(function() {
221+
$rootScope.max = 200;
222+
element = $compile('<progress max="max" animate="false"><bar ng-repeat="o in objects" value="o.value">{{o.value}}/{{max}}</bar></progress>')($rootScope);
223+
$rootScope.$digest();
224+
}));
225+
226+
it('has the appropriate aria markup', function() {
227+
expect(getBar(0).attr('aria-valuemax')).toBe('200');
228+
});
229+
230+
it('adjusts the "bar" width when it changes', function() {
231+
expect(getBar(0).css('width')).toBe('5%');
232+
$rootScope.max = 250;
233+
$rootScope.$digest();
234+
expect(getBar(0).css('width')).toBe('4%');
235+
});
236+
237+
it('adjusts the "bar" width when value changes', function() {
238+
$rootScope.objects[0].value = 60;
239+
$rootScope.$digest();
240+
expect(getBar(0).css('width')).toBe('30%');
241+
242+
$rootScope.objects[0].value += 12;
243+
$rootScope.$digest();
244+
expect(getBar(0).css('width')).toBe('36%');
245+
246+
$rootScope.objects[0].value = 0;
247+
$rootScope.$digest();
248+
expect(getBar(0).css('width')).toBe('0%');
249+
});
250+
251+
it('transcludes "bar" text', function() {
252+
expect(getBar(0).text()).toBe('10/200');
253+
});
254+
255+
it('adjusts the valuemax when it changes', function() {
256+
expect(getBar(0).attr('aria-valuemax')).toBe('200');
257+
$rootScope.max = 300;
258+
$rootScope.$digest();
259+
expect(getBar(0).attr('aria-valuemax')).toBe('300');
260+
});
261+
});
218262
});
219263
});

0 commit comments

Comments
 (0)