forked from ClockworkMod/clockworkmod.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jRating.jquery.js
217 lines (194 loc) · 6.59 KB
/
jRating.jquery.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/************************************************************************
*************************************************************************
@Name : jRating - jQuery Plugin
@Revison : 2.1
@Date : 26/01/2011
@Author: Surrel Mickael (www.myjqueryplugins.com - www.msconcept.fr)
@License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
**************************************************************************
*************************************************************************/
(function($) {
$.fn.jRating = function(op) {
var defaults = {
/** String vars **/
bigStarsPath : 'jquery/icons/stars.png', // path of the icon stars.png
smallStarsPath : 'jquery/icons/small.png', // path of the icon small.png
phpPath : 'php/jRating.php', // path of the php file jRating.php
type : 'big', // can be set to 'small' or 'big'
/** Boolean vars **/
step:false, // if true, mouseover binded star by star,
isDisabled:false,
showRateInfo: true,
/** Integer vars **/
length:5, // number of star to display
decimalLength : 0, // number of decimals.. Max 3, but you can complete the function 'getNote'
rateMax : 20, // maximal rate - integer from 0 to 9999 (or more)
rateInfosX : -45, // relative position in X axis of the info box when mouseover
rateInfosY : 5, // relative position in Y axis of the info box when mouseover
/** Functions **/
onSuccess : null,
onError : null
};
if(this.length>0)
return this.each(function() {
var opts = $.extend(defaults, op),
newWidth = 0,
starWidth = 0,
starHeight = 0,
bgPath = '';
if($(this).hasClass('jDisabled') || opts.isDisabled)
var jDisabled = true;
else
var jDisabled = false;
getStarWidth();
$(this).height(starHeight);
var average = parseFloat($(this).attr('data').split('_')[0]),
idBox = parseInt($(this).attr('data').split('_')[1]), // get the id of the box
widthRatingContainer = starWidth*opts.length, // Width of the Container
widthColor = average/opts.rateMax*widthRatingContainer, // Width of the color Container
quotient =
$('<div>',
{
'class' : 'jRatingColor',
css:{
width:widthColor
}
}).appendTo($(this)),
average =
$('<div>',
{
'class' : 'jRatingAverage',
css:{
width:0,
top:- starHeight
}
}).appendTo($(this)),
jstar =
$('<div>',
{
'class' : 'jStar',
css:{
width:widthRatingContainer,
height:starHeight,
top:- (starHeight*2),
background: 'url('+bgPath+') repeat-x'
}
}).appendTo($(this));
$(this).css({width: widthRatingContainer,overflow:'hidden',zIndex:1,position:'relative'});
if(!jDisabled)
$(this).bind({
mouseenter : function(e){
var realOffsetLeft = findRealLeft(this);
var relativeX = e.pageX - realOffsetLeft;
if (opts.showRateInfo)
var tooltip =
$('<p>',{
'class' : 'jRatingInfos',
html : getNote(relativeX)+' <span class="maxRate">/ '+opts.rateMax+'</span>',
css : {
top: (e.pageY + opts.rateInfosY),
left: (e.pageX + opts.rateInfosX)
}
}).appendTo('body').show();
},
mouseover : function(e){
$(this).css('cursor','pointer');
},
mouseout : function(){
$(this).css('cursor','default');
average.width(0);
},
mousemove : function(e){
var realOffsetLeft = findRealLeft(this);
var relativeX = e.pageX - realOffsetLeft;
if(opts.step) newWidth = Math.floor(relativeX/starWidth)*starWidth + starWidth;
else newWidth = relativeX;
average.width(newWidth);
if (opts.showRateInfo)
$("p.jRatingInfos")
.css({
left: (e.pageX + opts.rateInfosX)
})
.html(getNote(newWidth) +' <span class="maxRate">/ '+opts.rateMax+'</span>');
},
mouseleave : function(){
$("p.jRatingInfos").remove();
},
click : function(e){
$(this).unbind().css('cursor','default').addClass('jDisabled');
if (opts.showRateInfo) $("p.jRatingInfos").fadeOut('fast',function(){$(this).remove();});
e.preventDefault();
var rate = getNote(newWidth);
average.width(newWidth);
/** ONLY FOR THE DEMO, YOU CAN REMOVE THIS CODE **/
$('.datasSent p').html('<strong>idBox : </strong>'+idBox+'<br /><strong>rate : </strong>'+rate+'<br /><strong>action :</strong> rating');
$('.serverResponse p').html('<strong>Loading...</strong>');
/** END ONLY FOR THE DEMO **/
$.post(opts.phpPath,{
idBox : idBox,
rate : rate,
action : 'rating'
},
function(data) {
if(!data.error)
{
/** ONLY FOR THE DEMO, YOU CAN REMOVE THIS CODE **/
$('.serverResponse p').html(data.server);
/** END ONLY FOR THE DEMO **/
/** Here you can display an alert box,
or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
exemple : */
if(opts.onSuccess) opts.onSuccess();
}
else
{
/** ONLY FOR THE DEMO, YOU CAN REMOVE THIS CODE **/
$('.serverResponse p').html(data.server);
/** END ONLY FOR THE DEMO **/
/** Here you can display an alert box,
or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
exemple : */
if(opts.onError) opts.onError();
}
},
'json'
);
}
});
function getNote(relativeX) {
var noteBrut = parseFloat((relativeX*100/widthRatingContainer)*opts.rateMax/100);
switch(opts.decimalLength) {
case 1 :
var note = Math.round(noteBrut*10)/10;
break;
case 2 :
var note = Math.round(noteBrut*100)/100;
break;
case 3 :
var note = Math.round(noteBrut*1000)/1000;
break;
default :
var note = Math.round(noteBrut*1)/1;
}
return note;
};
function getStarWidth(){
switch(opts.type) {
case 'small' :
starWidth = 12; // width of the picture small.png
starHeight = 10; // height of the picture small.png
bgPath = opts.smallStarsPath;
break;
default :
starWidth = 23; // width of the picture stars.png
starHeight = 20; // height of the picture stars.png
bgPath = opts.bigStarsPath;
}
};
function findRealLeft(obj) {
if( !obj ) return 0;
return obj.offsetLeft + findRealLeft( obj.offsetParent );
};
});
}
})(jQuery);