-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplerating.js
198 lines (147 loc) · 6.88 KB
/
simplerating.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/******************************
jquery-simple rating
Author : Charly Martins
Version 1.2
https://github.com/Yosimitso/jquery-simplerating
*******************************/
(function(jQuery) {
jQuery.fn.simplerating = function(options) {
var settings = {
rating_number: 5, // RATING MAX GIVEN BY THE USER
rating_text : {},
image: '', // IMAGE USED FOR THE RATING SYSTEM
hidden_input: 'rating_simplerating', // THE ID AND NAME OF YOUR HIDDEN INPUT CONTAINING THE USER'S CHOICE
image_width: '', // OPTIONNAL, IF YOU NEED TO RESIZE THE IMAGE
initial_rating : 0, // OPTIONNAL, INITIAL RATING
callback_on_click: '' // OPTIONNAL, NAME OF THE FUNCTION WITHOUT () CALLED WHEN THE USER CLICK ON A RATING
};
var unactive = false;
if(options) { jQuery.extend(settings, options); };
$('#'+settings.hidden_input).val(settings.initial_rating); // SET THE VALUE OF THE INITIAL RATING TO THE HIDDEN INPUT
/* TEST ON SETTINGS */
if (settings.image == '' || settings.image == null) // IMAGE PROVIDED
{
trigger_error(this,'Simplerating : You must provide an image url');
return false;
}
if (settings.rating_number <= 0 || settings.rating_number == null)
{
trigger_error(this,'Simplerating : You must provide a rating number');
return false;
}
if (settings.hidden_input)
{
if (jQuery('#'+settings.hidden_input).length == 0) // CREATING THE HIDDEN INPUT
{
jQuery(this).append('<input type="hidden" autocomplete="off" value="'+settings.initial_rating+'" name="'+settings.hidden_input+'" id="'+settings.hidden_input+'">');
}
else
{
$('#'+settings.hidden_input).val(settings.initial_rating); // SET THE INITIAL RATING IF INPUT EXISTS
}
}
else
{
trigger_error(this,'Simplerating : An hidden input is required to store the user\'s choice');
return false;
}
if (!($.isEmptyObject(settings.rating_text))) // IF PROVIDED IT MUST INCLUDE ALL THE RATING NUMBER
{
for (test_text = settings.rating_number; test_text > 0;test_text-- )
{
if (!settings.rating_text[test_text])
{
trigger_error(this,'SimpleRating : The parameter "rating_text" must contains all the rating number available or must be set omitted, missing rating : '+test_text);
return false;
}
}
}
/* END TEST ON SETTINGS */
for (i = 1; i !== (settings.rating_number+1);i++) // PRINT THE RATING'S IMAGE
{
var append = '<img src="'+settings.image+'" width="'+settings.image_width+'" style="margin-left:5px;" id="rate_'+settings.hidden_input+'['+i+']" data="'+i+'" class="img-rate img-rate_'+settings.hidden_input;
if (i >= (settings.initial_rating+1)) // FOR THE INTIAL VALUE
{
append += ' low-opacity';
}
append += '"/>';
jQuery(this).append(append);
}
if (!($.isEmptyObject(settings.rating_text)))
{
number_text = '<div class="simplerating-text" id="simplerating-text_'+settings.hidden_input+'" style="min-height:30px;">';
if (settings.rating_text[parseInt(settings.initial_rating)])
{
number_text += settings.rating_text[parseInt(settings.initial_rating)];
}
number_text += '</div>';
jQuery(this).append(number_text);
}
$('.img-rate_'+settings.hidden_input).hover( function(event) { // WHEN USER HOVER A RATING
choice = parseInt($(this).attr('data'));
if (!unactive)
{
for (i = 1; i <= choice;i++)
{
$('#rate_'+settings.hidden_input+'\\['+i+'\\]').removeClass('low-opacity'); // HIGHLIGHT THE RATING
}
//alert('i = '+(parseInt($(this).attr('data'))+1)+'; i < '+settings.rating_number+1+'; i++');
for (i = choice+1; i <= settings.rating_number; i++) // FADE THE HIGHER RATING
{
$('#rate_'+settings.hidden_input+'\\['+i+'\\]').addClass('low-opacity');
}
if (settings.rating_text[choice])
{
$('#simplerating-text_'+settings.hidden_input).html(settings.rating_text[choice]);
}
}
});
$('.img-rate_'+settings.hidden_input).mouseleave( function(event) { // WHEN USER'S MOUSE LEAVE THE RATING SYSTEM
if (!unactive)
{
var value = parseInt($('#'+settings.hidden_input).val());
if (value === null || value === undefined)
{
value = settings.initial_rating;
}
var rating_number = settings.rating_number;
for (i = value+1; i <= rating_number;i++)
{
$('#rate_'+settings.hidden_input+'\\['+i+'\\]').addClass('low-opacity');
}
for (i = 1; i <= value; i++)
{
$('#rate_'+settings.hidden_input+'\\['+i+'\\]').removeClass('low-opacity');
}
if (settings.rating_text[$('#'+settings.hidden_input).val()]) // PRINT THE TEXT OF THE RANL
{
$('#simplerating-text_'+settings.hidden_input).html(settings.rating_text[parseInt($('#'+settings.hidden_input).val())]);
}
else
{
$('#simplerating-text_'+settings.hidden_input).html('');
}
}
});
$('.img-rate_'+settings.hidden_input).click( function() {
if (!unactive)
{
for (i = 1; i !== (parseInt($(this).attr('data')) +1);i++)
{
$('#rate_'+settings.hidden_input+'\\['+i+'\\]').removeClass('low-opacity');
}
$('#'+settings.hidden_input).val($(this).attr('data'));
if (settings.callback_on_click !== '') // IF A CALLBACK IS NEEDED
{
eval(settings.callback_on_click+'()');
unactive = true; // THE USER CAN'T CHOOSE AN ANOTHER RATING
}
}
});
function trigger_error(myself,message)
{
console.error(message);
jQuery(myself).html('SimpleRating : error, see console log');
}
};
})(jQuery);