Skip to content

Instantly share code, notes, and snippets.

@mokus80
Created March 25, 2015 11:00
Show Gist options
  • Save mokus80/e48d9f92bbcac8d6a5fa to your computer and use it in GitHub Desktop.
Save mokus80/e48d9f92bbcac8d6a5fa to your computer and use it in GitHub Desktop.
(function(i){function q(e){var j=e.originalEvent.changedTouches[0],f="";switch(e.type){case "touchmove":f="mousemove";break;case "touchend":f="mouseup";break;default:return}var l=document.createEvent("MouseEvent");l.initMouseEvent(f,true,true,window,1,j.screenX,j.screenY,j.clientX,j.clientY,false,false,false,false,0,null);j.target.dispatchEvent(l);e.preventDefault()}i.fn.rateit=function(e,j){var f={},l="init";if(this.length==0)return this;var m=i.type(e);if(m=="object"||e===undefined||e==null)f=i.extend({},
i.fn.rateit.defaults,e);else if(m=="string"&&j===undefined)return this.data("rateit-"+e);else if(m=="string")l="setvalue";return this.each(function(){var c=i(this),a=function(g,h){return c.data("rateit-"+g,h)};c.hasClass("rateit")||c.addClass("rateit");var n=c.css("direction")!="rtl";if(l=="setvalue"){if(!a("init"))throw"Can't set value before init";if(e=="readonly"&&!a("readonly")){c.find(".rateit-range").unbind();a("wired",false)}if(a("backingfld")){var b=i(a("backingfld"));e=="value"&&b.val(j);
if(e=="min"&&b[0].min)b[0].min=j;if(e=="max"&&b[0].max)b[0].max=j;if(e=="step"&&b[0].step)b[0].step=j}a(e,j)}if(!a("init")){a("min",a("min")||f.min);a("max",a("max")||f.max);a("step",a("step")||f.step);a("readonly",a("readonly")!==undefined?a("readonly"):f.readonly);a("resetable",a("resetable")!==undefined?a("resetable"):f.resetable);a("backingfld",a("backingfld")||f.backingfld);a("starwidth",a("starwidth")||f.starwidth);a("starheight",a("starheight")||f.starheight);a("value",a("value")||f.min);if(a("backingfld")){b=
i(a("backingfld"));a("value",b.hide().val());if(b[0].nodeName=="INPUT")if(b[0].type=="range"||b[0].type=="text"){a("min",parseInt(b.attr("min"))||a("min"));a("max",parseInt(b.attr("max"))||a("max"));a("step",parseInt(b.attr("step"))||a("step"))}if(b[0].nodeName=="SELECT"&&b[0].options.length>1){a("min",Number(b[0].options[0].value));a("max",Number(b[0].options[b[0].length-1].value));a("step",Number(b[0].options[1].value)-Number(b[0].options[0].value))}}c.append('<div class="rateit-reset"></div><div class="rateit-range"><div class="rateit-selected" style="height:'+
a("starheight")+'px"></div><div class="rateit-hover" style="height:'+a("starheight")+'px"></div></div>');if(!n){c.find(".rateit-reset").css("float","right");c.find(".rateit-selected").addClass("rateit-selected-rtl");c.find(".rateit-hover").addClass("rateit-hover-rtl")}a("init",true)}var d=c.find(".rateit-range");d.width(a("starwidth")*(a("max")-a("min"))).height(a("starheight"));if(a("value")){var o=(a("value")-a("min"))*a("starwidth");c.find(".rateit-selected").width(o)}b=c.find(".rateit-reset");
var p=function(g,h){var k=(h.changedTouches?h.changedTouches[0].pageX:h.pageX)-i(g).offset().left;n||(k=d.width()-k);if(k>d.width())k=d.width();if(k<0)k=0;return o=Math.ceil(k/a("starwidth")*(1/a("step")))};if(a("readonly"))b.hide();else{a("resetable")?b.click(function(){a("value",a("min"));d.find(".rateit-hover").hide().width(0);d.find(".rateit-selected").width(0).show();a("backingfld")&&i(a("backingfld")).val(a("min"));c.trigger("reset")}):b.hide();if(!a("wired")){d.bind("touchmove touchend",q);
d.mousemove(function(g){g=p(this,g);var h=g*a("starwidth")*a("step"),k=d.find(".rateit-hover");if(k.data("width")!=h){d.find(".rateit-selected").hide();k.width(h).show().data("width",h);c.trigger("hover",[g*a("step")+a("min")])}});d.mouseleave(function(){d.find(".rateit-hover").hide().width(0).data("width","");c.trigger("hover",[null]);d.find(".rateit-selected").show()});d.mouseup(function(g){g=p(this,g);var h=g*a("step")+a("min");a("value",h);a("backingfld")&&i(a("backingfld")).val(h);d.find(".rateit-hover").hide();
d.find(".rateit-selected").width(g*a("starwidth")*a("step")).show();c.trigger("hover",[null]).trigger("rated",[h])});a("wired",true)}a("resetable")&&b.show()}})};i.fn.rateit.defaults={min:0,max:5,step:0.5,starwidth:16,starheight:16,readonly:false,resetable:true};i("div.rateit").rateit()})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment