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

Commit

Permalink
feat(rating): added onHover and onLeave.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Desmond authored and bekos committed Jun 29, 2013
1 parent 0159132 commit 5b1115e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/rating/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div ng-controller="RatingDemoCtrl">
<rating value="rate" max="10" readonly="isReadonly"></rating>
<rating value="rate" max="10" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null"></rating>

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

<hr/>
<button class="btn btn-small btn-danger" ng-click="rate = 0" ng-disabled="isReadonly">Clear</button>
Expand Down
3 changes: 3 additions & 0 deletions src/rating/docs/demo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var RatingDemoCtrl = function ($scope) {
$scope.rate = 7;
$scope.isReadonly = false;
$scope.hoveringOver = function(value) {
$scope.overStar = value;
};
};
2 changes: 1 addition & 1 deletion src/rating/docs/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Rating directive that will take care of visualising a star rating bar.

It also provides optional attribute `max` to vary the number of stars and `readonly` attribute to diasble user's interaction.
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.
6 changes: 5 additions & 1 deletion src/rating/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ angular.module('ui.bootstrap.rating', [])
return {
restrict: 'EA',
scope: {
value: '='
value: '=',
onHover: '&',
onLeave: '&'
},
templateUrl: 'template/rating/rating.html',
replace: true,
Expand All @@ -31,10 +33,12 @@ angular.module('ui.bootstrap.rating', [])
if ( ! scope.readonly ) {
scope.val = value;
}
scope.onHover({value: value});
};

scope.reset = function() {
scope.val = angular.copy(scope.value);
scope.onLeave();
};
scope.reset();

Expand Down
30 changes: 25 additions & 5 deletions src/rating/test/rating.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('rating directive', function () {
expect(getState(stars)).toEqual([true, true, true, false, false]);
});

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

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

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

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

it('should fire onHover', function() {
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');

element = $compile('<rating value="rate" on-hover="hoveringOver(value)"></rating>')($rootScope);
$rootScope.$digest();

var star3 = element.find('i').eq(2);
star3.trigger('mouseover');
$rootScope.$digest();
expect($rootScope.hoveringOver).toHaveBeenCalledWith(3);
});

it('should fire onLeave', function() {
$rootScope.leaving = jasmine.createSpy('leaving');

element = $compile('<rating value="rate" on-leave="leaving()"></rating>')($rootScope);
$rootScope.$digest();

element.trigger('mouseleave');
$rootScope.$digest();
expect($rootScope.leaving).toHaveBeenCalled();
});
});

describe('setting ratingConfig', function() {
Expand All @@ -123,6 +145,4 @@ describe('setting ratingConfig', function() {
it('should change number of icon elements', function () {
expect(element.find('i').length).toBe(10);
});

});

});
2 changes: 1 addition & 1 deletion template/rating/rating.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<span ng-mouseleave="reset()">
<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>
</span>
</span>

0 comments on commit 5b1115e

Please sign in to comment.