jquery.raty.min.js
4.24 KB
(function(e){e.fn.raty=function(l){options=e.extend({},e.fn.raty.defaults,l);if(this.attr("id")===undefined){c("Invalid selector!");return;}$this=e(this);if(options.number>20){options.number=20;}if(options.path.substring(options.path.length-1,options.path.length)!="/"){options.path+="/";}var q=$this.attr("id"),x=options.path,v=options.cancelOff,t=options.cancelOn,r=options.showHalf,o=options.starHalf,h=options.starOff,n=options.starOn,s=options.onClick,g=0,m="";if(!isNaN(options.start)&&options.start>0){g=(options.start>options.number)?options.number:options.start;}for(var p=1;p<=options.number;p++){m=(options.number<=options.hintList.length&&options.hintList[p-1]!==null)?options.hintList[p-1]:p;starFile=(g>=p)?n:h;$this.append('<img id="'+q+"-"+p+'" src="'+x+starFile+'" alt="'+p+'" title="'+m+'" class="'+q+'"/>').append((p<options.number)?" ":"");}$this.append('<input id="'+q+'-score" type="hidden" name="'+options.scoreName+'"/>');e("#"+q+"-score").val(g);if(r){var k=e("input#"+q+"-score").val(),j=Math.ceil(k),u=(j-k).toFixed(1);if(u>=0.3&&u<=0.7){j=j-0.5;e("img#"+q+"-"+Math.ceil(j)).attr("src",x+o);}else{if(u>=0.8){j--;}else{e("img#"+q+"-"+j).attr("src",x+n);}}}if(!options.readOnly){if(options.showCancel){var w='<img src="'+x+options.cancelOff+'" alt="x" title="'+options.cancelHint+'" class="button-cancel"/>';if(options.cancelPlace=="left"){$this.prepend(w+" ");}else{$this.append(" ").append(w);}$this.css("width",options.number*20+20);e("#"+q+" img.button-cancel").live("mouseenter",function(){e(this).attr("src",x+t);e("img."+q).attr("src",x+h);}).live("mouseleave",function(){e(this).attr("src",x+v);e("img."+q).trigger("mouseout");}).live("click",function(){e("input#"+q+"-score").val(0);if(s){s(0);}});}else{$this.css("width",options.number*20);}e("img."+q).live("mouseenter",function(){var y=e("img."+q).length;for(var z=1;z<=y;z++){if(z<=this.alt){e("img#"+q+"-"+z).attr("src",x+n);}else{e("img#"+q+"-"+z).attr("src",x+h);}}}).live("click",function(){e("input#"+q+"-score").val(this.alt);if(s){s(this.alt);}});$this.live("mouseleave",function(){var D=e(this).attr("id"),z=e("img."+D).length,C=e("input#"+D+"-score").val();for(var A=1;A<=z;A++){if(A<=C){e("img#"+D+"-"+A).attr("src",x+n);}else{e("img#"+D+"-"+A).attr("src",x+h);}}if(r){var C=e("input#"+D+"-score").val(),y=Math.ceil(C),B=(y-C).toFixed(1);if(B>=0.3&&B<=0.7){y=y-0.5;e("img#"+D+"-"+Math.ceil(y)).attr("src",x+o);}else{if(B>=0.8){y--;}else{e("img#"+D+"-"+y).attr("src",x+n);}}}}).css("cursor","pointer");}else{$this.css("cursor","default");}return $this;};e.fn.raty.defaults={cancelHint:"cancel this rating!",cancelOff:"cancel-off.png",cancelOn:"cancel-on.png",cancelPlace:"left",hintList:["bad","poor","regular","good","gorgeous"],number:5,path:"img/",readOnly:false,scoreName:"score",showCancel:false,showHalf:false,starHalf:"star-half.png",start:0,starOff:"star-off.png",starOn:"star-on.png"};e.fn.raty.readOnly=function(g){if(g){e("img."+$this.attr("id")).die();$this.css("cursor","default").die();}else{d();f();b();$this.css("cursor","pointer");}return e.fn.raty;};e.fn.raty.start=function(g){a(g);return e.fn.raty;};e.fn.raty.click=function(h){var g=(h>=options.number)?options.number:h;a(g);if(options.onClick){options.onClick(g);}else{c('You should add the "onClick: function() {}" option.');}return e.fn.raty;};function d(){var g=$this.attr("id");e("img."+g).live("mouseenter",function(){var h=e("img."+g).length;for(var j=1;j<=h;j++){if(j<=this.alt){e("img#"+g+"-"+j).attr("src",options.path+options.starOn);}else{e("img#"+g+"-"+j).attr("src",options.path+options.starOff);}}});}function f(){$this.live("mouseleave",function(){var k=e(this).attr("id");var g=e("img."+k).length;var j=e("input#"+k+"-score").val();for(var h=1;h<=g;h++){if(h<=j){e("img#"+k+"-"+h).attr("src",options.path+options.starOn);}else{e("img#"+k+"-"+h).attr("src",options.path+options.starOff);}}});}function b(){var g=$this.attr("id");e("img."+g).live("click",function(){e("input#"+g+"-score").val(this.alt);});}function a(k){var j=$this.attr("id"),g=e("img."+j).length;e("input#"+j+"-score").val(k);for(var h=1;h<=g;h++){if(h<=k){e("img#"+j+"-"+h).attr("src",options.path+options.starOn);}else{e("img#"+j+"-"+h).attr("src",options.path+options.starOff);}}}function c(g){if(window.console&&window.console.log){window.console.log(g);}}})(jQuery);