-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrating.js
61 lines (57 loc) · 1.92 KB
/
rating.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
angular.module('angularify.semantic.rating', [])
.directive('rating', function(){
return {
restrict: "E",
replace: true,
scope: {
ngModel : '=',
max: '=',
onRate: '&?',
interactive: '=',
clearable: '='
},
controller: function() {
var vm = this;
if(!vm.max) vm.max = 5;
if(!+vm.ngModel) vm.ngModel = 0;
if(+vm.ngModel > vm.max ) vm.ngModel = +vm.max;
if(vm.interactive == undefined) vm.interactive = true;
if(vm.max == 1 && vm.clearable == undefined) vm.clearable = true;
vm.setRate = function(rate) {
if (vm.clearable && rate == +vm.ngModel) {
vm.ngModel = 0;
} else {
vm.ngModel = rate;
if (angular.isFunction(vm.onRate)) vm.onRate({ rate: rate });
}
};
},
controllerAs: 'vm',
bindToController: true,
require: 'ngModel',
template: '<div class="ui rating" ng-switch="vm.interactive">' +
'<i ng-switch-when="true" ' +
'class="icon" ' +
'ng-class="{ active: vm.current >= rate }" ' +
'ng-click="vm.setRate(rate)" ' +
'ng-mouseenter="vm.current = rate" ' +
'ng-mouseleave="vm.current = +vm.ngModel" ' +
'ng-repeat="rate in vm.range track by $index"' +
'></i>' +
'<i ng-switch-when="false" ' +
'class="icon" ' +
'ng-class="{ active: vm.current >= rate }" ' +
'ng-repeat="rate in vm.range track by $index"' +
'></i>' +
'</div>',
link: function($scope, iElement, iAttrs) {
$scope.$watch('vm.max', function (value) {
$scope.vm.range = Array.apply(null, Array(+value)).map(function (_, i) {return i+1;});
});
$scope.$watch('vm.ngModel', function(value) {
$scope.vm.current = value;
});
}
};
});