123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- jQuery.fn.ratingStars = function( options ) {
-
- var defaults = {
- selectors: {
- starsSelector: '.rating-stars',
- starSelector: '.rating-star',
- starActiveClass: 'is--active',
- starHoverClass: 'is--hover',
- starNoHoverClass: 'is--no-hover',
- targetFormElementSelector: '.rating-value'
- }
- };
-
- var settings = $.extend( {}, defaults, options );
- var methods = {
- init: function(element) {
- var me = this;
-
- methods.registerEvents(element);
- methods.loadDefaultValue(element);
- },
- loadDefaultValue: function (element) {
- var me = this;
- var defaultValue = $(element).children(settings.selectors.targetFormElementSelector).val();
- var i = 0;
- $.each($(element).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
- if(i <= (defaultValue - 1)) {
- $(element).addClass(settings.selectors.starActiveClass);
- }
- i++;
- });
- },
- registerEvents: function (element) {
- var me = this;
-
- $.each($(element).children(settings.starsSelector).children(settings.starSelector), function(index, starElement) {
- $(starElement).on("mouseenter", $.proxy(me.onStarEnter, me, starElement, element));
- $(starElement).on("mouseleave", $.proxy(me.onStarLeave, me, starElement, element));
- $(starElement).on("click touchstart", $.proxy(me.onStarSelected, me, starElement, element));
- });
- },
- onStarEnter: function(starElement, container) {
- var me = this;
- var elementIndex = $(starElement).index();
-
- // add the hover classes
- var i = 0;
- $.each($(container).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
- if(i <= elementIndex) {
- // add a class to represent the chosen stars.
- $(element).addClass(settings.selectors.starHoverClass);
- } else {
- // add a class to represent the remaining stars.
- $(element).addClass(settings.selectors.starNoHoverClass);
- }
- i++;
- });
- $(container).trigger("ratingOnEnter", {
- ratingValue: (elementIndex + 1)
- });
- },
-
- onStarLeave: function(starElement, container) {
- var me = this;
- var elementIndex = $(starElement).index();
-
- // remove all hover classes
- $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starHoverClass);
- $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starNoHoverClass);
- $(container).trigger("ratingOnLeave", {
- ratingValue: (elementIndex + 1)
- });
- },
-
- onStarSelected: function(starElement, container) {
- var me = this;
- var elementIndex = $(starElement).index();
-
- // remove the currently selected class
- $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starActiveClass);
-
- // set the selected class for the stars
- var i = 0;
- $.each($(container).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
- if(i <= elementIndex) {
- $(element).addClass(settings.selectors.starActiveClass);
- }
- i++;
- });
-
- // set the rating value to the form
- $(container).children(settings.selectors.targetFormElementSelector).val(elementIndex + 1);
- $(container).trigger("ratingChanged", {
- ratingValue: (elementIndex + 1)
- });
- }
- };
-
- return this.each(function() {
- methods.init($(this));
- });
- };
|