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

fix(progressbar): use max value on stacked progress bar #3830

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
22 changes: 19 additions & 3 deletions src/progressbar/progressbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ angular.module('ui.bootstrap.progressbar', [])

this.bars.push(bar);

bar.max = $scope.max;

bar.$watch('value', function( value ) {
bar.percent = +(100 * value / $scope.max).toFixed(2);
bar.recalculatePercentage();
});

bar.recalculatePercentage = function() {
bar.percent = +(100 * bar.value / bar.max).toFixed(2);
};

bar.$on('$destroy', function() {
element = null;
self.removeBar(bar);
Expand All @@ -32,6 +38,15 @@ angular.module('ui.bootstrap.progressbar', [])
this.removeBar = function(bar) {
this.bars.splice(this.bars.indexOf(bar), 1);
};

$scope.$watch('max', (function(self){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is a bit awkward - why not use the self variable defined at the top of the controller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't notice the reference. updated the PR

return function(max) {
self.bars.forEach(function (bar) {
bar.max = $scope.max;
bar.recalculatePercentage();
});
};
})(this));
}])

.directive('progress', function() {
Expand All @@ -41,7 +56,9 @@ angular.module('ui.bootstrap.progressbar', [])
transclude: true,
controller: 'ProgressController',
require: 'progress',
scope: {},
scope: {
max: '=?'
},
templateUrl: 'template/progressbar/progress.html'
};
})
Expand All @@ -54,7 +71,6 @@ angular.module('ui.bootstrap.progressbar', [])
require: '^progress',
scope: {
value: '=',
max: '=?',
type: '@'
},
templateUrl: 'template/progressbar/bar.html',
Expand Down
46 changes: 45 additions & 1 deletion src/progressbar/test/progressbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('progressbar directive', function () {
it('transcludes "bar" text', function() {
expect(getBar(0).text()).toBe('22/200');
});

it('adjusts the valuemax when it changes', function() {
expect(getBar(0).attr('aria-valuemax')).toBe('200');
$rootScope.max = 300;
Expand Down Expand Up @@ -215,5 +215,49 @@ describe('progressbar directive', function () {
expect(getBar(0)).not.toHaveClass(BAR_CLASS + '-success');
expect(getBar(0)).not.toHaveClass(BAR_CLASS + '-warning');
});

describe('"max" attribute', function() {
beforeEach(inject(function() {
$rootScope.max = 200;
element = $compile('<progress max="max" animate="false"><bar ng-repeat="o in objects" value="o.value">{{o.value}}/{{max}}</bar></progress>')($rootScope);
$rootScope.$digest();
}));

it('has the appropriate aria markup', function() {
expect(getBar(0).attr('aria-valuemax')).toBe('200');
});

it('adjusts the "bar" width when it changes', function() {
expect(getBar(0).css('width')).toBe('5%');
$rootScope.max = 250;
$rootScope.$digest();
expect(getBar(0).css('width')).toBe('4%');
});

it('adjusts the "bar" width when value changes', function() {
$rootScope.objects[0].value = 60;
$rootScope.$digest();
expect(getBar(0).css('width')).toBe('30%');

$rootScope.objects[0].value += 12;
$rootScope.$digest();
expect(getBar(0).css('width')).toBe('36%');

$rootScope.objects[0].value = 0;
$rootScope.$digest();
expect(getBar(0).css('width')).toBe('0%');
});

it('transcludes "bar" text', function() {
expect(getBar(0).text()).toBe('10/200');
});

it('adjusts the valuemax when it changes', function() {
expect(getBar(0).attr('aria-valuemax')).toBe('200');
$rootScope.max = 300;
$rootScope.$digest();
expect(getBar(0).attr('aria-valuemax')).toBe('300');
});
});
});
});