rate-it/public/js/rateit.js

1 line
12 KiB
JavaScript

function doRateIt(){if(window.MooTools){var e=new Class({Implements:Options,options:{step:.1,readonly:false,resetable:false},initialize:function(e){this.setOptions(e);$$(".rateItRating").each(function(e){this.initMe(e)}.bind(this))},initMe:function(e){if(!Browser.ie||Browser.version>6){e.id=e.getAttribute("id");e.rateable=e.getAttribute("rel")=="not-rateable"?false:true;e.wrapper=e.getElement(".wrapper");e.textEl=e.getElement(".ratingText");e.selected=e.wrapper.getElement(".rateItRating-selected");e.hover=e.wrapper.getElement(".rateItRating-hover");e.widthFx=new Fx.Tween(e.selected,{property:"width",link:"chain"});var t=this.getBackgroundImage(e.wrapper);this.options.starwidth=t.width;this.options.starheight=t.height/3;if(this.options.starwidth===undefined||this.options.starwidth<16){this.options.starwidth=16}if(this.options.starheight===undefined||this.options.starheight<16){this.options.starheight=16}this.setBackgroundPosition(e.selected,-1*this.options.starheight);this.setBackgroundPosition(e.hover,-1*2*this.options.starheight);e.starPercent=this.getStarPercent(e.id);e.ratableId=this.getRatableId(e.id);e.ratableType=this.getRatableType(e.id);this.options.max=this.getRatableMaxValue(e.id);e.selected.setStyle("height",this.options.starheight);e.hover.setStyle("height",this.options.starheight);e.wrapper.setStyle("width",this.options.starwidth*this.options.max);e.wrapper.setStyle("height",this.options.starheight);this.fillVote(e.starPercent,e);e.currentFill=this.getFillPercent(e.starPercent);if(e.rateable){e.mouseCrap=function(t){var n=t.event.layerX;if(!n){n=t.event.offsetX}var r=this.getVotePercent(n);var i=Math.ceil(r/100*this.options.max);var s=i*this.options.starwidth;if(e.hover.getStyle("width").toInt()!=s){e.selected.setStyle("display","none");e.hover.setStyle("width",Math.min(s,this.options.starwidth*this.options.max));e.hover.setStyle("display","block")}var o=i/this.options.max*100;this.fillVote(o,e)}.bind(this);e.wrapper.addEvent("mouseenter",function(t){e.wrapper.addEvent("mousemove",e.mouseCrap)});e.wrapper.addEvent("mouseleave",function(t){e.removeEvent("mousemove");e.hover.setStyle("width",0);e.hover.setStyle("display","none");e.selected.setStyle("display","block");e.widthFx.start(e.currentFill)});e.wrapper.addEvent("click",function(t){e.currentFill=e.newFill;e.wrapper.removeEvents();e.textEl.oldTxt=e.textEl.get("text");e.textEl.set("html","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");e.textEl.addClass("loading");if(typeof $(".mbrateItRating")!="undefined"&&e.id.indexOf("mb")==0){var n=e.id;n=n.replace("mb","");if(typeof arrRatings=="object"){for(var r=0;r<arrRatings.length;r++){if(arrRatings[r].rateItID==n){arrRatings[r].rated=true;arrRatings[r].width=e.hover.getStyle("width");break}}}if(typeof $(n)!="undefined"){var i=$(n).getElement(".wrapper");i.removeEvents();i.getElement(".rateItRating-selected").setStyle("display","none");i.getElement(".rateItRating-hover").setStyle("width",e.hover.getStyle("width"));i.getElement(".rateItRating-hover").setStyle("display","block")}}else{if(typeof arrRatings=="object"){for(var r=0;r<arrRatings.length;r++){if(arrRatings[r].rateItID==e.id){arrRatings[r].rated=true;arrRatings[r].width=e.hover.getStyle("width");break}}}}var s=this.getVotePercent(e.newFill);if(this.options.url!=null){(new Request({url:this.options.url,onComplete:e.updateText})).post({vote:s,id:e.ratableId,type:e.ratableType})}}.bind(this));e.updateText=function(t){error=t.split("ERROR:")[1];e.textEl.removeClass("loading");if(error){e.showError(error);return false}e.textEl.set("text",t);if(typeof $(".mbrateItRating")!="undefined"&&e.id.indexOf("mb")==0){var n=e.getAttribute("id");n=n.replace("mb","");if(typeof arrRatings=="object"){for(var r=0;r<arrRatings.length;r++){if(arrRatings[r].rateItID==n){arrRatings[r].description=t;break}}}if(typeof $(n)!="undefined"){$(n).getElement(".ratingText").set("text",t)}}else{if(typeof arrRatings=="object"){for(var r=0;r<arrRatings.length;r++){if(arrRatings[r].rateItID==e.id){arrRatings[r].description=t;break}}}}}}e.showError=function(t){e.textEl.addClass("ratingError");e.textEl.set("text",t);(function(){e.textEl.set("text",e.textEl.oldTxt);e.textEl.removeClass("ratingError")}).delay(2e3)}}else{e.getElement(".ratingText").inject(e,"before");e.remove()}},fillVote:function(e,t){t.newFill=this.getFillPercent(e);if(this.getVotePercent(t.newFill)>100){t.newFill=this.getFillPercent(100)}t.selected.setStyle("width",t.newFill)},getStarPercent:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);if(t!=null){var n=t[3].toFloat();var r=t[4].toFloat();var i=n/r*100;return i}else{return 0}},getFillPercent:function(e){return e/100*this.options.starwidth*this.options.max},getVotePercent:function(e){var t=this.options.starwidth*this.options.max;var n=(e/t*100).round(2);return n},getRatableId:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?t[1]:""},getRatableType:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?t[2]:""},getRatableMaxValue:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?t[4].toInt():0},setBackgroundPosition:function(e,t){e.setStyle("background-position","0% "+t+"px")},getBackgroundImagePath:function(e){return e.getStyle("background-image")},getBackgroundImage:function(e){var t=/url\s*\(["']?(.*)["']?\)/i;var n=document.createElement("img");var r=this.getBackgroundImagePath(e);r=r.match(t)[1];r=r.replace('"',"");n.src=r;return n}});window.addEvent("domready",function(t){RateItRating=new e({url:"SimpleAjax.php"})})}else if(window.jQuery){(function(){e={options:{step:.1,readonly:false,resetable:false},initialize:function(e){if(typeof e=="object"&&typeof e["url"]!="undefined")this.options.url=e["url"];var t=this;jQuery(".rateItRating").each(function(e,n){t.initMe(n)});return this},initMe:function(e){var n=this;if(!Browser.Engine.trident4){var r=jQuery(e);r.data("id",r.attr("id"));r.data("rateable",r.attr("rel")=="not-rateable"?false:true);r.data("wrapper",r.find(".wrapper"));r.data("textEl",r.find(".ratingText"));r.data("selected",r.find(".rateItRating-selected"));r.data("hover",r.find(".rateItRating-hover"));var i=n.getBackgroundImage(r.data("wrapper"));n.options.starwidth=i.width;n.options.starheight=i.height/3;if(n.options.starwidth===undefined||n.options.starwidth<16){n.options.starwidth=16}if(n.options.starheight===undefined||n.options.starheight<16){n.options.starheight=16}n.setBackgroundPosition(r.data("selected"),-1*n.options.starheight);n.setBackgroundPosition(r.data("hover"),-1*2*n.options.starheight);r.data("starPercent",n.getStarPercent(r.data("id")));r.data("ratableId",n.getRatableId(r.data("id")));r.data("ratableType",n.getRatableType(r.data("id")));n.options.max=n.getRatableMaxValue(r.data("id"));r.data("selected").css("height",n.options.starheight);r.data("hover").css("height",n.options.starheight);r.data("wrapper").css("width",n.options.starwidth*n.options.max);r.data("wrapper").css("height",n.options.starheight);n.fillVote(r.data("starPercent"),r);r.data("currentFill",n.getFillPercent(r.data("starPercent")));if(r.data("rateable")){r.data("wrapper").mouseenter(function(e){r.data("selected").hide(500,"easeInOutQuad");r.data("hover").show();r.data("wrapper").mousemove({el:r,self:n},n.mouseCrap)});r.data("wrapper").mouseleave(function(e){r.data("wrapper").unbind("mousemove");r.data("hover").hide();r.data("selected").show();r.data("selected").animate({width:r.data("currentFill")},500)});r.data("wrapper").click(function(e){r.data("currentFill",r.data("newFill"));r.data("wrapper").unbind();r.data("oldTxt",r.data("textEl").text());r.data("textEl").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");r.data("textEl").addClass("loading");if(typeof jQuery(".mbrateItRating")!="undefined"&&r.data("id").indexOf("mb")==0){var i=r.data("id");i=i.replace("mb","");if(typeof arrRatings=="object"){for(var s=0;s<arrRatings.length;s++){if(arrRatings[s].rateItID==i){arrRatings[s].rated=true;arrRatings[s].width=r.data("hover").css("width");break}}}if(typeof jQuery("#"+t(i))!="undefined"){var o=jQuery("#"+t(i)).find(".wrapper");o.unbind();o.find(".rateItRating-selected").css("display","none");o.find(".rateItRating-hover").css("width",r.data("hover").css("width"));o.find(".rateItRating-hover").css("display","block")}}else{if(typeof arrRatings=="object"){for(var s=0;s<arrRatings.length;s++){if(arrRatings[s].rateItID==r.data("id")){arrRatings[s].rated=true;arrRatings[s].width=r.data("hover").css("width");break}}}}var u=n.getVotePercent(r.data("newFill"));if(n.options.url!=null){jQuery.ajax({url:n.options.url,type:"post",data:{vote:u,id:r.data("ratableId"),type:r.data("ratableType")}}).done(function(e){r.data("updateText")(r,e)})}})}r.data("updateText",n.updateText)}else{alert("Ich bin ein IE6")}},fillVote:function(e,t){t.data("newFill",this.getFillPercent(e));if(this.getVotePercent(t.data("newFill"))>100){t.data("newFill",this.getFillPercent(100))}t.data("selected").css("width",t.data("newFill"))},mouseCrap:function(e){var t=e.data["el"];var n=e.data["self"];var r=e.originalEvent.layerX;if(!r){r=e.originalEvent.offsetX}var i=n.getVotePercent(r);var s=Math.ceil(i/100*n.options.max);var o=s*n.options.starwidth;if(parseInt(t.data("hover").css("width"))!=o){t.data("selected").css("display","none");t.data("hover").css("width",Math.min(o,n.options.starwidth*n.options.max));t.data("hover").css("display","block")}var u=s/n.options.max*100;n.fillVote(u,t)},getStarPercent:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);if(t!=null){var n=parseFloat(t[3]);var r=parseFloat(t[4]);var i=n/r*100;return i}else{return 0}},getFillPercent:function(e){return e/100*this.options.starwidth*this.options.max},getVotePercent:function(e){var t=this.options.starwidth*this.options.max;var n=(e/t*100).toFixed(2);return n},getRatableId:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?t[1]:""},getRatableType:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?t[2]:""},getRatableMaxValue:function(e){var t=e.match(/(\d*\|?\d*)-(page|article|ce|module|news|faq|galpic|news4ward)-(\d*\.?\d+)_(\d*\.?\d+)$/);return t!=null?parseInt(t[4]):0},setBackgroundPosition:function(e,t){e.css("background-position","0% "+t+"px")},getBackgroundImagePath:function(e){return e.css("background-image")},getBackgroundImage:function(e){var t=/url\s*\(["']?(.*)["']?\)/i;var n=document.createElement("img");var r=this.getBackgroundImagePath(e);r=r.match(t)[1];r=r.replace('"',"");n.src=r;return n},updateText:function(e,n){error=n.split("ERROR:")[1];e.data("textEl").removeClass("loading");if(error){this.RateItRating.showError(e,error);return false}e.data("textEl").text(n);if(typeof jQuery(".mbrateItRating")!="undefined"&&e.data("id").indexOf("mb")==0){var r=e.attr("id");r=r.replace("mb","");if(typeof arrRatings=="object"){for(var i=0;i<arrRatings.length;i++){if(arrRatings[i].rateItID==r){arrRatings[i].description=n;break}}}if(typeof jQuery("#"+t(r))!="undefined"){jQuery("#"+t(r)).find(".ratingText").text(n)}}else{if(typeof arrRatings=="object"){for(var i=0;i<arrRatings.length;i++){if(arrRatings[i].rateItID==e.data("id")){arrRatings[i].description=n;break}}}}},showError:function(e,t){e.data("textEl").addClass("ratingError");e.data("textEl").text(t);setTimeout(function(){e.data("textEl").text(e.data("oldTxt"));e.data("textEl").removeClass("ratingError")},2e3)}}})(jQuery);jQuery(document).ready(function(){jQuery.ajax({type:"GET",url:"system/modules/rateit/public/js/jquery-ui-effects.custom.min.js",dataType:"script",async:false,cache:true});jQuery.ajax({type:"GET",url:"system/modules/rateit/public/js/helper.js",dataType:"script",async:false,cache:true});RateItRating=Object.create(e).initialize({url:"SimpleAjax.php"})});var t=function(e){e=e.replace(new RegExp("\\$","g"),"\\$");e=e.replace(new RegExp("~","g"),"\\~");e=e.replace(new RegExp("\\[","g"),"\\[");e=e.replace(new RegExp("\\]","g"),"\\]");e=e.replace(new RegExp("\\|","g"),"\\|");e=e.replace(new RegExp("\\.","g"),"\\.");e=e.replace(new RegExp("#","g"),"\\#");return e}}}var RateItRating;onReadyRateIt(function(){doRateIt()})