rating.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('marketing.ratingControl', {
  11. options: {
  12. colorFilled: '#333',
  13. colorUnfilled: '#CCCCCC',
  14. colorHover: '#f30'
  15. },
  16. /** @inheritdoc */
  17. _create: function () {
  18. this._labels = this.element.find('label');
  19. this._bind();
  20. },
  21. /**
  22. * @private
  23. */
  24. _bind: function () {
  25. this._labels.on({
  26. click: $.proxy(function (e) {
  27. $(e.currentTarget).prev().prop('checked', true);
  28. this._updateRating();
  29. }, this),
  30. hover: $.proxy(function (e) {
  31. this._updateHover($(e.currentTarget), this.options.colorHover);
  32. }, this),
  33. mouseleave: $.proxy(function (e) {
  34. this._updateHover($(e.currentTarget), this.options.colorUnfilled);
  35. }, this)
  36. });
  37. this._updateRating();
  38. },
  39. /**
  40. * @param {jQuery} elem
  41. * @param {String} color
  42. * @private
  43. */
  44. _updateHover: function (elem, color) {
  45. elem.nextAll('label').addBack().filter(function () {
  46. return !$(this).data('checked');
  47. }).css('color', color);
  48. },
  49. /**
  50. * @private
  51. */
  52. _updateRating: function () {
  53. var checkedInputs = this.element.find('input[type="radio"]:checked');
  54. checkedInputs.nextAll('label').addBack().css('color', this.options.colorFilled).data('checked', true);
  55. checkedInputs.prevAll('label').css('color', this.options.colorUnfilled).data('checked', false);
  56. }
  57. });
  58. });