links.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup BaseText Base classes for text modules
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxBaseModTextLinks(oOptions) {
  11. this._sActionsUri = oOptions.sActionUri;
  12. this._sActionsUrl = oOptions.sActionUrl;
  13. this._sObjName = oOptions.sObjName == undefined ? 'oBxBaseModTextLinks' : oOptions.sObjName;
  14. this._iOwnerId = oOptions.iOwnerId == undefined ? 0 : oOptions.iOwnerId;
  15. this._iContentId = oOptions.iContentId == undefined ? 0 : oOptions.iContentId;
  16. this._sFormId = oOptions.sFormId == undefined ? 0 : oOptions.sFormId;
  17. this._sEditorId = oOptions.sEditorId == undefined ? '' : oOptions.sEditorId;
  18. this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
  19. this._iLimitAttachLinks = oOptions.iLimitAttachLinks == undefined ? 0 : oOptions.iLimitAttachLinks;
  20. this._sLimitAttachLinksErr = oOptions.sLimitAttachLinksErr == undefined ? '' : oOptions.sLimitAttachLinksErr;
  21. this._oAttachedLinks = oOptions.oAttachedLinks == undefined ? {} : oOptions.oAttachedLinks;
  22. var $this = this;
  23. $(document).ready(function () {
  24. $this.initFormPost($this._sFormId);
  25. });
  26. }
  27. BxBaseModTextLinks.prototype.initFormPost = function(sFormId)
  28. {
  29. var $this = this;
  30. var oForm = $('#' + sFormId);
  31. var oTextarea = oForm.find('textarea');
  32. if (typeof window.glOnSpaceEnterInEditor === 'undefined')
  33. window.glOnSpaceEnterInEditor = [];
  34. window.glOnSpaceEnterInEditor.push(function (sData, sSelector) {
  35. if(!oTextarea.is(sSelector))
  36. return;
  37. $this.parseContent(oForm, sData, true);
  38. });
  39. var sContent = oTextarea.val();
  40. if(sContent && sContent.length > 0)
  41. this.parseContent(oForm, sContent, false);
  42. if(this._bAutoAttach) {
  43. if (typeof window.glOnInsertImageInEditor === 'undefined')
  44. window.glOnInsertImageInEditor = [];
  45. window.glOnInsertImageInEditor.push(function (oFile, sSelector) {
  46. if(!oTextarea.is(sSelector))
  47. return false;
  48. const oFormData = new FormData();
  49. oFormData.append('file', oFile);
  50. oFormData.append('u', $this._sAutoUploader);
  51. oFormData.append('uid', $this._sAutoUploaderId);
  52. fetch($this._sActionsUrl + 'auto_attach_insertion/', {method: "POST", body: oFormData})
  53. .then(response => response.json())
  54. .then(result => {
  55. processJsonData(result)
  56. });
  57. return true;
  58. });
  59. }
  60. };
  61. BxBaseModTextLinks.prototype.parseContent = function(oForm, sData, bPerformAttach)
  62. {
  63. var oExp, aMatch = null;
  64. oExp = new RegExp(bx_get_regexp('tag'), "ig");
  65. sData = sData.replace(oExp, '');
  66. oExp = new RegExp(bx_get_regexp('mention'), "ig");
  67. sData = sData.replace(oExp, '');
  68. oExp = new RegExp(bx_get_regexp('url'), "ig");
  69. while(aMatch = oExp.exec(sData)) {
  70. var sUrl = aMatch[0].replace(/^(\s|( ))+|(\s|( ))+$/gm,'');
  71. if(!sUrl.length || this._oAttachedLinks[sUrl] != undefined || (this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length >= this._iLimitAttachLinks))
  72. continue;
  73. //--- Mark that 'attach link' process was started.
  74. this._oAttachedLinks[sUrl] = 0;
  75. if(bPerformAttach)
  76. this.addAttachLink(oForm, sUrl);
  77. }
  78. };
  79. BxBaseModTextLinks.prototype.addAttachLink = function(oElement, sUrl)
  80. {
  81. if(!sUrl || (this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length > this._iLimitAttachLinks))
  82. return;
  83. var $this = this;
  84. var oData = this._getDefaultData();
  85. oData['url'] = sUrl;
  86. oData['content_id'] = $this._iContentId;
  87. jQuery.post (
  88. this._sActionsUrl + 'add_attach_link/',
  89. oData,
  90. function(oData) {
  91. if(!oData.id || !oData.item || !$.trim(oData.item).length)
  92. return;
  93. //--- Mark that 'attach link' process was finished.
  94. $this._oAttachedLinks[sUrl] = oData.id;
  95. var iContentId = 0;
  96. if(oData && oData.content_id != undefined)
  97. iContentId = parseInt(oData.content_id);
  98. var oItem = $(oData.item).hide();
  99. $('#' + $this._aHtmlIds['attach_link_form_field'] + iContentId).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  100. $(this).bxProcessHtml();
  101. });
  102. },
  103. 'json'
  104. );
  105. };
  106. BxBaseModTextLinks.prototype.showAttachLink = function(oLink)
  107. {
  108. if(this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length >= this._iLimitAttachLinks) {
  109. bx_alert(this._sLimitAttachLinksErr);
  110. return false;
  111. }
  112. var oData = this._getDefaultData();
  113. oData['content_id'] = this._iContentId;
  114. $(window).dolPopupAjax({
  115. id: {value: this._aHtmlIds['attach_link_popup'], force: true},
  116. url: bx_append_url_params(this._sActionsUri + 'get_attach_link_form/', oData),
  117. closeOnOuterClick: false,
  118. removeOnClose: true
  119. });
  120. return false;
  121. };
  122. BxBaseModTextLinks.prototype.initFormAttachLink = function(sFormId)
  123. {
  124. var $this = this;
  125. var oForm = $('#' + sFormId);
  126. oForm.ajaxForm({
  127. dataType: "json",
  128. clearForm: true,
  129. beforeSubmit: function (formData, jqForm, options) {
  130. window[$this._sObjName].beforeFormAttachLinkSubmit(oForm);
  131. },
  132. success: function (oData) {
  133. window[$this._sObjName].afterFormAttachLinkSubmit(oForm, oData);
  134. }
  135. });
  136. };
  137. BxBaseModTextLinks.prototype.beforeFormAttachLinkSubmit = function(oForm)
  138. {
  139. this.loadingInButton($(oForm).children().find(':submit'), true);
  140. };
  141. BxBaseModTextLinks.prototype.afterFormAttachLinkSubmit = function (oForm, oData)
  142. {
  143. var $this = this;
  144. var fContinue = function() {
  145. if(oData && oData.item != undefined) {
  146. $('#' + $this._aHtmlIds['attach_link_popup']).dolPopupHide({});
  147. if(!$.trim(oData.item).length)
  148. return;
  149. var iContentId = 0;
  150. if(oData && oData.content_id != undefined)
  151. iContentId = parseInt(oData.content_id);
  152. var oItem = $(oData.item).hide();
  153. $('#' + $this._aHtmlIds['attach_link_form_field'] + iContentId).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  154. $(this).bxProcessHtml();
  155. });
  156. $this._oAttachedLinks[oData.url] = oData.id;
  157. return;
  158. }
  159. if(oData && oData.form != undefined && oData.form_id != undefined) {
  160. $('#' + oData.form_id).replaceWith(oData.form);
  161. $this.initFormAttachLink(oData.form_id);
  162. return;
  163. }
  164. };
  165. this.loadingInButton($(oForm).find(':submit'), false);
  166. if(oData && oData.message != undefined)
  167. bx_alert(oData.message, fContinue);
  168. else
  169. fContinue();
  170. };
  171. BxBaseModTextLinks.prototype.deleteAttachLink = function(oLink, iId)
  172. {
  173. var $this = this;
  174. var oData = this._getDefaultData();
  175. oData['id'] = iId;
  176. var oAttachLink = $('#' + this._aHtmlIds['attach_link_item'] + iId);
  177. bx_loading(oAttachLink, true);
  178. jQuery.post (
  179. this._sActionsUrl + 'delete_attach_link/',
  180. oData,
  181. function(oData) {
  182. var fContinue = function() {
  183. if(oData && oData.code != undefined && oData.code == 0) {
  184. oAttachLink.bx_anim('hide', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  185. $(this).remove;
  186. });
  187. }
  188. };
  189. bx_loading(oAttachLink, false);
  190. if(oData && oData.message != undefined)
  191. bx_alert(oData.message, fContinue);
  192. else
  193. fContinue();
  194. },
  195. 'json'
  196. );
  197. return false;
  198. };
  199. BxBaseModTextLinks.prototype._getDefaultData = function() {
  200. var oDate = new Date();
  201. return jQuery.extend({}, this._oRequestParams, {_t:oDate.getTime()});
  202. };
  203. BxBaseModTextLinks.prototype.loadingInButton = function(e, bShow) {
  204. if($(e).length)
  205. bx_loading_btn($(e), bShow);
  206. else
  207. bx_loading($('body'), bShow);
  208. };