BxDolConnection.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 BxDolConnection(oOptions)
  9. {
  10. this._sSystem = oOptions.sSystem;
  11. this._iContentId = oOptions.iContentId; // profile or content ID to connect with
  12. this._sContentType = oOptions.sContentType;
  13. this._bIsMutual = oOptions.bIsMutual;
  14. this._sObjName = undefined == oOptions.sObjName ? 'oConnection' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
  15. this._sActionsUri = 'conn.php';
  16. this._sActionsUrl = oOptions.sRootUrl + this._sActionsUri; // actions url address
  17. this._sAnimationEffect = 'fade';
  18. this._iAnimationSpeed = 'slow';
  19. this._sSP = undefined == oOptions.sStylePrefix ? 'bx-view' : oOptions.sStylePrefix;
  20. this._aHtmlIds = oOptions.aHtmlIds;
  21. this._aRequestParams = oOptions.aRequestParams;
  22. }
  23. BxDolConnection.prototype.connect = function(oLink, sAction, iContentId, bConfirm) {
  24. var $this = this;
  25. var oData = this._getDefaultParams();
  26. oData['act'] = sAction;
  27. oData['out'] = 'eval';
  28. if(!iContentId)
  29. iContentId = this._iContentId;
  30. var fPerform = function() {
  31. bx_loading_btn(oLink, 1);
  32. $.post(
  33. $this._sActionsUrl,
  34. oData,
  35. function (oData) {
  36. bx_loading_btn(oLink, 0);
  37. $(oLink).parents('.bx-popup-applied:first:visible').dolPopupHide();
  38. if(typeof(oData) != 'object')
  39. return;
  40. // Following or befriending is a toggle: on when made, off when removed or declined.
  41. if(typeof(bx_sound) == 'function')
  42. if(oData.err)
  43. bx_sound('error');
  44. else
  45. bx_sound('toggle', {direction: /^(remove|reject|cancel|unfollow|unsubscribe)/i.test(sAction) ? 'back' : 'forward', group: 'reactions'});
  46. if(oData.err)
  47. bx_alert(oData.msg);
  48. else {
  49. if(oData.eval != undefined)
  50. eval(oData.eval);
  51. else if(!loadDynamicBlockAuto(oLink))
  52. location.reload();
  53. }
  54. },
  55. 'json'
  56. );
  57. };
  58. if (typeof(bConfirm) != 'undefined' && bConfirm)
  59. bx_confirm(_t('_Are_you_sure'), fPerform);
  60. else
  61. fPerform();
  62. };
  63. BxDolConnection.prototype.onConnect = function(oData) {
  64. if(oData.data != 'undefined')
  65. $('#' + this._aHtmlIds['main'] + this._iContentId).replaceWith(oData.data).bxProcessHtml();
  66. };
  67. BxDolConnection.prototype.toggleConnectPopup = function(oLink, iContentId) {
  68. if(!iContentId)
  69. iContentId = this._iContentId;
  70. bx_menu_popup_inline('#' + this._aHtmlIds['do_popup'] + iContentId, oLink);
  71. };
  72. BxDolConnection.prototype.toggleByPopup = function(oLink) {
  73. var $this = this;
  74. var oData = this._getDefaultParams();
  75. oData['act'] = 'GetConnected';
  76. oData['fmt'] = 'html';
  77. $(oLink).dolPopupAjax({
  78. id: this._aHtmlIds['by_popup'] + this._iContentId,
  79. url: bx_append_url_params(this._sActionsUri, oData)
  80. });
  81. };
  82. BxDolConnection.prototype.getUsers = function(oLink, iStart, iPerPage) {
  83. var $this = this;
  84. var oData = this._getDefaultParams();
  85. oData['act'] = 'GetUsers';
  86. oData['start'] = iStart;
  87. oData['per_page'] = iPerPage;
  88. this._loadingInPopup(oLink, true);
  89. $.get(
  90. this._sActionsUri,
  91. oData,
  92. function(oData) {
  93. $this._loadingInPopup(oLink, false);
  94. oData.source = oLink;
  95. processJsonData(oData);
  96. },
  97. 'json'
  98. );
  99. };
  100. BxDolConnection.prototype.onGetUsers = function(oData) {
  101. if(!oData.content)
  102. return;
  103. $(oData.source).parents('.bx-popup-content-wrapped:first').html(oData.content);
  104. };
  105. BxDolConnection.prototype._loadingInButton = function(e, bShow) {
  106. if($(e).length)
  107. bx_loading_btn($(e), bShow);
  108. else
  109. bx_loading($('body'), bShow);
  110. };
  111. BxDolConnection.prototype._loadingInPopup = function(e, bShow) {
  112. var oParent = $(e).length ? $(e).parents('.bx-popup-content:first') : $('body');
  113. bx_loading(oParent, bShow);
  114. };
  115. BxDolConnection.prototype._getDefaultParams = function() {
  116. var oDate = new Date();
  117. return {
  118. obj: this._sSystem,
  119. id: this._iContentId,
  120. mutual: this._bIsMutual,
  121. content_type: this._sContentType,
  122. params: $.param(this._aRequestParams),
  123. _t: oDate.getTime(),
  124. };
  125. };
  126. /** @} */