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

Commit 5b1115e

Browse files
Sean Desmondbekos
Sean Desmond
authored andcommitted
feat(rating): added onHover and onLeave.
1 parent 0159132 commit 5b1115e

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

src/rating/docs/demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div ng-controller="RatingDemoCtrl">
2-
<rating value="rate" max="10" readonly="isReadonly"></rating>
2+
<rating value="rate" max="10" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null"></rating>
33

44
<hr/>
5-
<pre>Rate: <b>{{rate}}</b> - Readonly is: <i>{{isReadonly}}</i></pre>
5+
<pre>Rate: <b>{{rate}}</b> - Readonly is: <i>{{isReadonly}}</i> - Hovering over: <b>{{overStar || "none"}}</b></pre>
66

77
<hr/>
88
<button class="btn btn-small btn-danger" ng-click="rate = 0" ng-disabled="isReadonly">Clear</button>

src/rating/docs/demo.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
var RatingDemoCtrl = function ($scope) {
22
$scope.rate = 7;
33
$scope.isReadonly = false;
4+
$scope.hoveringOver = function(value) {
5+
$scope.overStar = value;
6+
};
47
};

src/rating/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Rating directive that will take care of visualising a star rating bar.
22

3-
It also provides optional attribute `max` to vary the number of stars and `readonly` attribute to diasble user's interaction.
3+
It also provides optional attributes: `max` to vary the number of stars, `readonly` to disable user's interaction, `on-hover` to signal when the user's mouse is over a particular star, and `on-leave` to signal when the mouse leaves the control altogether.

src/rating/rating.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ angular.module('ui.bootstrap.rating', [])
88
return {
99
restrict: 'EA',
1010
scope: {
11-
value: '='
11+
value: '=',
12+
onHover: '&',
13+
onLeave: '&'
1214
},
1315
templateUrl: 'template/rating/rating.html',
1416
replace: true,
@@ -31,10 +33,12 @@ angular.module('ui.bootstrap.rating', [])
3133
if ( ! scope.readonly ) {
3234
scope.val = value;
3335
}
36+
scope.onHover({value: value});
3437
};
3538

3639
scope.reset = function() {
3740
scope.val = angular.copy(scope.value);
41+
scope.onLeave();
3842
};
3943
scope.reset();
4044

src/rating/test/rating.spec.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('rating directive', function () {
2727
expect(getState(stars)).toEqual([true, true, true, false, false]);
2828
});
2929

30-
it('handles correcty the click event', function() {
30+
it('handles correctly the click event', function() {
3131
var stars = element.find('i');
3232

3333
var star2 = stars.eq(1);
@@ -43,7 +43,7 @@ describe('rating directive', function () {
4343
expect($rootScope.rate).toBe(5);
4444
});
4545

46-
it('handles correcty the hover event', function() {
46+
it('handles correctly the hover event', function() {
4747
var stars = element.find('i');
4848

4949
var star2 = stars.eq(1);
@@ -99,6 +99,28 @@ describe('rating directive', function () {
9999
expect(getState(stars)).toEqual([true, true, true, true, true]);
100100
});
101101

102+
it('should fire onHover', function() {
103+
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');
104+
105+
element = $compile('<rating value="rate" on-hover="hoveringOver(value)"></rating>')($rootScope);
106+
$rootScope.$digest();
107+
108+
var star3 = element.find('i').eq(2);
109+
star3.trigger('mouseover');
110+
$rootScope.$digest();
111+
expect($rootScope.hoveringOver).toHaveBeenCalledWith(3);
112+
});
113+
114+
it('should fire onLeave', function() {
115+
$rootScope.leaving = jasmine.createSpy('leaving');
116+
117+
element = $compile('<rating value="rate" on-leave="leaving()"></rating>')($rootScope);
118+
$rootScope.$digest();
119+
120+
element.trigger('mouseleave');
121+
$rootScope.$digest();
122+
expect($rootScope.leaving).toHaveBeenCalled();
123+
});
102124
});
103125

104126
describe('setting ratingConfig', function() {
@@ -123,6 +145,4 @@ describe('setting ratingConfig', function() {
123145
it('should change number of icon elements', function () {
124146
expect(element.find('i').length).toBe(10);
125147
});
126-
127-
});
128-
148+
});

template/rating/rating.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<span ng-mouseleave="reset()">
22
<i ng-repeat="number in range" ng-mouseenter="enter(number)" ng-click="rate(number)" ng-class="{'icon-star': number <= val, 'icon-star-empty': number > val}"></i>
3-
</span>
3+
</span>

0 commit comments

Comments
 (0)