post.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Timeline Timeline
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxTimelinePost(oOptions) {
  11. this._sActionsUri = oOptions.sActionUri;
  12. this._sActionsUrl = oOptions.sActionUrl;
  13. this._sObjName = oOptions.sObjName == undefined ? 'oTimelinePost' : oOptions.sObjName;
  14. this._iOwnerId = oOptions.iOwnerId == undefined ? 0 : parseInt(oOptions.iOwnerId);
  15. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
  16. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  17. this._bAutoAttach = oOptions.bAutoAttach == undefined ? false : oOptions.bAutoAttach;
  18. this._sAutoUploader = oOptions.sAutoUploader == undefined ? '' : oOptions.sAutoUploader;
  19. this._sAutoUploaderId = oOptions.sAutoUploaderId == undefined ? '' : oOptions.sAutoUploaderId;
  20. this._bMediaPriority = oOptions.bMediaPriority == undefined ? false : oOptions.bMediaPriority;
  21. this._iLimitAttachLinks = oOptions.iLimitAttachLinks == undefined ? 0 : oOptions.iLimitAttachLinks;
  22. this._sLimitAttachLinksErr = oOptions.sLimitAttachLinksErr == undefined ? '' : oOptions.sLimitAttachLinksErr;
  23. this._oAttachedLinks = oOptions.oAttachedLinks == undefined ? {} : oOptions.oAttachedLinks;
  24. this._sVideosAutoplay = oOptions.sVideosAutoplay == undefined ? 'off' : oOptions.sVideosAutoplay;
  25. this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
  26. this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
  27. var $this = this;
  28. if (typeof window.glOnInitEditor === 'undefined')
  29. window.glOnInitEditor = [];
  30. $(document).ready(function () {
  31. var oPost = $($this.sIdPost + ' form');
  32. if(!oPost || oPost.length == 0)
  33. oPost = $($this.sIdPostForm);
  34. oPost.each(function() {
  35. $this.initFormPost($(this).attr('id'));
  36. });
  37. });
  38. }
  39. BxTimelinePost.prototype = new BxTimelineMain();
  40. BxTimelinePost.prototype.initFormPost = function(sFormId)
  41. {
  42. var $this = this;
  43. var oForm = $('#' + sFormId);
  44. var oTextarea = oForm.find('textarea');
  45. autosize(oTextarea);
  46. oForm.ajaxForm({
  47. dataType: "json",
  48. beforeSubmit: function (formData, jqForm, options) {
  49. window[$this._sObjName].beforeFormPostSubmit(oForm);
  50. },
  51. success: function (oData) {
  52. window[$this._sObjName].afterFormPostSubmit(oForm, oData);
  53. }
  54. });
  55. $this.initTrackerInsertSpace(sFormId);
  56. if($this._bAutoAttach)
  57. $this.initTrackerInsertImage(sFormId);
  58. };
  59. BxTimelinePost.prototype.onFormPostSubmit = function(oForm)
  60. {
  61. if(this.isLockedForm($(oForm)))
  62. return false;
  63. return true;
  64. };
  65. BxTimelinePost.prototype.beforeFormPostSubmit = function(oForm)
  66. {
  67. this.loadingInButton($(oForm).children().find(':submit'), true);
  68. };
  69. BxTimelinePost.prototype.afterFormPostSubmit = function (oForm, oData)
  70. {
  71. var $this = this;
  72. var fContinue = function() {
  73. if(oData && oData.form != undefined && oData.form_id != undefined) {
  74. $('#' + oData.form_id).replaceWith(oData.form);
  75. $this.initFormPost(oData.form_id);
  76. return;
  77. }
  78. if(oData && oData.id != undefined) {
  79. var iId = parseInt(oData.id);
  80. if(iId <= 0)
  81. return;
  82. // Refresh value in LiveUpdates
  83. if(oLiveUpdates != undefined) {
  84. var aSystems = oLiveUpdates.getSystemsTransient();
  85. for(var sSystem in aSystems)
  86. if(sSystem.startsWith('bx_timeline')) {
  87. oLiveUpdates.setValue(sSystem, oData.id);
  88. break;
  89. }
  90. }
  91. $('.' + $this.sClassView + ':visible').each(function() {
  92. var oView = $(this);
  93. $this._getPost(oView, iId, jQuery.extend({}, $this._oRequestParams, {view: $this._getView(oView), afps_loading: 1}));
  94. // Attach callback on get post */
  95. $this.onGetPost(oView, iId, jQuery.extend({}, $this._oRequestParams, {view: $this._getView(oView), afps_loading: 1}));
  96. });
  97. }
  98. $this._getForm(oForm);
  99. };
  100. this.loadingInButton($(oForm).children().find(':submit'), false);
  101. // Posted: a subtle success; the form back with an error: error.
  102. if(oData && typeof(bx_sound) == 'function')
  103. if(oData.form != undefined && oData.form_id != undefined)
  104. bx_sound('error');
  105. else if(oData.id != undefined && parseInt(oData.id) > 0)
  106. bx_sound('success', {emphasis: 'subtle'});
  107. if(oData && oData.message != undefined)
  108. bx_alert(oData.message, fContinue);
  109. else
  110. fContinue();
  111. };
  112. BxTimelinePost.prototype.initFormAttachLink = function(sFormId)
  113. {
  114. var $this = this;
  115. var oForm = $('#' + sFormId);
  116. oForm.ajaxForm({
  117. dataType: "json",
  118. clearForm: true,
  119. beforeSubmit: function (formData, jqForm, options) {
  120. window[$this._sObjName].beforeFormAttachLinkSubmit(oForm);
  121. },
  122. success: function (oData) {
  123. window[$this._sObjName].afterFormAttachLinkSubmit(oForm, oData);
  124. }
  125. });
  126. };
  127. BxTimelinePost.prototype.beforeFormAttachLinkSubmit = function(oForm)
  128. {
  129. this.loadingInButton($(oForm).children().find(':submit'), true);
  130. };
  131. BxTimelinePost.prototype.afterFormAttachLinkSubmit = function (oForm, oData)
  132. {
  133. var $this = this;
  134. var fContinue = function() {
  135. if(oData && oData.item != undefined) {
  136. $('#' + $this._aHtmlIds['attach_link_popup']).dolPopupHide({});
  137. if(!$.trim(oData.item).length)
  138. return;
  139. //--- Mark that 'attach link' process was finished.
  140. $this._oAttachedLinks[oData.url] = oData.id;
  141. var iEventId = 0;
  142. if(oData && oData.event_id != undefined)
  143. iEventId = parseInt(oData.event_id);
  144. var oItem = $(oData.item).hide();
  145. $('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).prepend(oItem).find('#' + oItem.attr('id')).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  146. $(this).bxProcessHtml();
  147. });
  148. $this.unlockForm($('#' + $this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first'));
  149. return;
  150. }
  151. if(oData && oData.form != undefined && oData.form_id != undefined) {
  152. $('#' + oData.form_id).replaceWith(oData.form);
  153. $this.initFormAttachLink(oData.form_id);
  154. return;
  155. }
  156. };
  157. this.loadingInButton($(oForm).find(':submit'), false);
  158. if(oData && oData.message != undefined)
  159. bx_alert(oData.message, fContinue);
  160. else
  161. fContinue();
  162. };
  163. BxTimelinePost.prototype.deleteAttachLink = function(oLink, iId)
  164. {
  165. var $this = this;
  166. var oData = this._getDefaultData();
  167. oData['id'] = iId;
  168. var oAttachLink = $('#' + this._aHtmlIds['attach_link_item'] + iId);
  169. bx_loading(oAttachLink, true);
  170. jQuery.post (
  171. this._sActionsUrl + 'delete_attach_link/',
  172. oData,
  173. function(oData) {
  174. var fContinue = function() {
  175. if(oData && oData.code != undefined && oData.code == 0) {
  176. oAttachLink.bx_anim('hide', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  177. $(this).remove;
  178. if(oData.url != undefined && oData.url.length != 0)
  179. delete $this._oAttachedLinks[oData.url];
  180. });
  181. }
  182. };
  183. bx_loading(oAttachLink, false);
  184. if(oData && oData.message != undefined)
  185. bx_alert(oData.message, fContinue);
  186. else
  187. fContinue();
  188. },
  189. 'json'
  190. );
  191. return false;
  192. };
  193. BxTimelinePost.prototype.showAttachLink = function(oLink, iEventId)
  194. {
  195. if($(oLink).hasClass('bx-btn-disabled'))
  196. return false;
  197. if(this._iLimitAttachLinks != 0 && Object.keys(this._oAttachedLinks).length >= this._iLimitAttachLinks) {
  198. bx_alert(this._sLimitAttachLinksErr);
  199. return false;
  200. }
  201. var oData = this._getDefaultData();
  202. oData['event_id'] = iEventId;
  203. $(window).dolPopupAjax({
  204. id: {value: this._aHtmlIds['attach_link_popup'], force: true},
  205. url: bx_append_url_params(this._sActionsUri + 'get_attach_link_form/', oData),
  206. closeOnOuterClick: false,
  207. removeOnClose: true
  208. });
  209. return false;
  210. };
  211. BxTimelinePost.prototype.onAttachMediaUploadBefore = function(oUploader)
  212. {
  213. $('#' + oUploader._sResultContainerId).parents('form:first').find(":submit").prop('disabled', true).addClass('bx-btn-disabled');
  214. };
  215. BxTimelinePost.prototype.onAttachMediaUpload = function(oUploader, iEventId)
  216. {
  217. var $this = this;
  218. var oData = this._getDefaultData();
  219. oData['event_id'] = iEventId;
  220. if(this._bMediaPriority) {
  221. var oLinkFormField = $('#' + this._aHtmlIds['attach_link_form_field'] + iEventId);
  222. oLinkFormField.parents('form:first').find('.bx-menu-item.add-link .bx-btn').addClass('bx-btn-disabled');
  223. if(oLinkFormField.children().length > 0) {
  224. oLinkFormField.hide();
  225. jQuery.post (
  226. this._sActionsUrl + 'delete_attach_links/',
  227. oData,
  228. function(oData) {
  229. if(!oData)
  230. return;
  231. var fContinue = function() {
  232. if(oData.code == undefined)
  233. return;
  234. if(oData.code == 0) {
  235. oLinkFormField.html('');
  236. if(oData.urls != undefined && oData.urls.length != 0)
  237. for(var i in oData.urls)
  238. delete $this._oAttachedLinks[oData.urls[i]];
  239. }
  240. oLinkFormField.show();
  241. };
  242. if(oData.message != undefined)
  243. bx_alert(oData.message, fContinue);
  244. else
  245. fContinue();
  246. },
  247. 'json'
  248. );
  249. }
  250. }
  251. $('#' + oUploader._sResultContainerId).parents('form:first').find(":submit").prop('disabled', false).removeClass('bx-btn-disabled');
  252. };
  253. BxTimelinePost.prototype.onAttachMediaRestoreGhosts = function(oUploader, aData)
  254. {
  255. //perform some action after attached media's ghosts restoration.
  256. };
  257. BxTimelinePost.prototype.onAttachMediaDeleteGhost = function(iEventId, sMsg)
  258. {
  259. if(sMsg != 'ok')
  260. return;
  261. var oForm = $('#' + this._aHtmlIds['attach_link_form_field'] + iEventId).parents('form:first');
  262. if(!oForm.find('.bx-tl-uploader-file').length)
  263. oForm.find('.bx-menu-item.add-link .bx-btn').removeClass('bx-btn-disabled');
  264. };
  265. BxTimelinePost.prototype._getForm = function(oElement)
  266. {
  267. var $this = this;
  268. var oData = this._getDefaultData();
  269. jQuery.post (
  270. this._sActionsUrl + 'get_post_form/',
  271. oData,
  272. function(oData) {
  273. if(oData && oData.options != undefined)
  274. $this.updateOptions(oData.options);
  275. if(oData && oData.form != undefined && oData.form_id != undefined) {
  276. $('#' + oData.form_id).replaceWith(oData.form);
  277. $this.initFormPost(oData.form_id);
  278. }
  279. },
  280. 'json'
  281. );
  282. };
  283. BxTimelinePost.prototype._onGetPost = function(oData)
  284. {
  285. var $this = this;
  286. var fContinue = function(oData) {
  287. if(!$.trim(oData.item).length)
  288. return;
  289. var aTypes = Array.isArray(oData.type) ? oData.type : new Array(oData.type);
  290. aTypes.forEach(function(sType) {
  291. oData['type'] = sType;
  292. $this.oView = $($this._getHtmlId('main', oData) + ':visible');
  293. /*
  294. * Current UPF: 'Post to Feed' and Timeline: 'Post to Feed' forms
  295. * should work with both Feed and For You Feed blocks.
  296. */
  297. if(!$this.oView.length && oData['type'] == 'feed')
  298. $this.oView = $($this._getHtmlId('main', jQuery.extend({}, oData, {type: 'feed_and_hot'})) + ':visible');
  299. /*
  300. * For backward compatibility.
  301. * Current UPF: 'Post to Feed' and Timeline: 'Post to Feed' forms
  302. * should work with an old Account Feed ('Owner and Connections') block.
  303. */
  304. if(!$this.oView.length && oData['type'] == 'feed')
  305. $this.oView = $($this._getHtmlId('main', jQuery.extend({}, oData, {type: 'owner_and_connections'})) + ':visible');
  306. var oLoadMore = $this.oView.find('.' + $this.sSP + '-load-more');
  307. if(!oLoadMore.is(':visible'))
  308. oLoadMore.show();
  309. var oEmpty = $this.oView.find('.' + $this.sSP + '-empty');
  310. if(oEmpty.is(':visible'))
  311. oEmpty.hide();
  312. var oContent = $(oData.item).bxProcessHtml();
  313. switch(oData.view) {
  314. case 'timeline':
  315. var oItems = $this.oView.find('.' + $this.sClassItems);
  316. var oDivider = oItems.find('.' + $this.sClassDividerToday);
  317. var bDivider = oDivider.length > 0;
  318. if(bDivider && !oDivider.is(':visible'))
  319. oDivider.show();
  320. oContent.hide();
  321. var oItem = bDivider ? oDivider.after(oContent).next('.' + $this.sClassItem + ':hidden') : oContent.prependTo(oItems);
  322. oItem.bx_anim('show', $this._sAnimationEffect, $this._iAnimationSpeed, function() {
  323. $(this).find('.bx-tl-item-text .bx-tl-content').bxCheckOverflowHeight($this.sSP + '-overflow', function(oElement) {
  324. $this.onFindOverflow(oElement);
  325. });
  326. $this.initFlickity($this.oView);
  327. });
  328. if($this._sVideosAutoplay != 'off')
  329. $this.initVideos($this.oView);
  330. break;
  331. case 'outline':
  332. $this.prependMasonry(oContent, function(oItems) {
  333. $(oItems).find('.bx-tl-item-text .bx-tl-content').bxCheckOverflowHeight($this.sSP + '-overflow', function(oElement) {
  334. $this.onFindOverflow(oElement);
  335. });
  336. $this.initFlickity($this.oView);
  337. });
  338. break;
  339. }
  340. });
  341. };
  342. if(oData && oData.message != undefined && oData.message.length != 0)
  343. bx_alert(oData.message, function() {
  344. fContinue(oData);
  345. });
  346. else
  347. fContinue(oData);
  348. };