-
Notifications
You must be signed in to change notification settings - Fork 6.7k
[Rating] Add titles for each stars #3621
Changes from all commits
0e9d1f7
25b4e2c
5033e1a
93ddabc
5bda32a
9f938b7
70a6f41
10feaf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ angular.module('ui.bootstrap.rating', []) | |
.constant('ratingConfig', { | ||
max: 5, | ||
stateOn: null, | ||
stateOff: null | ||
stateOff: null, | ||
titles : ['bad','poor','regular','good','gorgeous'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add spaces after each comma |
||
}) | ||
|
||
.controller('RatingController', ['$scope', '$attrs', 'ratingConfig', function($scope, $attrs, ratingConfig) { | ||
|
@@ -22,19 +23,30 @@ 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.titles = angular.isArray(tmpTitles) && tmpTitles.length > 0 ? tmpTitles : ratingConfig.titles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete extra whitespace before |
||
|
||
var ratingStates = angular.isDefined($attrs.ratingStates) ? $scope.$parent.$eval($attrs.ratingStates) : | ||
new Array( angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max ); | ||
$scope.range = this.buildTemplateObjects(ratingStates); | ||
}; | ||
|
||
this.buildTemplateObjects = function(states) { | ||
for (var i = 0, n = states.length; i < n; i++) { | ||
states[i] = angular.extend({ index: i }, { stateOn: this.stateOn, stateOff: this.stateOff }, states[i]); | ||
states[i] = angular.extend({ index: i }, { stateOn: this.stateOn, stateOff: this.stateOff, title:this.getTitle(i) }, states[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add space after |
||
} | ||
return states; | ||
}; | ||
|
||
|
||
this.getTitle = function (index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete space before |
||
if( index >= this.titles.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spacing of if statement, i.e.
|
||
return index+1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to |
||
} | ||
else { | ||
return this.titles[index]; | ||
} | ||
}; | ||
|
||
$scope.rate = function(value) { | ||
if ( !$scope.readonly && value >= 0 && value <= $scope.range.length ) { | ||
ngModelCtrl.$setViewValue(value); | ||
|
@@ -84,4 +96,4 @@ angular.module('ui.bootstrap.rating', []) | |
ratingCtrl.init( ngModelCtrl ); | ||
} | ||
}; | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,15 @@ describe('rating directive', function () { | |
return state; | ||
} | ||
|
||
function getTitles() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix indentation (if it is a tab character, change to spaces) |
||
var stars = getStars(); | ||
var titles = []; | ||
for (var i = 0, n = stars.length; i < n; i++) { | ||
titles.push( stars.eq(i).attr('title') ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change for loop to
|
||
return titles; | ||
} | ||
|
||
function triggerKeyDown(keyCode) { | ||
var e = $.Event('keydown'); | ||
e.which = keyCode; | ||
|
@@ -267,4 +276,48 @@ describe('rating directive', function () { | |
expect(getState('on', 'off')).toEqual([true, true, true, true, true, false, false, false, false, false]); | ||
}); | ||
}); | ||
|
||
describe('Default title', function() { | ||
it('should return the default title for each star', function() { | ||
expect(getTitles()).toEqual(['bad','poor','regular','good','gorgeous']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spacing of array elements |
||
}); | ||
}); | ||
|
||
describe('shows different title when `max` attribute is greater than the titles array ', function() { | ||
var originalConfig = {}; | ||
beforeEach(inject(function(ratingConfig) { | ||
$rootScope.rate = 5; | ||
angular.extend(originalConfig, ratingConfig); | ||
ratingConfig.max = 10; | ||
element = $compile('<rating ng-model="rate"></rating>')($rootScope); | ||
$rootScope.$digest(); | ||
})); | ||
afterEach(inject(function(ratingConfig) { | ||
// return it to the original state | ||
angular.extend(ratingConfig, originalConfig); | ||
})); | ||
|
||
it('should return the default title for each star', function() { | ||
expect(getTitles()).toEqual(['bad','poor','regular','good','gorgeous','6','7','8','9','10']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spacing of array elements |
||
}); | ||
}); | ||
|
||
describe('shows custom titles ', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix indentation |
||
it('should return the custom title for each star', function() { | ||
element = $compile('<rating ng-model="rate" titles="[44,45,46]"></rating>')($rootScope); | ||
$rootScope.$digest(); | ||
expect(getTitles()).toEqual(['44','45','46','4','5']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spacing |
||
}); | ||
it('should return the default title if the custom title is an empty array', function() { | ||
element = $compile('<rating ng-model="rate" titles="[]"></rating>')($rootScope); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should omit the titles attribute completely |
||
$rootScope.$digest(); | ||
expect(getTitles()).toEqual(['bad','poor','regular','good','gorgeous']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix spacing |
||
}); | ||
it('should return the default title if the custom title is not an array', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix indentation |
||
element = $compile('<rating ng-model="rate" titles="test"></rating>')($rootScope); | ||
$rootScope.$digest(); | ||
expect(getTitles()).toEqual(['bad','poor','regular','good','gorgeous']); | ||
}); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<span ng-mouseleave="reset()" ng-keydown="onKeydown($event)" tabindex="0" role="slider" aria-valuemin="0" aria-valuemax="{{range.length}}" aria-valuenow="{{value}}"> | ||
<i ng-repeat="r in range track by $index" ng-mouseenter="enter($index + 1)" ng-click="rate($index + 1)" class="glyphicon" ng-class="$index < value && (r.stateOn || 'glyphicon-star') || (r.stateOff || 'glyphicon-star-empty')"> | ||
<i ng-repeat="r in range track by $index" ng-mouseenter="enter($index + 1)" ng-click="rate($index + 1)" class="glyphicon" ng-class="$index < value && (r.stateOn || 'glyphicon-star') || (r.stateOff || 'glyphicon-star-empty')" ng-attr-title="{{r.title}}" > | ||
<span class="sr-only">({{ $index < value ? '*' : ' ' }})</span> | ||
</i> | ||
</span> | ||
</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add spaces after each comma