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

Commit 9dfe315

Browse files
bekospkozlowski-opensource
authored andcommittedFeb 1, 2014
feat(progressbar): make widget accessible
* Add WAI-ARIA markup. Closes #1717
1 parent 1946833 commit 9dfe315

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed
 

‎src/progressbar/progressbar.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,28 @@ angular.module('ui.bootstrap.progressbar', [])
1010
animate = angular.isDefined($attrs.animate) ? $scope.$parent.$eval($attrs.animate) : progressConfig.animate;
1111

1212
this.bars = [];
13-
this.max = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : progressConfig.max;
13+
$scope.max = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : progressConfig.max;
1414

1515
this.addBar = function(bar, element) {
16-
bar.$element = element;
17-
1816
if ( !animate ) {
19-
bar.$element.css({'transition': 'none'});
17+
element.css({'transition': 'none'});
2018
}
2119

2220
this.bars.push(bar);
2321

24-
bar.$watch('value', function() {
25-
self.update( bar );
22+
bar.$watch('value', function( value ) {
23+
bar.percent = Math.round(100 * value / $scope.max);
2624
});
2725

2826
bar.$on('$destroy', function() {
29-
bar.$element = null;
27+
element = null;
3028
self.removeBar(bar);
3129
});
3230
};
3331

34-
this.update = function( bar ) {
35-
var percent = this.getPercentage( bar.value );
36-
bar.$element.css({ 'width': percent + '%' });
37-
};
38-
3932
this.removeBar = function(bar) {
4033
this.bars.splice(this.bars.indexOf(bar), 1);
4134
};
42-
43-
this.getPercentage = function(value) {
44-
return Math.round(100 * value / this.max);
45-
};
4635
}])
4736

4837
.directive('progress', function() {

‎src/progressbar/test/progressbar.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe('progressbar directive', function () {
2929
expect(getBar(0).css('width')).toBe('22%');
3030
});
3131

32+
it('has the appropriate aria markup', function() {
33+
var bar = getBar(0);
34+
expect(bar.attr('role')).toBe('progressbar');
35+
expect(bar.attr('aria-valuemin')).toBe('0');
36+
expect(bar.attr('aria-valuemax')).toBe('100');
37+
expect(bar.attr('aria-valuenow')).toBe('22');
38+
expect(bar.attr('aria-valuetext')).toBe('22%');
39+
});
40+
3241
it('transcludes "bar" text', function() {
3342
expect(getBar(0).text()).toBe('22 %');
3443
});
@@ -43,13 +52,30 @@ describe('progressbar directive', function () {
4352
expect(getBar(0)).toHaveClass('pizza');
4453
});
4554

55+
it('adjusts the "bar" width and aria when value changes', function() {
56+
$rootScope.value = 60;
57+
$rootScope.$digest();
58+
59+
var bar = getBar(0);
60+
expect(bar.css('width')).toBe('60%');
61+
62+
expect(bar.attr('aria-valuemin')).toBe('0');
63+
expect(bar.attr('aria-valuemax')).toBe('100');
64+
expect(bar.attr('aria-valuenow')).toBe('60');
65+
expect(bar.attr('aria-valuetext')).toBe('60%');
66+
});
67+
4668
describe('"max" attribute', function () {
4769
beforeEach(inject(function() {
4870
$rootScope.max = 200;
4971
element = $compile('<progressbar max="max" animate="false" value="value">{{value}}/{{max}}</progressbar>')($rootScope);
5072
$rootScope.$digest();
5173
}));
5274

75+
it('has the appropriate aria markup', function() {
76+
expect(getBar(0).attr('aria-valuemax')).toBe('200');
77+
});
78+
5379
it('adjusts the "bar" width', function() {
5480
expect(element.children().eq(0).css('width')).toBe('11%');
5581
});

‎template/progressbar/bar.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div class="progress-bar" ng-class="type && 'progress-bar-' + type" ng-transclude></div>
1+
<div class="progress-bar" ng-class="type && 'progress-bar-' + type" role="progressbar" aria-valuenow="{{value}}" aria-valuemin="0" aria-valuemax="{{max}}" ng-style="{width: percent + '%'}" aria-valuetext="{{percent}}%" ng-transclude></div>

‎template/progressbar/progressbar.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
<div class="progress"><div class="progress-bar" ng-class="type && 'progress-bar-' + type" ng-transclude></div></div>
1+
<div class="progress">
2+
<div class="progress-bar" ng-class="type && 'progress-bar-' + type" role="progressbar" aria-valuenow="{{value}}" aria-valuemin="0" aria-valuemax="{{max}}" ng-style="{width: percent + '%'}" aria-valuetext="{{percent}}%" ng-transclude></div>
3+
</div>

0 commit comments

Comments
 (0)