Skip to content

Commit f8736e2

Browse files
committed
star rating directive
1 parent 799b4c5 commit f8736e2

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

app/directives/star-rating.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Star Rating Directive
3+
*
4+
* @memberOf riccardo
5+
* @description
6+
* Directive that is managing a rating feature.
7+
*/
8+
9+
(function () {
10+
'use strict';
11+
12+
angular.module('riccardo').directive('starRating', starRating);
13+
14+
function starRating() {
15+
return {
16+
restrict: 'EA',
17+
template:
18+
'<ul class="star-rating" ng-class="{readonly: readonly}">' +
19+
' <li ng-repeat="star in stars" class="star" ng-class="{filled: star.filled}" ng-click="toggle($index)">' +
20+
' &#9733' +
21+
' </li>' +
22+
'</ul>',
23+
scope: {
24+
ratingValue: '=ngModel',
25+
max: '=?', // optional (default is 5)
26+
onRatingSelect: '&?',
27+
readonly: '=?'
28+
},
29+
link: function (scope, element, attributes) {
30+
if (scope.max == undefined) {
31+
scope.max = 5;
32+
}
33+
34+
function updateStars() {
35+
scope.stars = [];
36+
for (var i = 0; i < scope.max; i++) {
37+
scope.stars.push({
38+
filled: i < scope.ratingValue
39+
});
40+
}
41+
}
42+
43+
scope.toggle = function (index) {
44+
if (scope.readonly == undefined || scope.readonly === false) {
45+
scope.ratingValue = index + 1;
46+
scope.onRatingSelect({
47+
rating: index + 1
48+
});
49+
}
50+
};
51+
52+
scope.$watch('ratingValue', function (oldValue, newValue) {
53+
if (newValue || newValue === 0) {
54+
updateStars();
55+
}
56+
});
57+
}
58+
};
59+
}
60+
})();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Star Rating System
2+
3+
.star-rating {
4+
display: inline-block;
5+
margin: 0;
6+
padding: 0;
7+
8+
> .star {
9+
display: inline-block;
10+
font-size: rem(28);
11+
color: $greyDD;
12+
cursor: pointer;
13+
text-shadow: rem(1) rem(1) $greyAA;
14+
list-style-type: none;
15+
padding: 1px;
16+
17+
&.filled {
18+
color: $yellow;
19+
}
20+
}
21+
22+
&.readonly .star.filled {
23+
color: $yellow;
24+
}
25+
}

0 commit comments

Comments
 (0)