forked from PiyushRamuka/jquery.WebRating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.webRating.min.js
12 lines (12 loc) · 5.1 KB
/
jquery.webRating.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* 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").starRating();
*/
;(function(e,t,n,r){e.fn.webRating=function(r){function l(){if(!!i.imgSrc==false)return;if(i.ratingCount==0)return;if(!!i.onClassHover==false){i.onClassHover=i.onClass}if(!!i.offClassHover==false){i.offClassHover=i.offClass}s=e(s).filter(function(){var t=e(this);return!t.data("loaded")&&t.attr(i.score)});var t=e(new Image);t.attr("src",i.imgSrc);var r=0;s.each(function(){var s=e(this);s.data("loaded",true);var a=+s.attr(i.score);var f=+s.attr(i.count);var l=s.attr(i.attribute);var c=n.createElement("div");c.id="bgDiv";c.style.width=i.width*i.ratingCount+u;c.style.height=i.height+u;if(i.autoParentWidth){this.style.width=c.style.width}h(e(c),a,f);e(c).data("clickCount",0);e(c).data("score",a);e(c).data("count",f);e(c).data("argCallback",l);e(c).data("cookieIndex",r++);var y=n.createElement("div");y.id="onDiv";y.setAttribute("class",i.onClass);c.appendChild(y);y.style.width=p(a);y.style.height=i.height+u;y.style.cssFloat="left";var b=n.createElement("div");b.id="offDiv";b.setAttribute("class",i.offClass);c.appendChild(b);b.style.width=d(a);b.style.height=i.height+u;b.style.cssFloat="left";var w=n.createElement("div");w.id="starParent";w.setAttribute("style","position: absolute;");w.style.textAlign="left";c.appendChild(y);c.appendChild(b);c.appendChild(w);s.append(c);for(var E=0;E<i.ratingCount;E++){var S=n.createElement("div");S.id=E+1;S.style.width=i.width+u;S.style.height=i.height+u;S.style.cssFloat="left";if(i.tp_eachStar){if(i.tp_eachStar[E+1]){S.title=i.tp_eachStar[E+1]}}var x=n.createElement("img");x.setAttribute("src",t.attr("src"));x.style.marginLeft=o+i.xLocation+u;x.style.marginTop=o+i.yLocation+u;x.style.clip="rect("+i.yLocation+"px,"+(+i.xLocation+ +i.width)+"px,"+(+i.yLocation+ +i.height)+"px,"+i.xLocation+"px)";x.style.position="absolute";x.style.maxWidth="none";S.appendChild(x);w.appendChild(S)}if(v(e(c))<1){m(e(w));return}e(w).on({mouseover:function(){if(v(e(c))<1){m(e(w));return}var t=e(this).attr("id");y.style.width=p(t);b.style.width=d(t);var n=e(c).data("argCallback");i.onHover&&i.onHover(t,n)},click:function(){var t=e(this);var n=e(w);var r=e(c);if(v(r)<1){m(n);return}g(r,1);var s=+t.attr("id");var o=+r.data("score");var u=+r.data("count");if(i.updateScore&&u>=0){o=(o*u+s)/(u+1),2;u++;r.data("score",o);r.data("count",u);h(r,o,u);y.style.width=p(o);b.style.width=d(o)}r.css("cursor","default");y.className=i.onClass;b.className=i.offClass;var a=r.data("argCallback");i.onClick&&i.onClick(s,a)}},"div");e(c).on({mouseenter:function(){var t=e(this);var n=t.children().get(0);var r=t.children().get(1);var s=e(t.children().get(2));if(v(t)<1){m(s);return}n.className=i.onClassHover;r.className=i.offClassHover;t.css("cursor","pointer")},mouseleave:function(){var t=e(this);var n=t.children().get(0);var r=t.children().get(1);t.css("cursor","default");n.className=i.onClass;r.className=i.offClass;var s=t.data("score");n.style.width=p(s);r.style.width=d(s)}})})}function c(){l()}function h(e,t,n){var r="";if(i.tp_showAverage){r+=i.prefixAverage+t.toFixed(2)}if(i.tp_showMaxScore){if(r.length>0)r+="/";r+=i.ratingCount}if(i.tp_showCount){if(n&&n>0){if(r.length>0)r+=" ";r+="("+n+")"}}e&&e.attr("title",r);return r}function p(e){return e/i.ratingCount*100+a}function d(e){return(i.ratingCount-e)/i.ratingCount*100+a}function v(e){var t=+e.data("clickCount");if(i.cookieEnable){var n=+b(i.cookiePrefix+e.data("cookieIndex"));if(!!n==false){n=0}t=n>t?n:t}return i.maxClick-t}function m(t){var n=t.children();var r=n.size();for(var i=0;i<r;i++){e(n.get(i)).removeAttr("title")}}function g(t,n){var r=+t.data("clickCount");if(i.cookieEnable){var s=+b(i.cookiePrefix+t.data("cookieIndex"));if(!!s==false){s=0}r=s>r?s:r}e(t).data("clickCount",r+n);if(i.cookieEnable){w(i.cookiePrefix+t.data("cookieIndex"));y(i.cookiePrefix+t.data("cookieIndex"),r+n,i.cookieLifeDays)}}function y(e,t,r){if(r){var i=new Date;i.setTime(i.getTime()+r*24*60*60*1e3);var s="; expires="+i.toGMTString()}else var s="";n.cookie=e+"="+t+s+"; path=/"}function b(e){var t=e+"=";var r=n.cookie.split(";");for(var i=0;i<r.length;i++){var s=r[i];while(s.charAt(0)==" ")s=s.substring(1,s.length);if(s.indexOf(t)==0)return s.substring(t.length,s.length)}return null}function w(e){y(e,"",-1)}var i={bind:"load",ratingCount:5,imgSrc:null,xLocation:0,yLocation:0,width:0,height:0,onClass:null,offClass:null,onClassHover:null,offClassHover:null,autoParentWidth:true,cookieEnable:true,cookieLifeDays:1,cookiePrefix:"webRating_",prefixAverage:"Avg",tp_showAverage:true,tp_showMaxScore:true,tp_showCount:true,tp_eachStar:null,updateScore:true,maxClick:1,onClick:null,onHover:null,attribute:"data-webRatingArg",score:"data-webRating",count:"data-webRatingN"};if(r)e.extend(i,r);var s=this;var o="-";var u="px";var a="%";var f=i.prefixAverage.trim();if(f.length>0){i.prefixAverage=i.prefixAverage.trim()+" "}else{i.prefixAverage=""}if(i.bind=="load")e(t).load(c);else if(i.bind=="event")c();return this};e.fn.WebRating=e.fn.webRating})(jQuery,window,document);