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

fix(progressbar): Progressbar doesnt modify object provided in max attribute #5374

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions src/progressbar/progressbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module('ui.bootstrap.progressbar', [])
animate = angular.isDefined($attrs.animate) ? $scope.$parent.$eval($attrs.animate) : progressConfig.animate;

this.bars = [];
$scope.max = angular.isDefined($scope.max) ? $scope.max : progressConfig.max;
$scope.max = getMaxOrDefault();

this.addBar = function(bar, element, attrs) {
if (!animate) {
Expand All @@ -19,7 +19,7 @@ angular.module('ui.bootstrap.progressbar', [])

this.bars.push(bar);

bar.max = $scope.max;
bar.max = getMaxOrDefault();
bar.title = attrs && angular.isDefined(attrs.title) ? attrs.title : 'progressbar';

bar.$watch('value', function(value) {
Expand Down Expand Up @@ -50,12 +50,17 @@ angular.module('ui.bootstrap.progressbar', [])
});
};

$scope.$watch('max', function(max) {
//$attrs.$observe('maxParam', function(maxParam) {
$scope.$watch('maxParam', function(maxParam) {
self.bars.forEach(function(bar) {
bar.max = $scope.max;
bar.max = getMaxOrDefault();
bar.recalculatePercentage();
});
});

function getMaxOrDefault () {
return angular.isDefined($scope.maxParam) ? $scope.maxParam : progressConfig.max;
}
}])

.directive('uibProgress', function() {
Expand All @@ -65,7 +70,7 @@ angular.module('ui.bootstrap.progressbar', [])
controller: 'UibProgressController',
require: 'uibProgress',
scope: {
max: '=?'
maxParam: '=?max'
},
templateUrl: 'uib/template/progressbar/progress.html'
};
Expand Down Expand Up @@ -94,7 +99,7 @@ angular.module('ui.bootstrap.progressbar', [])
controller: 'UibProgressController',
scope: {
value: '=',
max: '=?',
maxParam: '=?max',
type: '@'
},
templateUrl: 'uib/template/progressbar/progressbar.html',
Expand Down
24 changes: 24 additions & 0 deletions src/progressbar/test/progressbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ describe('progressbar directive', function() {
});
});

describe('"max" attribute using object', function() {
beforeEach(inject(function() {
element = $compile('<uib-progressbar max="settings.max" animate="false" value="settings.value">{{settings.value}}/{{settings.max}}</uib-progressbar>')($rootScope);
$rootScope.$digest();
}));

it('should not modify outside object', function() {
if (typeof $rootScope.settings === 'object') {
// angular set's up the nested object therefore we have to check like this to avoid test crash
expect($rootScope.settings.max).toBeUndefined();
}
expect($rootScope.settings).toBeUndefined();
expect(getBar(0).attr('aria-valuemax')).toBe('100');
$rootScope.settings = {
max: 300,
value: 40
};
$rootScope.$digest();
expect($rootScope.settings.max).toBe(300);
expect(getBar(0).attr('aria-valuemax')).toBe('300');
});
});


describe('"type" attribute', function() {
beforeEach(inject(function() {
$rootScope.type = 'success';
Expand Down