-
Notifications
You must be signed in to change notification settings - Fork 46
/
angular-input-stars.js
164 lines (140 loc) · 6 KB
/
angular-input-stars.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
angular.module('angular-input-stars', [])
.directive('inputStars', [function () {
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
var directive = {
restrict: 'EA',
replace: true,
template: '<ul ng-class="listClass">' +
'<li ng-touch="paintStars($index)" ng-mouseenter="paintStars($index, true, $event)" ng-mouseleave="unpaintStars($index, false)" ng-repeat="item in items track by $index">' +
'<i ng-class="getClass($index)" ng-click="setValue($index, $event)"></i>' +
'</li>' +
'</ul>',
require: 'ngModel',
scope:{
bindModel:'=ngModel'
},
link: link
};
return directive;
function link(scope, element, attrs, ngModelCtrl) {
var computed = {
get allowHalf () {
return typeof attrs.allowHalf == 'string' && attrs.allowHalf != 'false'
},
get readonly() {
return attrs.readonly != 'false' && (attrs.readonly || attrs.readonly === '');
},
get fullIcon() {
return attrs.iconFull || 'fa-star';
},
get halfIcon() {
return attrs.iconHalf || 'fa-star-half-o';
},
get emptyIcon() {
return attrs.iconEmpty || 'fa-star-o';
},
get iconBase() {
return attrs.iconBase || 'fa fa-fw';
},
get iconHover() {
return attrs.iconHover || 'angular-input-stars-hover';
}
};
scope.items = new Array(+attrs.max);
scope.listClass = attrs.listClass || 'angular-input-stars';
ngModelCtrl.$render = function () {
if (isFloat(ngModelCtrl.$viewValue)) {
scope.lastValue = (Math.round(ngModelCtrl.$viewValue * 2) / 2)
} else {
scope.lastValue = ngModelCtrl.$viewValue || 0;
}
};
scope.getClass = function (index) {
var icon;
if (index >= scope.lastValue) {
icon = computed.iconBase + ' ' + computed.emptyIcon;
} else {
var isHalf = index + 0.5;
if (computed.allowHalf && isHalf === scope.lastValue) {
icon = computed.iconBase + ' ' + computed.halfIcon + ' active ';
} else {
icon = computed.iconBase + ' ' + computed.fullIcon + ' active ';
}
}
return computed.readonly ? icon + ' readonly' : icon;
};
scope.unpaintStars = function ($index, hover) {
scope.paintStars(scope.lastValue - 1, hover);
};
scope.paintStars = function ($index, hover, $event) {
// ignore painting if readonly
if (computed.readonly) {
return;
}
var items = element.find('li').find('i');
for (var index = 0; index < items.length; index++) {
var $star = angular.element(items[index]);
var classesToRemove;
var classesToAdd;
if ($index >= index) {
classesToRemove = [computed.emptyIcon, computed.halfIcon]
classesToAdd = [computed.iconHover, computed.fullIcon, 'active']
} else {
classesToRemove = [computed.fullIcon, computed.iconHover, computed.halfIcon, 'active']
// isHalf
if (computed.allowHalf && $index + 0.5 === index) {
classesToAdd = [computed.halfIcon, 'active']
} else {
classesToAdd = [computed.emptyIcon]
}
}
$star.removeClass(classesToRemove.join(' '));
$star.addClass(classesToAdd.join(' '));
}
if (! hover) {
items.removeClass(computed.iconHover);
}
};
/**
* Returns whether the user is hovering the first half of the star or not.
*
* @param {MouseEvent} e The mouse event.
* @param {HTMLLIElement} starDOMNode The scope "star" dom node.
* @returns {boolean}
*/
function isHoveringFirstHalf (e, starDOMNode) {
return e.pageX < starDOMNode.getBoundingClientRect().left + starDOMNode.offsetWidth / 2
}
scope.setValue = function (index, e) {
// ignore setting value if readonly
if (computed.readonly) {
return;
}
var star = e.target,
newValue;
if (computed.allowHalf && isHoveringFirstHalf(e, star)) {
newValue = index + 0.5;
} else {
newValue = index + 1;
}
// sets to 0 if the user clicks twice on the first "star"
// the user should be allowed to give a 0 score
if (newValue === scope.lastValue) {
newValue = 0;
}
scope.lastValue = newValue;
ngModelCtrl.$setViewValue(newValue);
ngModelCtrl.$render();
//Execute custom trigger function if there is one
if(attrs.onStarClick){
try {
scope.$parent.$eval(attrs.onStarClick, {$event: e});
} catch (e) {
console.error(e)
}
}
};
}
}]);