polls.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup BaseGeneral Base classes for text modules
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxBaseModGeneralPolls(oOptions) {
  11. this._sActionsUri = oOptions.sActionUri;
  12. this._sActionsUrl = oOptions.sActionUrl;
  13. this._sObjName = oOptions.sObjName == undefined ? 'oBxBaseModGeneralPolls' : oOptions.sObjName;
  14. this._iOwnerId = oOptions.iOwnerId == undefined ? 0 : oOptions.iOwnerId;
  15. this._iParentContentId = oOptions.iParentContentId == undefined ? 0 : oOptions.iParentContentId;
  16. this._sParentFormId = oOptions.sParentFormId == undefined ? '' : oOptions.sParentFormId;
  17. this._sEditorId = oOptions.sEditorId == undefined ? '' : oOptions.sEditorId;
  18. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
  19. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  20. this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
  21. this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
  22. }
  23. BxBaseModGeneralPolls.prototype.initFlickity = function(oParent) {
  24. if(!oParent)
  25. return;
  26. if(typeof(oParent) == 'string')
  27. oParent = $(oParent);
  28. var sItemClass = 'bx-base-text-poll';
  29. var sItemsClass = 'bx-base-text-polls-showcase';
  30. var oItems = $(oParent).hasClass(sItemsClass) ? oParent : oParent.find('.' + sItemsClass);
  31. if(oItems.find('.' + sItemClass).length <= 1)
  32. return;
  33. oItems.flickity({
  34. cellSelector: 'div.' + sItemClass,
  35. cellAlign: 'left',
  36. imagesLoaded: true,
  37. wrapAround: true,
  38. pageDots: false
  39. });
  40. };
  41. BxBaseModGeneralPolls.prototype.initPollForm = function(sFormId)
  42. {
  43. var $this = this;
  44. var oForm = $('#' + sFormId);
  45. oForm.ajaxForm({
  46. dataType: "json",
  47. clearForm: true,
  48. beforeSubmit: function (formData, jqForm, options) {
  49. window[$this._sObjName].beforePollFormSubmit(oForm);
  50. },
  51. success: function (oData) {
  52. window[$this._sObjName].afterPollFormSubmit(oForm, oData);
  53. }
  54. });
  55. };
  56. BxBaseModGeneralPolls.prototype.beforePollFormSubmit = function(oForm)
  57. {
  58. this.loadingInButton($(oForm).children().find(':submit'), true);
  59. };
  60. BxBaseModGeneralPolls.prototype.afterPollFormSubmit = function (oForm, oData)
  61. {
  62. var $this = this;
  63. var fContinue = function() {
  64. if(oData && oData.item != undefined) {
  65. $('#' + $this._aHtmlIds['add_poll_popup']).dolPopupHide({onHide: function(oPopup) {
  66. oPopup.remove();
  67. }});
  68. if(!$.trim(oData.item).length)
  69. return;
  70. var oItem = $(oData.item).hide();
  71. $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['add_poll_form_field']).append(oItem).find('#' + $this._aHtmlIds['poll'] + oData.id).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed);
  72. return;
  73. }
  74. if(oData && oData.form != undefined && oData.form_id != undefined) {
  75. $('#' + oData.form_id).replaceWith(oData.form);
  76. $this.initPollForm(oData.form_id);
  77. return;
  78. }
  79. };
  80. this.loadingInButton($(oForm).find(':submit'), false);
  81. if(oData && oData.message != undefined)
  82. bx_alert(oData.message, fContinue);
  83. else
  84. fContinue();
  85. };
  86. BxBaseModGeneralPolls.prototype.embedPoll = function(oLink, iId)
  87. {
  88. var sEmbedUrl = this._sActionsUrl + 'embed_poll/' + iId;
  89. var sHtmlId = 'bx-base-general-poll-' + iId;
  90. bx_editor_insert_html (this._sEditorId, sHtmlId, '<iframe id="bx-base-text-embed-poll-' + iId + '" src="' + sEmbedUrl + '" frameborder="0" width="100%" height="240px"></iframe>');
  91. };
  92. BxBaseModGeneralPolls.prototype.deletePoll = function(oLink, iId, aEditors)
  93. {
  94. var $this = this;
  95. bx_confirm('', function() {
  96. var oData = $this._getDefaultData();
  97. oData['id'] = iId;
  98. console.log('#' + $this._sParentFormId, $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['poll'] + iId));
  99. var oPoll = $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['poll'] + iId);
  100. bx_loading(oPoll, true);
  101. if(typeof(bx_editor_remove_img) !== 'undefined') {
  102. bx_editor_remove_img (
  103. aEditors,
  104. ['iframe[src="' + $this._sActionsUrl + 'embed_poll/' + iId + '"]', '#bx-base-text-embed-poll-' + iId]
  105. );
  106. }
  107. jQuery.post (
  108. $this._sActionsUrl + 'delete_poll/',
  109. oData,
  110. function(oData) {
  111. var fContinue = function() {
  112. if(oData && oData.code != undefined && oData.code == 0) {
  113. oPoll.bx_anim('hide', $this._sAnimationEffect, $this._sAnimationSpeed, function() {
  114. $(this).remove;
  115. });
  116. }
  117. };
  118. bx_loading(oPoll, false);
  119. if(oData && oData.message != undefined)
  120. bx_alert(oData.message, fContinue);
  121. else
  122. fContinue();
  123. },
  124. 'json'
  125. );
  126. });
  127. return false;
  128. };
  129. BxBaseModGeneralPolls.prototype.showPollForm = function(oLink)
  130. {
  131. var oData = this._getDefaultData();
  132. oData['parent_cid'] = this._iParentContentId;
  133. $(window).dolPopupAjax({
  134. id: {value: this._aHtmlIds['add_poll_popup'], force: true},
  135. url: bx_append_url_params(this._sActionsUri + 'get_poll_form/', oData),
  136. closeOnOuterClick: false
  137. });
  138. return false;
  139. };
  140. BxBaseModGeneralPolls.prototype.changePollView = function(oLink, sView, iPollId, onComplete) {
  141. var $this = this;
  142. this.loadingInBox(oLink, true);
  143. jQuery.get (
  144. this._sActionsUrl + 'get_poll',
  145. {
  146. poll_id: iPollId,
  147. view: sView
  148. },
  149. function(oData) {
  150. if(oLink)
  151. $this.loadingInBox(oLink, false);
  152. if(!oData.content)
  153. return;
  154. var sPollId = $this._aHtmlIds['poll'] + iPollId;
  155. var sContentId = $this._aHtmlIds['poll_content'] + iPollId;
  156. var oPoll = $(oLink).parents('.bx-db-container:first').find('#' + sPollId);
  157. if(!oPoll.length)
  158. oPoll = $(oLink).parents('#' + sPollId + ':first');
  159. if(!oPoll.length)
  160. oPoll = $('#' + sPollId);
  161. if(!oPoll.length)
  162. return;
  163. var oContent = $(oLink).parents('.bx-db-container:first').find('#' + sContentId);
  164. if(!oContent.length)
  165. oContent = $(oLink).parents('.bx-base-text-poll:first').find('#' + sContentId);
  166. if(!oContent.length)
  167. oContent = $('#' + sContentId);
  168. if(!oContent.length)
  169. return;
  170. oContent.bx_anim('hide', $this._sAnimationEffect, $this._iAnimationSpeed, function() {
  171. $(this).replaceWith(oData.content);
  172. if(typeof onComplete === 'function')
  173. onComplete(oPoll, iPollId, oData);
  174. });
  175. },
  176. 'json'
  177. );
  178. };
  179. BxBaseModGeneralPolls.prototype.addPollAnswer = function(oButton, sName) {
  180. var oButton = $(oButton);
  181. var oSubentry = oButton.parents('#bx-form-element-' + sName).find('.bx-form-input-answer:hidden:first').clone();
  182. oButton.parents('.bx-form-input-answer-add:first').before(oSubentry.removeClass('bx-fi-answer-blank').find("input[type = 'text']").removeAttr('disabled').end());
  183. };
  184. BxBaseModGeneralPolls.prototype.deletePollAnswer = function(oButton) {
  185. $(oButton).parents('.bx-form-input-answer:first').remove();
  186. };
  187. BxBaseModGeneralPolls.prototype.onPollAnswerVote = function(oLink, oData, iPollId) {
  188. var $this = this;
  189. this.changePollView(oLink, 'results', iPollId, function(oPoll, iPollId, oData) {
  190. var sMenuLink = $this._aHtmlIds['poll_view_link_results'] + iPollId;
  191. var oMenuLink = $(oPoll).find('.bx-menu-inter:first #' + sMenuLink);
  192. if(oMenuLink.length > 0)
  193. oMenuLink.parent().siblings('.bx-menu-inter-act:visible').hide().siblings('.bx-menu-inter-pas:hidden').show().siblings('#' + sMenuLink + '-pas:visible').hide().siblings('#' + sMenuLink + '-act:hidden').show();
  194. });
  195. };
  196. BxBaseModGeneralPolls.prototype.loadingInButton = function(e, bShow) {
  197. if($(e).length)
  198. bx_loading_btn($(e), bShow);
  199. else
  200. bx_loading($('body'), bShow);
  201. };
  202. BxBaseModGeneralPolls.prototype.loadingInBox = function(e, bShow) {
  203. var oParent = $(e).length ? $(e).parents('.bx-base-text-poll:first') : $('body');
  204. bx_loading(oParent, bShow);
  205. };
  206. BxBaseModGeneralPolls.prototype.loadingInBlock = function(e, bShow) {
  207. var oParent = $(e).length ? $(e).parents('.bx-db-container:first') : $('body');
  208. bx_loading(oParent, bShow);
  209. };
  210. BxBaseModGeneralPolls.prototype._getDefaultData = function() {
  211. var oDate = new Date();
  212. return jQuery.extend({}, this._oRequestParams, {_t:oDate.getTime()});
  213. };
  214. /** @} */