BxDolConnection.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if(oData.err)
  41. bx_alert(oData.msg);
  42. else {
  43. if(oData.eval != undefined)
  44. eval(oData.eval);
  45. else if(!loadDynamicBlockAuto(oLink))
  46. location.reload();
  47. }
  48. },
  49. 'json'
  50. );
  51. };
  52. if (typeof(bConfirm) != 'undefined' && bConfirm)
  53. bx_confirm(_t('_Are_you_sure'), fPerform);
  54. else
  55. fPerform();
  56. };
  57. BxDolConnection.prototype.onConnect = function(oData) {
  58. if(oData.data != 'undefined')
  59. $('#' + this._aHtmlIds['main'] + this._iContentId).replaceWith(oData.data).bxProcessHtml();
  60. };
  61. BxDolConnection.prototype.toggleConnectPopup = function(oLink, iContentId) {
  62. if(!iContentId)
  63. iContentId = this._iContentId;
  64. bx_menu_popup_inline('#' + this._aHtmlIds['do_popup'] + iContentId, oLink);
  65. };
  66. BxDolConnection.prototype.toggleByPopup = function(oLink) {
  67. var $this = this;
  68. var oData = this._getDefaultParams();
  69. oData['act'] = 'GetConnected';
  70. oData['fmt'] = 'html';
  71. $(oLink).dolPopupAjax({
  72. id: this._aHtmlIds['by_popup'] + this._iContentId,
  73. url: bx_append_url_params(this._sActionsUri, oData)
  74. });
  75. };
  76. BxDolConnection.prototype.getUsers = function(oLink, iStart, iPerPage) {
  77. var $this = this;
  78. var oData = this._getDefaultParams();
  79. oData['act'] = 'GetUsers';
  80. oData['start'] = iStart;
  81. oData['per_page'] = iPerPage;
  82. this._loadingInPopup(oLink, true);
  83. $.get(
  84. this._sActionsUri,
  85. oData,
  86. function(oData) {
  87. $this._loadingInPopup(oLink, false);
  88. oData.source = oLink;
  89. processJsonData(oData);
  90. },
  91. 'json'
  92. );
  93. };
  94. BxDolConnection.prototype.onGetUsers = function(oData) {
  95. if(!oData.content)
  96. return;
  97. $(oData.source).parents('.bx-popup-content-wrapped:first').html(oData.content);
  98. };
  99. BxDolConnection.prototype._loadingInButton = function(e, bShow) {
  100. if($(e).length)
  101. bx_loading_btn($(e), bShow);
  102. else
  103. bx_loading($('body'), bShow);
  104. };
  105. BxDolConnection.prototype._loadingInPopup = function(e, bShow) {
  106. var oParent = $(e).length ? $(e).parents('.bx-popup-content:first') : $('body');
  107. bx_loading(oParent, bShow);
  108. };
  109. BxDolConnection.prototype._getDefaultParams = function() {
  110. var oDate = new Date();
  111. return {
  112. obj: this._sSystem,
  113. id: this._iContentId,
  114. mutual: this._bIsMutual,
  115. content_type: this._sContentType,
  116. params: $.param(this._aRequestParams),
  117. _t: oDate.getTime(),
  118. };
  119. };
  120. /** @} */