BxDolScore.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 BxDolScore(oOptions)
  9. {
  10. this._sObjName = undefined == oOptions.sObjName ? 'oScore' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
  11. this._sSystem = oOptions.sSystem; // current score system
  12. this._iAuthorId = oOptions.iAuthorId; // score's author ID.
  13. this._iObjId = oOptions.iObjId; // object id the scores are collected for
  14. this._sActionsUri = 'score.php';
  15. this._sActionsUrl = oOptions.sRootUrl + this._sActionsUri; // actions url address
  16. this._sSocket = oOptions.sSocket === undefined ? this._sSystem : oOptions.sSocket;
  17. this._sAnimationEffect = 'fade';
  18. this._iAnimationSpeed = 'slow';
  19. this._sSP = undefined == oOptions.sStylePrefix ? 'bx-score' : oOptions.sStylePrefix;
  20. this._aHtmlIds = oOptions.aHtmlIds;
  21. this._aRequestParams = oOptions.aRequestParams;
  22. this._iSaveWidth = -1;
  23. var $this = this;
  24. $(document).ready(function() {
  25. $this.init();
  26. });
  27. }
  28. BxDolScore.prototype.init = function()
  29. {
  30. var $this = this;
  31. $(document).ready(function() {
  32. if(window.oBxDolSockets !== undefined && $this._sSocket)
  33. oBxDolSockets.subscribe($this._sSocket, $this._iObjId, 'voted', function(oData) {
  34. $this.onVoteAs(oData);
  35. });
  36. });
  37. return;
  38. };
  39. BxDolScore.prototype.toggleByPopup = function(oLink, sType) {
  40. var oParams = this._getDefaultParams();
  41. oParams['action'] = 'GetVotedBy';
  42. if(sType)
  43. oParams['type'] = sType;
  44. $(oLink).dolPopupAjax({
  45. id: this._aHtmlIds['by_popup'],
  46. url: bx_append_url_params(this._sActionsUri, oParams),
  47. removeOnClose: true
  48. });
  49. };
  50. BxDolScore.prototype.voteUp = function (oLink, onComplete)
  51. {
  52. this._vote(oLink, 'up', onComplete);
  53. };
  54. BxDolScore.prototype.voteDown = function (oLink, onComplete)
  55. {
  56. this._vote(oLink, 'down', onComplete);
  57. };
  58. BxDolScore.prototype.onVoteAs = function(sData)
  59. {
  60. oData = JSON.parse(sData);
  61. var oLink = $('#' + this._aHtmlIds['main']).find('.' + this._sSP + '-do-vote.' + this._sSP + '-dv-' + oData.type);
  62. if(oLink && oLink.length)
  63. this._onVote(oLink, oData);
  64. };
  65. BxDolScore.prototype._vote = function (oLink, sType, onComplete)
  66. {
  67. var $this = this;
  68. var oParams = this._getDefaultParams();
  69. oParams['action'] = 'vote_' + sType;
  70. $.post(
  71. this._sActionsUrl,
  72. oParams,
  73. function(oData) {
  74. if(oData && oData.message != undefined)
  75. bx_alert(oData.message, function() {
  76. $this._onVote(oLink, oData, onComplete);
  77. });
  78. else
  79. $this._onVote(oLink, oData, onComplete);
  80. },
  81. 'json'
  82. );
  83. };
  84. BxDolScore.prototype._onVote = function(oLink, oData, onComplete)
  85. {
  86. if(oData && oData.code != 0)
  87. return;
  88. if(oData && oData.label_icon)
  89. $(oLink).find('.sys-action-do-icon .sys-icon').attr('class', 'sys-icon ' + oData.label_icon);
  90. if(oData && oData.label_title) {
  91. $(oLink).attr('title', oData.label_title);
  92. $(oLink).find('.sys-action-do-text').html(oData.label_title);
  93. }
  94. if(oData && oData.disabled)
  95. this._getActions(oLink).removeAttr('onclick').addClass($(oLink).hasClass('bx-btn') ? 'bx-btn-disabled' : 'bx-score-disabled');
  96. var oCounter = this._getCounter(oLink);
  97. if(oCounter && oCounter.length > 0) {
  98. if(oData && oData.counter)
  99. oCounter.replaceWith(oData.counter);
  100. else
  101. oCounter.html(oData.scoref).parents('.' + $this._sSP + '-counter-holder:first:hidden').bx_anim('show');
  102. }
  103. if(typeof onComplete == 'function')
  104. onComplete(oLink, oData);
  105. };
  106. BxDolScore.prototype._getActions = function(oElement) {
  107. var oParent = $(oElement);
  108. if(!oParent.hasClass(this._sSP))
  109. oParent = $(oElement).parents('.' + this._sSP + ':first');
  110. return oParent.find('.' + this._sSP + '-do-vote');
  111. };
  112. BxDolScore.prototype._getCounter = function(oElement) {
  113. var oParent = $(oElement);
  114. if(!oParent.hasClass(this._sSP))
  115. oParent = $(oElement).parents('.' + this._sSP + ':first');
  116. var oCounter = oParent.find('.' + this._sSP + '-counter');
  117. if(oCounter && oCounter.length > 0)
  118. return oCounter;
  119. return $('#' + this._aHtmlIds['counter'] + '.' + this._sSP + '-counter');
  120. };
  121. BxDolScore.prototype._loadingInButton = function(e, bShow) {
  122. if($(e).length)
  123. bx_loading_btn($(e), bShow);
  124. else
  125. bx_loading($('body'), bShow);
  126. };
  127. BxDolScore.prototype._getDefaultParams = function() {
  128. var oDate = new Date();
  129. return {
  130. sys: this._sSystem,
  131. id: this._iObjId,
  132. params: $.param(this._aRequestParams),
  133. _t: oDate.getTime()
  134. };
  135. };
  136. /** @} */