Skip to content

Commit 09ba4df

Browse files
committed
fix(progressbar): use max value on stacked progress bar
Fixes angular-ui#3618
1 parent 49e73a8 commit 09ba4df

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

Diff for: src/progressbar/progressbar.js

+18-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,14 @@ 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(self){
43+
return function(max) {
44+
self.bars.forEach(function (bar) {
45+
bar.max = $scope.max;
46+
bar.recalculatePercentage();
47+
});
48+
}})(this));
3549
}])
3650

3751
.directive('progress', function() {
@@ -41,7 +55,9 @@ angular.module('ui.bootstrap.progressbar', [])
4155
transclude: true,
4256
controller: 'ProgressController',
4357
require: 'progress',
44-
scope: {},
58+
scope: {
59+
max: '=?'
60+
},
4561
templateUrl: 'template/progressbar/progress.html'
4662
};
4763
})
@@ -54,7 +70,6 @@ angular.module('ui.bootstrap.progressbar', [])
5470
require: '^progress',
5571
scope: {
5672
value: '=',
57-
max: '=?',
5873
type: '@'
5974
},
6075
templateUrl: 'template/progressbar/bar.html',

Diff for: 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)