_jquery.rating.js
3.61 KB
!function(t){t.rating=function(s,i){this.options=t.extend({fx:"float",image:"images/stars.png",width:15,stars:5,minimal:0,titles:["голос","голоса","голосов"],readOnly:!1,url:"",type:"post",loader:"images/ajax-loader.gif",click:function(){}},i||{}),this.el=t(s),this.left=0,this.val=parseFloat(t(".val",s).val())||0,this.val>this.options.stars&&(this.val=this.options.stars),this.val<0&&(this.val=0),this.old=this.val,this.votes=parseInt(t(".votes",s).val())||"",this.voteID=t(".vote-id",s).val()||"",this.vote_wrap=t('<div class="vote-wrap"></div>'),this.vote_block=t('<div class="vote-block"></div>'),this.vote_hover=t('<div class="vote-hover"></div>'),this.vote_stars=t('<div class="vote-stars"></div>'),this.vote_active=t('<div class="vote-active"></div>'),this.vote_result=t('<div class="vote-result"></div>'),this.vote_success=t('<div class="vote-success"></div>'),this.loader=t('<img src="'+this.options.loader+'" alt="load...">'),this.init()};var s=t.rating;s.fn=s.prototype={rating:"0.1"},s.fn.extend=s.extend=t.extend,s.fn.extend({init:function(){if(this.render(),!this.options.readOnly){var s=this,i=0,o=0;this.vote_hover.bind("mousemove mouseover",function(e){if(!s.options.readOnly){var n=t(this),a=0;i=e.clientX>0?e.clientX:e.pageX,o=i-n.offset().left-2;var h=s.options.width*s.options.stars,v=s.options.minimal*s.options.width;o>h&&(o=h),v>o&&(o=v),a=Math.round(o/s.options.width*10)/10,"half"==s.options.fx?o=Math.ceil(o/s.options.width*2)*s.options.width/2:"float"!=s.options.fx&&(o=Math.ceil(o/s.options.width)*s.options.width),a=Math.round(o/s.options.width*10)/10,s.vote_active.css({width:o,"background-position":"0 15px"}),s.vote_success.html("Оценка: "+a+"из 5")}}).bind("mouseout",function(){s.options.readOnly||(s.reset(),s.vote_success.empty())}).bind("click.rating",function(){if(!s.options.readOnly){var t=Math.round(o/s.options.width*10)/10;t>s.options.stars&&(t=s.options.stars),0>t&&(t=0),s.old=s.val,s.val=(s.val*s.votes+t)/(s.votes+1),s.val.toFixed(1),s.vote_success.html("Ваша оценка: "+t),""!=s.options.url&&s.send(t),s.options.readOnly=!0,s.options.click.apply(this,[t])}})}},set:function(){this.vote_active.css({width:this.val*this.options.width,"background-position":"0 15px"})},reset:function(){this.vote_active.css({width:this.old*this.options.width,"background-position":"0 15px"})},setvoters:function(){this.vote_result.html(this.declOfNum(this.votes))},render:function(){this.el.html(this.vote_wrap.append(this.vote_hover.css({padding:"0 4px",height:this.options.width,width:this.options.width*this.options.stars}),this.vote_result.text(this.declOfNum(this.votes)),this.vote_success)),this.vote_block.append(this.vote_stars.css({height:this.options.width,width:this.options.width*this.options.stars,background:"url('"+this.options.image+"') 0 0"}),this.vote_active.css({height:this.options.width,width:this.val*this.options.width,background:"url('"+this.options.image+"') 0 15px"})).appendTo(this.vote_hover)},send:function(s){var i=this;this.vote_result.html(this.loader),t.ajax({url:i.options.url,type:i.options.type,data:{id:this.voteID,score:s},dataType:"json",success:function(t){"OK"==t.status?(i.votes++,i.set()):i.reset(),i.setvoters(),t.msg&&i.vote_success.html(t.msg)}})},declOfNum:function(t){return 0>=t?"":(t=Math.abs(Math.floor(t)),cases=[2,0,1,1,1,2],t+" "+this.options.titles[t%100>4&&20>t%100?2:cases[5>t%10?t%10:5]])}}),t.fn.rating=function(i){if("string"==typeof i){var o=t(this).data("rating"),e=Array.prototype.slice.call(arguments,1);return o[i].apply(o,e)}return this.each(function(){var o=t(this).data("rating");o?(i&&t.extend(o.options,i),o.init()):t(this).data("rating",new s(this,i))})}}(jQuery);