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

Commit 8747b58

Browse files
kyhwesleycho
authored andcommitted
feat(rating): ability to disable rating reset
Closes #5532 Closes #5631
1 parent e15a22a commit 8747b58

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/rating/docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ Rating directive that will take care of visualising a star rating bar.
3838
_(Default: ['one', 'two', 'three', 'four', 'five']`)_ -
3939
An array of strings defining titles for all icons.
4040

41+
* `enable-reset`
42+
<small class="badge">$</small>
43+
_(Default: `true`)_ -
44+
Clicking the icon of the current rating will reset the rating to 0.
45+
4146
* `state-off`
4247
<small class="badge">$</small>
4348
<small class="badge">C</small>

src/rating/rating.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ angular.module('ui.bootstrap.rating', [])
44
max: 5,
55
stateOn: null,
66
stateOff: null,
7+
enableReset: true,
78
titles : ['one', 'two', 'three', 'four', 'five']
89
})
910

@@ -25,7 +26,9 @@ angular.module('ui.bootstrap.rating', [])
2526

2627
this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn;
2728
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff;
28-
var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles ;
29+
this.enableReset = angular.isDefined($attrs.enableReset) ?
30+
$scope.$parent.$eval($attrs.enableReset) : ratingConfig.enableReset;
31+
var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles;
2932
this.titles = angular.isArray(tmpTitles) && tmpTitles.length > 0 ?
3033
tmpTitles : ratingConfig.titles;
3134

@@ -52,7 +55,8 @@ angular.module('ui.bootstrap.rating', [])
5255

5356
$scope.rate = function(value) {
5457
if (!$scope.readonly && value >= 0 && value <= $scope.range.length) {
55-
ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue === value ? 0 : value);
58+
var newViewValue = self.enableReset && ngModelCtrl.$viewValue === value ? 0 : value;
59+
ngModelCtrl.$setViewValue(newViewValue);
5660
ngModelCtrl.$render();
5761
}
5862
};

src/rating/test/rating.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ describe('rating directive', function() {
146146
expect(getState()).toEqual([true, true, true, true, true]);
147147
});
148148

149+
it('handles enable-reset attribute', function() {
150+
$rootScope.canReset = false;
151+
element = $compile('<uib-rating ng-model="rate" enable-reset="canReset"></uib-rating>')($rootScope);
152+
$rootScope.$digest();
153+
154+
var star = {
155+
states: [true, true, true, true, true],
156+
rating: 5
157+
};
158+
159+
var selectStar = getStar(star.rating);
160+
161+
selectStar.click();
162+
$rootScope.$digest();
163+
expect(getState()).toEqual(star.states);
164+
expect($rootScope.rate).toBe(5);
165+
expect(element.attr('aria-valuenow')).toBe('5');
166+
167+
selectStar.click();
168+
$rootScope.$digest();
169+
expect(getState()).toEqual(star.states);
170+
expect($rootScope.rate).toBe(5);
171+
expect(element.attr('aria-valuenow')).toBe('5');
172+
});
173+
149174
it('should fire onHover', function() {
150175
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');
151176
element = $compile('<uib-rating ng-model="rate" on-hover="hoveringOver(value)"></uib-rating>')($rootScope);

0 commit comments

Comments
 (0)