polls.js 8.2 KB

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