-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjquery.webRating.js
483 lines (412 loc) · 17.3 KB
/
jquery.webRating.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*!
* jQuery WebRating - v1.0.03
*
* Copyright 2014, Piyush Ramuka
*
* Dual licensed under the MIT and GPL v2 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*
* jQuery("div").webRating();
*/
;(function($, window, document, undefined)
{
$.fn.webRating = function(settings)
{
/**
* settings and configuration data
* @type {}
*/
var configuration =
{
// general
bind : "load",
// image & color
ratingCount : 5,
imgSrc : null,
xLocation : 0, //in px
yLocation : 0, //in px
width : 0, //in px
height : 0, //in px
//CSS
onClass : null,
offClass : null,
onClassHover : null,
offClassHover : null,
autoParentWidth : true,
//cookie
cookieEnable : true,
cookieLifeDays : 1,
cookiePrefix : "webRating_",
//Tooltip
prefixAverage : "Avg",
tp_showAverage : true,
tp_showMaxScore : true,
tp_showCount : true,
tp_eachStar : null,
//Maths
updateScore : true,
//click
maxClick : 1,
onClick : null,
onHover : null,
// attribute
attribute : "data-webRatingArg",
score : "data-webRating", //Current rating
count : "data-webRatingN" //Current count
};
// overwrite configuration with custom user settings
if( settings ) $.extend(configuration, settings);
// all given items by jQuery selector
var items = this;
//Constants
var minus = '-';
var px = 'px';
var per = '%';
/**
* Update prefixAverage with proper space
*/
var _ratingAvgPrefix = configuration.prefixAverage.trim();
if(_ratingAvgPrefix.length > 0){
configuration.prefixAverage = configuration.prefixAverage.trim() + " ";
}
else{
configuration.prefixAverage = "";
}
// on first page load get initial images
if( configuration.bind == "load" ) $(window).load(_init);
// if event driven don't wait for page loading
else if( configuration.bind == "event" ) _init();
/**
*/
function drawRatingStar(){
// Basic testing
if(!!configuration.imgSrc == false) return;
if(configuration.ratingCount == 0) return;
//Class test
if(!!configuration.onClassHover == false){
configuration.onClassHover = configuration.onClass;
}
if(!!configuration.offClassHover == false){
configuration.offClassHover = configuration.offClass;
}
// cleanup all items which are already loaded or don't have attribute parameter
items = $(items).filter(function(){
var element = $(this);
return (!(element.data('loaded')) && (element.attr(configuration.score)));
});
//Preload Image
var imageObj = $(new Image());
imageObj.attr("src", configuration.imgSrc);
//process each div element
var cookieIndex = 0;
items.each(function(){
//Element
var element = $(this);
element.data('loaded', true);
//Math
var score = +element.attr(configuration.score);
var count = +element.attr(configuration.count);
var argCallback = element.attr(configuration.attribute);
//Create background div
var bgDiv = document.createElement("div");
bgDiv.id = "bgDiv";
bgDiv.style.width = (configuration.width * configuration.ratingCount) + px;
bgDiv.style.height = (configuration.height) + px;
//Set parent width
if(configuration.autoParentWidth){
this.style.width = bgDiv.style.width;
}
//Tool tip average
_addRatingAvgTp($(bgDiv), score, count);
//Data data in object
$(bgDiv).data('clickCount', 0);
$(bgDiv).data('score', score);
$(bgDiv).data('count', count);
$(bgDiv).data('argCallback', argCallback);
$(bgDiv).data('cookieIndex', cookieIndex++);
//Create on div
var onDiv = document.createElement("div");
onDiv.id = "onDiv";
onDiv.setAttribute('class', configuration.onClass);
bgDiv.appendChild(onDiv);
onDiv.style.width = _getOnPercentage(score);
onDiv.style.height = (configuration.height) + px;
onDiv.style.cssFloat = "left";
//Create off div
var offDiv = document.createElement("div");
offDiv.id = "offDiv";
offDiv.setAttribute('class', configuration.offClass);
bgDiv.appendChild(offDiv);
offDiv.style.width = _getOffPercentage(score);
offDiv.style.height = (configuration.height) + px;
offDiv.style.cssFloat = "left";
//Star container
var starContainer = document.createElement("div");
starContainer.id = "starParent";
starContainer.setAttribute("style", "position: absolute;");
starContainer.style.textAlign = "left";
bgDiv.appendChild(onDiv);
bgDiv.appendChild(offDiv);
bgDiv.appendChild(starContainer);
element.append(bgDiv);
//Insert start in each div
for(var i = 0; i < configuration.ratingCount; i++){
//Star Div
var starDiv = document.createElement("div");
starDiv.id = i + 1;
starDiv.style.width = (configuration.width) + px;
starDiv.style.height = (configuration.height) + px;
starDiv.style.cssFloat = "left";
//Add tool tip
if (configuration.tp_eachStar) {
if (configuration.tp_eachStar[i + 1]) {
starDiv.title = configuration.tp_eachStar[i + 1];
}
}
//Star Image
var starImg = document.createElement("img");
starImg.setAttribute("src", imageObj.attr("src"));
starImg.style.marginLeft = minus + configuration.xLocation + px;
starImg.style.marginTop = minus + configuration.yLocation + px;
starImg.style.clip = "rect(" + configuration.yLocation + "px," +
(+configuration.xLocation + +configuration.width) + "px,"
+ (+configuration.yLocation + +configuration.height) + "px," + configuration.xLocation + "px)";
starImg.style.position = "absolute";
starImg.style.maxWidth = 'none';
starDiv.appendChild(starImg);
starContainer.appendChild(starDiv);
}
//Return if not clicks allowed
if (_ratingRemainingClick($(bgDiv)) < 1) {
_removeRatingStarTitle($(starContainer));
return;
}
//Add hover effect
$(starContainer).on({
mouseover: function() {
//Check if maximum rating as been made
if (_ratingRemainingClick($(bgDiv)) < 1) {
_removeRatingStarTitle($(starContainer));
return;
}
//Maths
var hoverScore = $(this).attr("id");
onDiv.style.width = _getOnPercentage(hoverScore);
offDiv.style.width = _getOffPercentage(hoverScore);
//OnHover function call
var argCallback = $(bgDiv).data("argCallback");
configuration.onHover && configuration.onHover(hoverScore, argCallback);
},
click: function() {
//Get relevant objs
var $starDiv = $(this);
var $starContainer = $(starContainer);
var $bgDiv = $(bgDiv);
//Check if maximum rating as been made
if (_ratingRemainingClick($bgDiv) < 1) {
_removeRatingStarTitle($starContainer);
return;
}
_ratingUpdateClickCount($bgDiv, 1);
//Math
var clickScore = +$starDiv.attr("id");
//Update score if enabled
var score = +$bgDiv.data("score");
var count = +$bgDiv.data("count");
if (configuration.updateScore && (count >= 0)) {
score = ((score * count) + clickScore) / (count + 1), 2;
count++;
$bgDiv.data("score", score);
$bgDiv.data("count", count);
//Toop tip average
_addRatingAvgTp($bgDiv, score, count);
//Update display
onDiv.style.width = _getOnPercentage(score);
offDiv.style.width = _getOffPercentage(score);
}
//Change the cursor
$bgDiv.css('cursor', 'default');
//Change hover class
onDiv.className = configuration.onClass;
offDiv.className = configuration.offClass;
//Make data object for callback function
var argCallback = $bgDiv.data("argCallback");
configuration.onClick && configuration.onClick(clickScore, argCallback);
},
}, "div");
//Mouse enter & leave effect
$(bgDiv).on({
mouseenter: function() {
//Get relevant objs
var bgDiv = $(this);
var onDiv = (bgDiv.children().get(0));
var offDiv = (bgDiv.children().get(1));
var starContainer = $(bgDiv.children().get(2));
//Check if maximum rating as been made
if (_ratingRemainingClick(bgDiv) < 1) {
_removeRatingStarTitle(starContainer);
return;
}
//Change hover class
onDiv.className = configuration.onClassHover;
offDiv.className = configuration.offClassHover;
//Change the cursor
bgDiv.css('cursor', 'pointer');
},
mouseleave: function() {
//Get relevant objs
var bgDiv = $(this);
var onDiv = (bgDiv.children().get(0));
var offDiv = (bgDiv.children().get(1));
//Change the cursor
bgDiv.css('cursor', 'default');
//Change hover class
onDiv.className = configuration.onClass;
offDiv.className = configuration.offClass;
//Maths
var hoverScore = bgDiv.data("score");
onDiv.style.width = _getOnPercentage(hoverScore);
offDiv.style.width = _getOffPercentage(hoverScore);
},
});
});
}
/**
* _init()
*
* initialize lazy plugin
* bind loading to events or set delay time to load all images at once
*
* @return void
*/
function _init(){
//Draw all starts with initial rating
drawRatingStar();
}
/**
* Adds toop tip to the input div
*/
function _addRatingAvgTp($divElement, score, clickCount) {
var str = "";
//Add score first
if (configuration.tp_showAverage) {
str += configuration.prefixAverage + score.toFixed(2);
}
//Add max score
if (configuration.tp_showMaxScore) {
if (str.length > 0)
str += "/";
str += configuration.ratingCount;
}
//Add count
if (configuration.tp_showCount) {
if ((clickCount) && (clickCount > 0)) {
if (str.length > 0)
str += " ";
str += "(" + clickCount + ")";
}
}
//Append to div
$divElement && ($divElement.attr('title', str));
return str;
}
/**
* get on percentage
*/
function _getOnPercentage(score) {
return ((score / configuration.ratingCount) * 100) + per;
}
/**
* get off percentage
*/
function _getOffPercentage(score) {
return (((configuration.ratingCount - score) / configuration.ratingCount) * 100) + per;
}
/**
* Checks if number of clicks has maxed out or not
* return: count remaining clicks
*/
function _ratingRemainingClick(dataDiv) {
//Read data from cookie
var clickCount = +dataDiv.data('clickCount');
if (configuration.cookieEnable) {
var _cookie = +_readCookieRating(configuration.cookiePrefix + dataDiv.data('cookieIndex'));
if (!!_cookie == false) {
_cookie = 0;
}
clickCount = (_cookie > clickCount) ? _cookie : clickCount;
}
//Check click count
return (configuration.maxClick - clickCount);
}
/**
Remove individual rating start title
*/
function _removeRatingStarTitle(parentElement) {
var starDiv = parentElement.children();
var maxCount = starDiv.size();
for (var i = 0; i < maxCount; i++) {
$(starDiv.get(i)).removeAttr('title');
}
}
/**
* Checks if number of clicks has maxed out or not
* return: void
*/
function _ratingUpdateClickCount(dataDiv, incrementCount) {
//Get click count
var clickCount = +dataDiv.data('clickCount');
if (configuration.cookieEnable) {
var _cookie = +_readCookieRating(configuration.cookiePrefix + dataDiv.data('cookieIndex'));
if (!!_cookie == false) {
_cookie = 0;
}
clickCount = (_cookie > clickCount) ? _cookie : clickCount;
}
//Update count in memory & cookie
$(dataDiv).data('clickCount', clickCount + incrementCount);
if (configuration.cookieEnable) {
_eraseCookieRating(configuration.cookiePrefix + dataDiv.data('cookieIndex'));
_createCookieRating(configuration.cookiePrefix + dataDiv.data('cookieIndex'), clickCount + incrementCount, configuration.cookieLifeDays);
}
}
/**
* Create cookie
*/
function _createCookieRating(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else
var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
/**
* Read cookie
*/
function _readCookieRating(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ')
c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
/**
* delete cookie
*/
function _eraseCookieRating(name) {
_createCookieRating(name, "", -1);
}
return this;
};
// make lazy a bit more case-insensitive :)
$.fn.WebRating = $.fn.webRating;
})(jQuery, window, document);