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

Commit

Permalink
feat(rating): ability to disable rating reset
Browse files Browse the repository at this point in the history
Closes #5532
Closes #5631
  • Loading branch information
kyh authored and wesleycho committed Mar 15, 2016
1 parent e15a22a commit 8747b58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/rating/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Rating directive that will take care of visualising a star rating bar.
_(Default: ['one', 'two', 'three', 'four', 'five']`)_ -
An array of strings defining titles for all icons.

* `enable-reset`
<small class="badge">$</small>
_(Default: `true`)_ -
Clicking the icon of the current rating will reset the rating to 0.

* `state-off`
<small class="badge">$</small>
<small class="badge">C</small>
Expand Down
8 changes: 6 additions & 2 deletions src/rating/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ angular.module('ui.bootstrap.rating', [])
max: 5,
stateOn: null,
stateOff: null,
enableReset: true,
titles : ['one', 'two', 'three', 'four', 'five']
})

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

this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn;
this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff;
var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles ;
this.enableReset = angular.isDefined($attrs.enableReset) ?
$scope.$parent.$eval($attrs.enableReset) : ratingConfig.enableReset;
var tmpTitles = angular.isDefined($attrs.titles) ? $scope.$parent.$eval($attrs.titles) : ratingConfig.titles;
this.titles = angular.isArray(tmpTitles) && tmpTitles.length > 0 ?
tmpTitles : ratingConfig.titles;

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

$scope.rate = function(value) {
if (!$scope.readonly && value >= 0 && value <= $scope.range.length) {
ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue === value ? 0 : value);
var newViewValue = self.enableReset && ngModelCtrl.$viewValue === value ? 0 : value;
ngModelCtrl.$setViewValue(newViewValue);
ngModelCtrl.$render();
}
};
Expand Down
25 changes: 25 additions & 0 deletions src/rating/test/rating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ describe('rating directive', function() {
expect(getState()).toEqual([true, true, true, true, true]);
});

it('handles enable-reset attribute', function() {
$rootScope.canReset = false;
element = $compile('<uib-rating ng-model="rate" enable-reset="canReset"></uib-rating>')($rootScope);
$rootScope.$digest();

var star = {
states: [true, true, true, true, true],
rating: 5
};

var selectStar = getStar(star.rating);

selectStar.click();
$rootScope.$digest();
expect(getState()).toEqual(star.states);
expect($rootScope.rate).toBe(5);
expect(element.attr('aria-valuenow')).toBe('5');

selectStar.click();
$rootScope.$digest();
expect(getState()).toEqual(star.states);
expect($rootScope.rate).toBe(5);
expect(element.attr('aria-valuenow')).toBe('5');
});

it('should fire onHover', function() {
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');
element = $compile('<uib-rating ng-model="rate" on-hover="hoveringOver(value)"></uib-rating>')($rootScope);
Expand Down

0 comments on commit 8747b58

Please sign in to comment.