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

Commit

Permalink
refactor(rating): move logic to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bekos authored and pkozlowski-opensource committed Aug 17, 2013
1 parent ce226fa commit ed3700b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/rating/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div ng-controller="RatingDemoCtrl">
<rating value="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null"></rating>
<span class="badge badge-inverse" ng-show="overStar && !isReadonly">{{overStar}} / {{max}}</span>
<span class="badge" ng-class="{'badge-warning': percent<30, 'badge-info': percent>=30 && percent<70, 'badge-success': percent>=70}" ng-show="overStar && !isReadonly">{{percent}}%</span>

<hr/>
<pre>Rate: <b>{{rate}}</b> - Readonly is: <i>{{isReadonly}}</i> - Hovering over: <b>{{overStar || "none"}}</b></pre>
Expand Down
2 changes: 2 additions & 0 deletions src/rating/docs/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var RatingDemoCtrl = function ($scope) {
$scope.rate = 7;
$scope.max = 10;
$scope.isReadonly = false;

$scope.hoveringOver = function(value) {
$scope.overStar = value;
$scope.percent = 100 * (value / $scope.max);
};
};
85 changes: 43 additions & 42 deletions src/rating/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,55 @@ angular.module('ui.bootstrap.rating', [])
max: 5
})

.directive('rating', ['ratingConfig', '$parse', function(ratingConfig, $parse) {
.controller('RatingController', ['$scope', '$attrs', '$parse', 'ratingConfig', function($scope, $attrs, $parse, ratingConfig) {

this.maxRange = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max;

$scope.range = [];
for (var i = 1; i <= this.maxRange; i++) {
$scope.range.push(i);
}

$scope.rate = function(value) {
if ( ! $scope.readonly ) {
$scope.value = value;
}
};

$scope.enter = function(value) {
if ( ! $scope.readonly ) {
$scope.val = value;
}
$scope.onHover({value: value});
};

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

$scope.$watch('value', function(value) {
$scope.val = value;
});

$scope.readonly = false;
if ($attrs.readonly) {
$scope.$parent.$watch($parse($attrs.readonly), function(value) {
$scope.readonly = !!value;
});
}
}])

.directive('rating', function() {
return {
restrict: 'EA',
scope: {
value: '=',
onHover: '&',
onLeave: '&'
},
controller: 'RatingController',
templateUrl: 'template/rating/rating.html',
replace: true,
link: function(scope, element, attrs) {

var maxRange = angular.isDefined(attrs.max) ? scope.$parent.$eval(attrs.max) : ratingConfig.max;

scope.range = [];
for (var i = 1; i <= maxRange; i++) {
scope.range.push(i);
}

scope.rate = function(value) {
if ( ! scope.readonly ) {
scope.value = value;
}
};

scope.enter = function(value) {
if ( ! scope.readonly ) {
scope.val = value;
}
scope.onHover({value: value});
};

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

scope.$watch('value', function(value) {
scope.val = value;
});

scope.readonly = false;
if (attrs.readonly) {
scope.$parent.$watch($parse(attrs.readonly), function(value) {
scope.readonly = !!value;
});
}
}
replace: true
};
}]);
});

0 comments on commit ed3700b

Please sign in to comment.