jquery.rating-stars.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. jQuery.fn.ratingStars = function( options ) {
  2. var defaults = {
  3. selectors: {
  4. starsSelector: '.rating-stars',
  5. starSelector: '.rating-star',
  6. starActiveClass: 'is--active',
  7. starHoverClass: 'is--hover',
  8. starNoHoverClass: 'is--no-hover',
  9. targetFormElementSelector: '.rating-value'
  10. }
  11. };
  12. var settings = $.extend( {}, defaults, options );
  13. var methods = {
  14. init: function(element) {
  15. var me = this;
  16. methods.registerEvents(element);
  17. methods.loadDefaultValue(element);
  18. },
  19. loadDefaultValue: function (element) {
  20. var me = this;
  21. var defaultValue = $(element).children(settings.selectors.targetFormElementSelector).val();
  22. var i = 0;
  23. $.each($(element).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
  24. if(i <= (defaultValue - 1)) {
  25. $(element).addClass(settings.selectors.starActiveClass);
  26. }
  27. i++;
  28. });
  29. },
  30. registerEvents: function (element) {
  31. var me = this;
  32. $.each($(element).children(settings.starsSelector).children(settings.starSelector), function(index, starElement) {
  33. $(starElement).on("mouseenter", $.proxy(me.onStarEnter, me, starElement, element));
  34. $(starElement).on("mouseleave", $.proxy(me.onStarLeave, me, starElement, element));
  35. $(starElement).on("click touchstart", $.proxy(me.onStarSelected, me, starElement, element));
  36. });
  37. },
  38. onStarEnter: function(starElement, container) {
  39. var me = this;
  40. var elementIndex = $(starElement).index();
  41. // add the hover classes
  42. var i = 0;
  43. $.each($(container).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
  44. if(i <= elementIndex) {
  45. // add a class to represent the chosen stars.
  46. $(element).addClass(settings.selectors.starHoverClass);
  47. } else {
  48. // add a class to represent the remaining stars.
  49. $(element).addClass(settings.selectors.starNoHoverClass);
  50. }
  51. i++;
  52. });
  53. $(container).trigger("ratingOnEnter", {
  54. ratingValue: (elementIndex + 1)
  55. });
  56. },
  57. onStarLeave: function(starElement, container) {
  58. var me = this;
  59. var elementIndex = $(starElement).index();
  60. // remove all hover classes
  61. $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starHoverClass);
  62. $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starNoHoverClass);
  63. $(container).trigger("ratingOnLeave", {
  64. ratingValue: (elementIndex + 1)
  65. });
  66. },
  67. onStarSelected: function(starElement, container) {
  68. var me = this;
  69. var elementIndex = $(starElement).index();
  70. // remove the currently selected class
  71. $(container).children(settings.starsSelector).children(settings.starSelector).removeClass(settings.selectors.starActiveClass);
  72. // set the selected class for the stars
  73. var i = 0;
  74. $.each($(container).children(settings.starsSelector).children(settings.starSelector), function(index, element) {
  75. if(i <= elementIndex) {
  76. $(element).addClass(settings.selectors.starActiveClass);
  77. }
  78. i++;
  79. });
  80. // set the rating value to the form
  81. $(container).children(settings.selectors.targetFormElementSelector).val(elementIndex + 1);
  82. $(container).trigger("ratingChanged", {
  83. ratingValue: (elementIndex + 1)
  84. });
  85. }
  86. };
  87. return this.each(function() {
  88. methods.init($(this));
  89. });
  90. };