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

Commit ed3700b

Browse files
bekospkozlowski-opensource
authored andcommitted
refactor(rating): move logic to controller
1 parent ce226fa commit ed3700b

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

src/rating/docs/demo.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div ng-controller="RatingDemoCtrl">
22
<rating value="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null"></rating>
3-
<span class="badge badge-inverse" ng-show="overStar && !isReadonly">{{overStar}} / {{max}}</span>
3+
<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>
44

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

src/rating/docs/demo.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ var RatingDemoCtrl = function ($scope) {
22
$scope.rate = 7;
33
$scope.max = 10;
44
$scope.isReadonly = false;
5+
56
$scope.hoveringOver = function(value) {
67
$scope.overStar = value;
8+
$scope.percent = 100 * (value / $scope.max);
79
};
810
};

src/rating/rating.js

+43-42
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,55 @@ angular.module('ui.bootstrap.rating', [])
44
max: 5
55
})
66

7-
.directive('rating', ['ratingConfig', '$parse', function(ratingConfig, $parse) {
7+
.controller('RatingController', ['$scope', '$attrs', '$parse', 'ratingConfig', function($scope, $attrs, $parse, ratingConfig) {
8+
9+
this.maxRange = angular.isDefined($attrs.max) ? $scope.$parent.$eval($attrs.max) : ratingConfig.max;
10+
11+
$scope.range = [];
12+
for (var i = 1; i <= this.maxRange; i++) {
13+
$scope.range.push(i);
14+
}
15+
16+
$scope.rate = function(value) {
17+
if ( ! $scope.readonly ) {
18+
$scope.value = value;
19+
}
20+
};
21+
22+
$scope.enter = function(value) {
23+
if ( ! $scope.readonly ) {
24+
$scope.val = value;
25+
}
26+
$scope.onHover({value: value});
27+
};
28+
29+
$scope.reset = function() {
30+
$scope.val = angular.copy($scope.value);
31+
$scope.onLeave();
32+
};
33+
34+
$scope.$watch('value', function(value) {
35+
$scope.val = value;
36+
});
37+
38+
$scope.readonly = false;
39+
if ($attrs.readonly) {
40+
$scope.$parent.$watch($parse($attrs.readonly), function(value) {
41+
$scope.readonly = !!value;
42+
});
43+
}
44+
}])
45+
46+
.directive('rating', function() {
847
return {
948
restrict: 'EA',
1049
scope: {
1150
value: '=',
1251
onHover: '&',
1352
onLeave: '&'
1453
},
54+
controller: 'RatingController',
1555
templateUrl: 'template/rating/rating.html',
16-
replace: true,
17-
link: function(scope, element, attrs) {
18-
19-
var maxRange = angular.isDefined(attrs.max) ? scope.$parent.$eval(attrs.max) : ratingConfig.max;
20-
21-
scope.range = [];
22-
for (var i = 1; i <= maxRange; i++) {
23-
scope.range.push(i);
24-
}
25-
26-
scope.rate = function(value) {
27-
if ( ! scope.readonly ) {
28-
scope.value = value;
29-
}
30-
};
31-
32-
scope.enter = function(value) {
33-
if ( ! scope.readonly ) {
34-
scope.val = value;
35-
}
36-
scope.onHover({value: value});
37-
};
38-
39-
scope.reset = function() {
40-
scope.val = angular.copy(scope.value);
41-
scope.onLeave();
42-
};
43-
scope.reset();
44-
45-
scope.$watch('value', function(value) {
46-
scope.val = value;
47-
});
48-
49-
scope.readonly = false;
50-
if (attrs.readonly) {
51-
scope.$parent.$watch($parse(attrs.readonly), function(value) {
52-
scope.readonly = !!value;
53-
});
54-
}
55-
}
56+
replace: true
5657
};
57-
}]);
58+
});

0 commit comments

Comments
 (0)