BxDolVoteStars.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolVoteStars(oOptions)
  9. {
  10. BxDolVote.call(this, oOptions);
  11. this._iSaveWidth = -1;
  12. }
  13. BxDolVoteStars.prototype = Object.create(BxDolVote.prototype);
  14. BxDolVoteStars.prototype.constructor = BxDolVoteStars;
  15. BxDolVoteStars.prototype.init = function ()
  16. {
  17. BxDolVote.prototype.init.call(this);
  18. var $this = this;
  19. $('.' + this._sSP + '.' + this._sSP + '-stars').each(function() {
  20. var oDoVote = $(this);
  21. var fRate = oDoVote.attr('bx_vote_data_rate');
  22. var iStarWidth = $this._getStarWidthDo(oDoVote);
  23. $this._getSliderDo(oDoVote).width(Math.round(fRate * iStarWidth));
  24. });
  25. $('.' + this._sSP + '-legend.' + this._sSP + '-legend-stars').each(function() {
  26. var oLegend = $(this);
  27. var iStarWidth = $this._getStarWidthLegend(oLegend);
  28. oLegend.find('.' + $this._sSP + '-legend-item').each(function() {
  29. var oItem = $(this);
  30. oItem.find('.' + $this._sSP + '-slider').width(parseInt(oItem.attr('bx_vote_item_value')) * iStarWidth);
  31. });
  32. });
  33. };
  34. /**
  35. * Rating is choosing a value: a select, and a toggle going off when the rating is taken back.
  36. */
  37. BxDolVoteStars.prototype._cueOnVote = function(oData)
  38. {
  39. if(typeof(bx_sound) != 'function' || !oData)
  40. return;
  41. if(oData.code != undefined && parseInt(oData.code) != 0)
  42. bx_sound('error');
  43. else if(oData.voted)
  44. bx_sound('select', {group: 'reactions'});
  45. else
  46. bx_sound('toggle', {direction: 'back', group: 'reactions'});
  47. };
  48. BxDolVoteStars.prototype.onVote = function (oLink, oData, onComplete)
  49. {
  50. if(oData && oData.code != 0)
  51. return;
  52. this._iSaveWidth = Math.round(oData.rate * this._getStarWidthDo(oLink));
  53. BxDolVote.prototype.onVote.call(this, oLink, oData, onComplete);
  54. };
  55. BxDolVoteStars.prototype.over = function (oLink)
  56. {
  57. var oSlider = this._getSliderDo(oLink);
  58. var iIndex = this._getButtons(oLink).index(oLink);
  59. this._iSaveWidth = parseInt(oSlider.width());
  60. oSlider.width((iIndex + 1) * this._getStarWidthDo(oLink));
  61. };
  62. BxDolVoteStars.prototype.out = function (oLink)
  63. {
  64. var oSlider = this._getSliderDo(oLink);
  65. oSlider.width(this._iSaveWidth);
  66. };
  67. BxDolVoteStars.prototype._getButtons = function(oElement)
  68. {
  69. if($(oElement).hasClass(this._sSP))
  70. return $(oElement).find('.' + this._sSP + '-button');
  71. else
  72. return $(oElement).parents('.' + this._sSP + ':first').find('.' + this._sSP + '-button');
  73. };
  74. BxDolVoteStars.prototype._getSliderDo = function(oElement)
  75. {
  76. return this._getSlider(oElement, '.' + this._sSP + '-do');
  77. };
  78. BxDolVoteStars.prototype._getSliderLegend = function(oElement)
  79. {
  80. return this._getSlider(oElement, '.' + this._sSP + '-legend');
  81. };
  82. BxDolVoteStars.prototype._getSlider = function(oElement, sParent) {
  83. var sSlider = (sParent.length > 0 ? sParent + ' ' : '') + '.' + this._sSP + '-slider';
  84. if($(oElement).hasClass(this._sSP))
  85. return $(oElement).find(sSlider);
  86. else
  87. return $(oElement).parents('.' + this._sSP + ':first').find(sSlider);
  88. };
  89. BxDolVoteStars.prototype._getStarWidthDo = function(oElement)
  90. {
  91. return this._getSliderDo(oElement).find('.sys-icon').width();
  92. };
  93. BxDolVoteStars.prototype._getStarWidthLegend = function(oElement)
  94. {
  95. return this._getSliderLegend(oElement).find('.sys-icon').width();
  96. };
  97. /** @} */