BxDolFavorite.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 BxDolFavorite(options)
  9. {
  10. this._sObjName = undefined == options.sObjName ? 'oFavorite' : options.sObjName; // javascript object name, to run current object instance from onTimer
  11. this._sSystem = options.sSystem; // current comment system
  12. this._iAuthorId = options.iAuthorId; // this comment's author ID.
  13. this._iObjId = options.iObjId; // this object id comments
  14. this._sActionsUri = 'favorite.php';
  15. this._sActionsUrl = options.sRootUrl + this._sActionsUri; // actions url address
  16. this._sAnimationEffect = 'fade';
  17. this._iAnimationSpeed = 'slow';
  18. this._sSP = undefined == options.sStylePrefix ? 'bx-favorite' : options.sStylePrefix;
  19. this._aHtmlIds = options.aHtmlIds;
  20. this._oParent = null;
  21. }
  22. BxDolFavorite.prototype.toggleByPopup = function(oLink) {
  23. var $this = this;
  24. var oData = this._getDefaultParams();
  25. oData['action'] = 'GetFavoritedBy';
  26. $(oLink).dolPopupAjax({
  27. id: this._aHtmlIds['by_popup'],
  28. url: bx_append_url_params(this._sActionsUri, oData)
  29. });
  30. };
  31. BxDolFavorite.prototype.favorite = function(oLink) {
  32. var $this = this;
  33. var oData = this._getDefaultParams();
  34. oData['action'] = 'Favorite';
  35. this._oParent = $(oLink);
  36. $.get(
  37. this._sActionsUrl,
  38. oData,
  39. function (oData) {
  40. $this.processJson(oData, this._oParent);
  41. },
  42. 'json'
  43. );
  44. };
  45. BxDolFavorite.prototype.showNewList = function (obj) {
  46. $(obj).parents('form').find("#bx-form-element-title").show();
  47. $(obj).parents('form').find("#bx-form-element-allow_view_favorite_list_to").show();
  48. $(obj).parents('form').find("#bx-form-element-new_list").hide();
  49. }
  50. BxDolFavorite.prototype.cmtDelete = function (obj, list_id) {
  51. var $this = this;
  52. bx_confirm('', function () {
  53. var oData = $this._getDefaultParams();
  54. oData['action'] = 'DeleteList';
  55. oData['list_id'] = list_id;
  56. $.get(
  57. $this._sActionsUrl,
  58. oData,
  59. function (oData) {
  60. $this.processJson(oData, this._oParent);
  61. },
  62. 'json'
  63. );
  64. });
  65. }
  66. BxDolFavorite.prototype.cmtEdit = function (obj, list_id) {
  67. var $this = this;
  68. var oData = $this._getDefaultParams();
  69. oData['action'] = 'EditList';
  70. oData['list_id'] = list_id;
  71. $.get(
  72. $this._sActionsUrl,
  73. oData,
  74. function (oData) {
  75. $this.processJson(oData, $(obj));
  76. },
  77. 'json'
  78. );
  79. }
  80. BxDolFavorite.prototype.onEditFavoriteList = function (oData, oElement) {
  81. var $this = this;
  82. var fContinue = function() {
  83. if(oData && oData.code != 0)
  84. return;
  85. location.reload();
  86. };
  87. if (oData && oData.msg != undefined && oData.msg.length > 0)
  88. bx_alert(oData.msg, fContinue);
  89. else
  90. fContinue();
  91. }
  92. BxDolFavorite.prototype.onFavorite = function(oData, oElement)
  93. {
  94. var $this = this;
  95. // A favorite is a toggle: on when added, off when taken back.
  96. if(typeof(bx_sound) == 'function' && oData)
  97. if(oData.code != undefined && parseInt(oData.code) != 0)
  98. bx_sound('error');
  99. else
  100. bx_sound('toggle', {direction: oData.favorited ? 'forward' : 'back', group: 'reactions'});
  101. var fContinue = function() {
  102. if(oData && oData.code != 0)
  103. return;
  104. if(oData && oData.label_icon)
  105. $(oElement).find('.sys-action-do-icon .sys-icon').attr('class', 'sys-icon ' + oData.label_icon);
  106. if(oData && oData.label_title) {
  107. $(oElement).attr('title', oData.label_title);
  108. $(oElement).find('.sys-action-do-text').html(oData.label_title);
  109. }
  110. if(oData && oData.disabled)
  111. $(oElement).removeAttr('onclick').addClass($(oElement).hasClass('bx-btn') ? 'bx-btn-disabled' : 'bx-favorite-disabled');
  112. var oCounter = $this._getCounter(oElement);
  113. if(oCounter && oCounter.length > 0) {
  114. oCounter.html(oData.countf);
  115. oCounter.parents('.' + $this._sSP + '-counter-holder:first').bx_anim(oData.count > 0 ? 'show' : 'hide');
  116. }
  117. };
  118. if(oData && oData.msg != undefined && oData.msg.length > 0)
  119. bx_alert(oData.msg, fContinue);
  120. else
  121. fContinue();
  122. };
  123. BxDolFavorite.prototype.processJson = function (oData, oElement)
  124. {
  125. oElement = oElement != undefined ? oElement : this._oParent;
  126. var $this = this;
  127. var fContinue = function() {
  128. //--- Show Popup
  129. if(oData && oData.popup != undefined) {
  130. $('.' + $this._sSP + '-do-form').parents(".bx-popup-wrapper").remove();
  131. $(oData.popup).hide().prependTo('body').bxProcessHtml().dolPopup({
  132. pointer: {
  133. el: oElement
  134. },
  135. fog: {
  136. color: '#fff',
  137. opacity: .7
  138. }
  139. });
  140. }
  141. //--- Evaluate JS code
  142. if (oData && oData.eval != undefined)
  143. eval(oData.eval);
  144. };
  145. //--- Show Message
  146. if(oData && oData.message != undefined)
  147. bx_alert(oData.message, fContinue);
  148. else if (oData && oData.redirect != undefined)
  149. document.location = oData.redirect;
  150. else
  151. fContinue();
  152. };
  153. BxDolFavorite.prototype._getButtons = function(oElement) {
  154. if($(oElement).hasClass(this._sSP))
  155. return $(oElement).find('.' + this._sSP + '-button');
  156. else
  157. return $(oElement).parents('.' + this._sSP + ':first').find('.' + this._sSP + '-button');
  158. };
  159. BxDolFavorite.prototype._getCounter = function(oElement) {
  160. if($(oElement).hasClass(this._sSP))
  161. return $(oElement).find('.' + this._sSP + '-counter');
  162. else
  163. return $(oElement).parents('.' + this._sSP + ':first').find('.' + this._sSP + '-counter');
  164. };
  165. BxDolFavorite.prototype._loadingInButton = function(e, bShow) {
  166. if($(e).length)
  167. bx_loading_btn($(e), bShow);
  168. else
  169. bx_loading($('body'), bShow);
  170. };
  171. BxDolFavorite.prototype._getDefaultParams = function() {
  172. var oDate = new Date();
  173. return {
  174. sys: this._sSystem,
  175. object_id: this._iObjId,
  176. _t: oDate.getTime()
  177. };
  178. };
  179. /** @} */