BxDolVoteReactions.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 BxDolVoteReactions(oOptions)
  9. {
  10. BxDolVote.call(this, oOptions);
  11. this._bQuickMode = oOptions.bQuickMode === undefined ? 0 : oOptions.bQuickMode; // enable 'quick' mode - vote with default reaction when clicked.
  12. this._bSingleMode = oOptions.bSingleMode === undefined ? 0 : oOptions.bSingleMode; // enable 'single' mode - vote with the only one available reaction when clicked.
  13. this._iTimeoutShowId = 0;
  14. this._iTimeoutShowDelay = 750;
  15. this._iTimeoutHideId = 0;
  16. this._iTimeoutHideDelay = 1000;
  17. this._fOnVoteIn = null;
  18. this._fOnVoteOut = null;
  19. this._fOnDoPopupIn = null;
  20. this._fOnDoPopupOut = null;
  21. this._sClassDo = 'bx-vote-do-vote';
  22. this._sClassDoVoted = 'bx-vote-voted';
  23. }
  24. BxDolVoteReactions.prototype = Object.create(BxDolVote.prototype);
  25. BxDolVoteReactions.prototype.constructor = BxDolVoteReactions;
  26. BxDolVoteReactions.prototype.init = function()
  27. {
  28. BxDolVote.prototype.init.call(this);
  29. var $this = this;
  30. if(this._bQuickMode && !this._bSingleMode) {
  31. if(bx_is_mouse())
  32. $('#' + this._aHtmlIds['main'] + ' .' + this._sClassDo).hover(function() {
  33. $this.onVoteIn(this);
  34. }, function() {
  35. $this.onVoteOut(this);
  36. });
  37. else
  38. $('#' + this._aHtmlIds['main'] + ' .' + this._sClassDo).onLongTouch(function(oElement) {
  39. $this.onTouch(oElement);
  40. });
  41. }
  42. };
  43. BxDolVoteReactions.prototype.vote = function(oLink, iValue, sReaction, onComplete)
  44. {
  45. var $this = this;
  46. var oParams = this._getDefaultParams();
  47. oParams['action'] = 'Vote';
  48. oParams['value'] = iValue;
  49. oParams['reaction'] = sReaction;
  50. if(this._iTimeoutShowId)
  51. clearTimeout(this._iTimeoutShowId);
  52. $('#' + this._aHtmlIds['do_popup']).dolPopupHide({});
  53. $.post(
  54. this._sActionsUrl,
  55. oParams,
  56. function(oData) {
  57. $this._cueOnVote(oData);
  58. if(oData && oData.message != undefined)
  59. bx_alert(oData.message, function() {
  60. $this.onVote(oLink, oData, onComplete);
  61. });
  62. else
  63. $this.onVote(oLink, oData, onComplete);
  64. },
  65. 'json'
  66. );
  67. };
  68. BxDolVoteReactions.prototype.onVote = function (oLink, oData, onComplete)
  69. {
  70. var $this = this;
  71. if(oData && oData.code != 0)
  72. return;
  73. oLink = $('.' + this._aHtmlIds['main'] + ' .' + this._sClassDo);
  74. //--- Update Do button.
  75. oLink.each(function() {
  76. if(oData && oData.label_use)
  77. switch(oData.label_use) {
  78. case 'icon':
  79. if($(this).find('.sys-action-do-icon .sys-icon').length && !$(oData.label_icon).hasClass('sys-icon'))
  80. $(this).find('.sys-action-do-icon .sys-icon').attr('class', 'sys-icon ' + oData.label_icon).html('');
  81. else
  82. $(this).find('.sys-action-do-icon').html(oData.label_icon);
  83. break;
  84. case 'emoji':
  85. $(this).find('.sys-action-do-icon .sys-icon').attr('class', 'sys-icon sys-icon-emoji').html(oData.label_emoji);
  86. break;
  87. case 'image':
  88. $(this).find('.sys-action-do-icon').html(oData.label_image);
  89. break;
  90. }
  91. if(oData && oData.label_title) {
  92. $(this).attr('title', oData.label_title);
  93. $(this).find('.sys-action-do-text').html(oData.label_title);
  94. }
  95. if(oData && oData.label_click)
  96. $(this).attr('onclick', 'javascript:' + oData.label_click)
  97. if(oData && oData.disabled)
  98. $(this).removeAttr('onclick').addClass($(this).hasClass('bx-btn') ? 'bx-btn-disabled' : 'bx-vote-disabled');
  99. if(oData && oData.voted != undefined)
  100. $(this).toggleClass($this._sClassDoVoted, oData.voted);
  101. });
  102. //--- Update Counter.
  103. var oCounter = this._getCounter(oLink);
  104. if(oCounter && oCounter.length > 0) {
  105. oCounter.filter('.' + oData.reaction).each(function() {
  106. $(this).html(oData.countf).toggleClass('bx-vc-hidden', !oData.count);
  107. });
  108. //--- Update Total.
  109. if(oData.total)
  110. oCounter.filter('.total-count').each(function() {
  111. $(this).html(oData.total.countf).toggleClass('bx-vc-hidden', !oData.total.count);
  112. });
  113. //--- Show counter.
  114. oCounter.filter('.' + oData.reaction).each(function() {
  115. $(this).parents('.' + $this._sSP + '-counter-wrapper:first').toggleClass('bx-vc-hidden', !oData.count && !oData.total.count);
  116. });
  117. }
  118. if(typeof onComplete == 'function')
  119. onComplete(oLink, oData);
  120. };
  121. BxDolVoteReactions.prototype.onVoteIn = function(oLink)
  122. {
  123. var $this = this;
  124. if($(oLink).hasClass(this._sClassDoVoted))
  125. return;
  126. if(this._iTimeoutHideId)
  127. clearTimeout(this._iTimeoutHideId);
  128. var oPopup = this.getDoPopup();
  129. if(oPopup !== false)
  130. return;
  131. this._iTimeoutShowId = setTimeout(function() {
  132. $this.toggleDoPopup(oLink, $(oLink).attr('bx-vote-value'));
  133. }, this._iTimeoutShowDelay);
  134. };
  135. BxDolVoteReactions.prototype.onVoteOut = function(oLink)
  136. {
  137. if($(oLink).hasClass(this._sClassDoVoted))
  138. return;
  139. if(this._iTimeoutShowId)
  140. clearTimeout(this._iTimeoutShowId);
  141. this.hideDoPopup();
  142. };
  143. BxDolVoteReactions.prototype.onTouch = function(oLink)
  144. {
  145. if($(oLink).hasClass(this._sClassDoVoted))
  146. return;
  147. var oPopup = this.getDoPopup();
  148. if(oPopup !== false)
  149. return;
  150. this.toggleDoPopup(oLink, $(oLink).attr('bx-vote-value'), {
  151. closeOnOuterClick: false
  152. });
  153. };
  154. BxDolVoteReactions.prototype.getDoPopup = function()
  155. {
  156. var oPopup = $('#' + this._aHtmlIds['do_popup'] + ':visible');
  157. return oPopup.length > 0 && oPopup.hasClass('bx-popup-applied') ? oPopup : false;
  158. };
  159. BxDolVoteReactions.prototype.toggleDoPopup = function(oLink, iValue, oOptions)
  160. {
  161. var $this = this;
  162. var oParams = this._getDefaultParams();
  163. oParams['action'] = 'GetDoVotePopup';
  164. if(this._iTimeoutShowId)
  165. clearTimeout(this._iTimeoutShowId);
  166. oOptions = oOptions || {};
  167. oOptions = $.extend({}, {
  168. id: {value: this._aHtmlIds['do_popup'], force: true},
  169. url: bx_append_url_params(this._sActionsUri, oParams),
  170. value: iValue,
  171. onShow: function(oPopup) {
  172. $this.onDoPopupShow(oPopup);
  173. },
  174. onHide: function(oPopup) {
  175. $this.onDoPopupHide(oPopup);
  176. },
  177. cssClass: 'bx-popup-vote-reactions ' + $this._sSystem.replaceAll('_', '-')
  178. }, oOptions);
  179. $(oLink).dolPopupAjax(oOptions);
  180. };
  181. BxDolVoteReactions.prototype.hideDoPopup = function()
  182. {
  183. var oPopup = this.getDoPopup();
  184. if(!oPopup)
  185. return;
  186. this._iTimeoutHideId = setTimeout(function() {
  187. oPopup.dolPopupHide();
  188. }, this._iTimeoutHideDelay);
  189. };
  190. BxDolVoteReactions.prototype.onDoPopupShow = function(oPopup)
  191. {
  192. var $this = this;
  193. this._fOnDoPopupIn = function() {
  194. $this.onDoPopupIn(this);
  195. };
  196. this._fOnDoPopupOut = function() {
  197. $this.onDoPopupOut(this);
  198. };
  199. $(oPopup).hover(this._fOnDoPopupIn, this._fOnDoPopupOut);
  200. };
  201. BxDolVoteReactions.prototype.onDoPopupHide = function(oPopup)
  202. {
  203. $(oPopup).unbind('mouseenter', this._fOnDoPopupIn).unbind('mouseleave', this._fOnDoPopupOut);
  204. };
  205. BxDolVoteReactions.prototype.onDoPopupIn = function(oPopup)
  206. {
  207. if(this._iTimeoutHideId)
  208. clearTimeout(this._iTimeoutHideId);
  209. };
  210. BxDolVoteReactions.prototype.onDoPopupOut = function(oPopup)
  211. {
  212. this.hideDoPopup();
  213. };
  214. BxDolVoteReactions.prototype.toggleByPopup = function(oLink, sReaction)
  215. {
  216. var oParams = this._getDefaultParams();
  217. oParams['action'] = 'GetVotedBy';
  218. if(sReaction)
  219. oParams['reaction'] = sReaction;
  220. $(oLink).dolPopupAjax({
  221. id: {value: this._aHtmlIds['by_popup'], force: true},
  222. url: bx_append_url_params(this._sActionsUri, oParams),
  223. removeOnClose: true
  224. });
  225. };
  226. BxDolVoteReactions.prototype.changeVotedBy = function(oLink, sReaction)
  227. {
  228. var oContent = $('#' + this._aHtmlIds['by_popup'] + ' .' + this._sSP + '-bls-content');
  229. if(oContent.length > 0)
  230. oContent.children(':visible').hide().siblings('.' + this._sSP + '-bl-' + sReaction).show();
  231. };
  232. BxDolVoteReactions.prototype._getCounter = function(oElement)
  233. {
  234. var oCounter = BxDolVote.prototype._getCounter.call(this, oElement);
  235. if(oCounter && oCounter.length > 0)
  236. return oCounter;
  237. return $('.' + this._aHtmlIds['counter']).find('.' + this._sSP + '-counter');
  238. };
  239. (function($) {
  240. $.fn.onLongTouch = function(fCallback) {
  241. return this.each(function() {
  242. var oSource = this;
  243. var iTimeoutId = null;
  244. this.addEventListener('touchstart', function(e) {
  245. iTimeoutId = setTimeout(function() {
  246. iTimeoutId = null;
  247. e.stopPropagation();
  248. fCallback(oSource);
  249. }, 500);
  250. });
  251. this.addEventListener('contextmenu', function(e) {
  252. e.preventDefault();
  253. });
  254. this.addEventListener('touchend', function() {
  255. if(iTimeoutId)
  256. clearTimeout(iTimeoutId);
  257. });
  258. this.addEventListener('touchmove', function() {
  259. if(iTimeoutId)
  260. clearTimeout(iTimeoutId);
  261. });
  262. });
  263. };
  264. })(jQuery);
  265. /** @} */