BxDolVote.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 BxDolVote(oOptions)
  9. {
  10. if(typeof oOptions === 'undefined')
  11. return;
  12. this._sObjName = oOptions.sObjName === undefined ? 'oVote' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
  13. this._sSystem = oOptions.sSystem; // current comment system
  14. this._iAuthorId = oOptions.iAuthorId; // this comment's author ID.
  15. this._iObjId = oOptions.iObjId; // this object id comments
  16. this._sActionsUri = 'vote.php';
  17. this._sActionsUrl = oOptions.sRootUrl + this._sActionsUri; // actions url address
  18. this._sSocket = oOptions.sSocket === undefined ? this._sSystem : oOptions.sSocket;
  19. this._sAnimationEffect = 'fade';
  20. this._iAnimationSpeed = 'slow';
  21. this._sSP = oOptions.sStylePrefix === undefined ? 'bx-vote' : oOptions.sStylePrefix;
  22. this._aHtmlIds = oOptions.aHtmlIds;
  23. this._aRequestParams = oOptions.aRequestParams;
  24. var $this = this;
  25. $(document).ready(function() {
  26. $this.init();
  27. });
  28. }
  29. BxDolVote.prototype.init = function()
  30. {
  31. var $this = this;
  32. $(document).ready(function() {
  33. if(window.oBxDolSockets !== undefined && $this._sSocket)
  34. oBxDolSockets.subscribe($this._sSocket, $this._iObjId, 'voted', function(oData) {
  35. $this.onVoteAs(oData);
  36. });
  37. });
  38. return;
  39. };
  40. BxDolVote.prototype.toggleByPopup = function(oLink)
  41. {
  42. var oData = this._getDefaultParams();
  43. oData['action'] = 'GetVotedBy';
  44. $(oLink).dolPopupAjax({
  45. id: this._aHtmlIds['by_popup'],
  46. url: bx_append_url_params(this._sActionsUri, oData)
  47. });
  48. };
  49. BxDolVote.prototype.vote = function(oLink, iValue, onComplete)
  50. {
  51. var $this = this;
  52. var oParams = this._getDefaultParams();
  53. oParams['action'] = 'Vote';
  54. oParams['value'] = iValue;
  55. $.post(
  56. this._sActionsUrl,
  57. oParams,
  58. function(oData) {
  59. $this._cueOnVote(oData);
  60. if(oData && oData.message != undefined)
  61. bx_alert(oData.message, function() {
  62. $this.onVote(oLink, oData, onComplete);
  63. });
  64. else
  65. $this.onVote(oLink, oData, onComplete);
  66. },
  67. 'json'
  68. );
  69. };
  70. /**
  71. * The interface sound of the visitor's own vote: a like is a toggle, on when given and off when taken back.
  72. */
  73. BxDolVote.prototype._cueOnVote = function(oData)
  74. {
  75. if(typeof(bx_sound) != 'function' || !oData)
  76. return;
  77. if(oData.code != undefined && parseInt(oData.code) != 0)
  78. bx_sound('error');
  79. else
  80. bx_sound('toggle', {direction: oData.voted ? 'forward' : 'back', group: 'reactions'});
  81. };
  82. BxDolVote.prototype.onVoteAs = function(oData)
  83. {
  84. var oLink = $('#' + this._aHtmlIds['main']).find('.' + this._sSP + '-do-vote');
  85. if(oLink && oLink.length)
  86. this.onVote(oLink, JSON.parse(oData));
  87. };
  88. BxDolVote.prototype.onVote = function(oLink, oData, onComplete)
  89. {
  90. if(oData && oData.code != 0)
  91. return;
  92. var oCounter = this._getCounter(oLink);
  93. if(oCounter && oCounter.length > 0) {
  94. oCounter.html(oData.countf);
  95. oCounter.parents('.' + this._sSP + '-counter-holder:first').bx_anim(oData.count > 0 ? 'show' : 'hide');
  96. }
  97. if(typeof onComplete == 'function')
  98. onComplete(oLink, oData);
  99. };
  100. BxDolVote.prototype._getCounter = function(oElement)
  101. {
  102. if($(oElement).hasClass(this._sSP))
  103. return $(oElement).find('.' + this._sSP + '-counter');
  104. else
  105. return $(oElement).parents('.' + this._sSP + ':first').find('.' + this._sSP + '-counter');
  106. };
  107. BxDolVote.prototype._loadingInButton = function(e, bShow)
  108. {
  109. if($(e).length)
  110. bx_loading_btn($(e), bShow);
  111. else
  112. bx_loading($('body'), bShow);
  113. };
  114. BxDolVote.prototype._getDefaultParams = function()
  115. {
  116. var oDate = new Date();
  117. return {
  118. sys: this._sSystem,
  119. id: this._iObjId,
  120. params: $.param(this._aRequestParams),
  121. _t: oDate.getTime()
  122. };
  123. };
  124. /** @} */