forked from javiertoledo/bootstrap-rating-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-rating-input.js
111 lines (97 loc) · 3.67 KB
/
bootstrap-rating-input.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
(function ($) {
$.fn.rating = function () {
var element;
// A private function to highlight a star corresponding to a given value
function _paintValue(ratingInput, value) {
var selectedStar = $(ratingInput).find('i[data-value=' + value + ']');
selectedStar.removeClass('glyphicon glyphicon-star-empty').addClass('glyphicon glyphicon-star');
selectedStar.prevAll('i').removeClass('glyphicon glyphicon-star-empty').addClass('glyphicon glyphicon-star');
selectedStar.nextAll('i').removeClass('glyphicon glyphicon-star').addClass('glyphicon glyphicon-star-empty');
}
// A private function to remove the selected rating
function _clearValue(ratingInput) {
var self = $(ratingInput);
self.find('i').removeClass('glyphicon glyphicon-star').addClass('glyphicon glyphicon-star-empty');
self.find('.rating-clear').hide();
self.find('input').val('').trigger('change');
}
// Iterate and transform all selected inputs
for (element = this.length - 1; element >= 0; element--) {
var el, i, ratingInputs,
originalInput = $(this[element]),
max = originalInput.data('max') || 5,
min = originalInput.data('min') || 0,
clearable = originalInput.data('clearable') || null,
stars = '';
// HTML element construction
for (i = min; i <= max; i++) {
// Create <max> empty stars
stars += ['<i class="glyphicon glyphicon-star-empty" data-value="', i, '"></i>'].join('');
}
// Add a clear link if clearable option is set
if (clearable) {
stars += [
' <a class="rating-clear" style="display:none;" href="javascript:void"><i class="icon-remove"></i> ',
clearable,
'</a>'].join('');
}
el = [
// Rating widget is wrapped inside a div
'<div class="rating-input">',
stars,
// Value will be hold in a hidden input with same name and id than original input so the form will still work
'<input type="hidden" name="',
originalInput.attr('name'),
'" value="',
originalInput.val(),
'" id="',
originalInput.attr('id'),
'" />',
'</div>'].join('');
// Replace original inputs HTML with the new one
originalInput.replaceWith(el);
}
// Give live to the newly generated widgets
$('.rating-input')
// Highlight stars on hovering
.on('mouseenter', 'i', function () {
var self = $(this);
_paintValue(self.parent(), self.data('value'));
})
// View current value while mouse is out
.on('mouseleave', 'i', function () {
var self = $(this);
var val = self.siblings('input').val();
if (val) {
_paintValue(self.parent(), val);
} else {
_clearValue(self.parent());
}
})
// Set the selected value to the hidden field
.on('click', 'i', function () {
var self = $(this);
var val = self.data('value');
self.siblings('input').val(val).trigger('change');
self.siblings('.rating-clear').show();
})
// Remove value on clear
.on('click', '.rating-clear', function () {
_clearValue($(this).parent());
})
// Initialize view with default value
.each(function () {
var val = $(this).find('input').val();
if (val) {
_paintValue(this, val);
$(this).find('.rating-clear').show();
}
});
};
// Auto apply conversion of number fields with class 'rating' into rating-fields
$(function () {
if ($('input.rating[type=number]').length > 0) {
$('input.rating[type=number]').rating();
}
});
}(jQuery));