1 |
- jQuery.fn.ratingStars=function(e){var r={selectors:{starsSelector:".rating-stars",starSelector:".rating-star",starActiveClass:"is--active",starHoverClass:"is--hover",starNoHoverClass:"is--no-hover",targetFormElementSelector:".rating-value"}},t=$.extend({},r,e),s={init:function(e){s.registerEvents(e),s.loadDefaultValue(e)},loadDefaultValue:function(e){var r=$(e).children(t.selectors.targetFormElementSelector).val(),s=0;$.each($(e).children(t.starsSelector).children(t.starSelector),function(e,a){s<=r-1&&$(a).addClass(t.selectors.starActiveClass),s++})},registerEvents:function(e){var r=this;$.each($(e).children(t.starsSelector).children(t.starSelector),function(t,s){$(s).on("mouseenter",$.proxy(r.onStarEnter,r,s,e)),$(s).on("mouseleave",$.proxy(r.onStarLeave,r,s,e)),$(s).on("click touchstart",$.proxy(r.onStarSelected,r,s,e))})},onStarEnter:function(e,r){var s=$(e).index(),a=0;$.each($(r).children(t.starsSelector).children(t.starSelector),function(e,r){a<=s?$(r).addClass(t.selectors.starHoverClass):$(r).addClass(t.selectors.starNoHoverClass),a++}),$(r).trigger("ratingOnEnter",{ratingValue:s+1})},onStarLeave:function(e,r){var s=$(e).index();$(r).children(t.starsSelector).children(t.starSelector).removeClass(t.selectors.starHoverClass),$(r).children(t.starsSelector).children(t.starSelector).removeClass(t.selectors.starNoHoverClass),$(r).trigger("ratingOnLeave",{ratingValue:s+1})},onStarSelected:function(e,r){var s=$(e).index();$(r).children(t.starsSelector).children(t.starSelector).removeClass(t.selectors.starActiveClass);var a=0;$.each($(r).children(t.starsSelector).children(t.starSelector),function(e,r){a<=s&&$(r).addClass(t.selectors.starActiveClass),a++}),$(r).children(t.selectors.targetFormElementSelector).val(s+1),$(r).trigger("ratingChanged",{ratingValue:s+1})}};return this.each(function(){s.init($(this))})};
|