form.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Ads Ads
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxAdsForm(oOptions) {
  11. this._sActionsUri = oOptions.sActionUri;
  12. this._sActionsUrl = oOptions.sActionUrl;
  13. this._sObjNameForm = oOptions.sObjNameForm;
  14. this._sObjName = oOptions.sObjName == undefined ? 'oBxAdsForm' : oOptions.sObjName;
  15. this._iOwnerId = oOptions.iOwnerId == undefined ? 0 : oOptions.iOwnerId;
  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. BxAdsForm.prototype.selectCategory = function(oButton) {
  22. var oForm = $(oButton).parents('form:first');
  23. var oCategory = oForm.find("[name='category_select']");
  24. if(!oCategory || oCategory.length == 0)
  25. return;
  26. var $this = this;
  27. var oParams = this._getDefaultData();
  28. oParams['category'] = oCategory.val();
  29. var oContextId = oForm.find("[name='context_id']");
  30. if(oContextId && oContextId.length != 0)
  31. oParams['context_id'] = oContextId.val();
  32. this.loadingInBlock(oButton, true);
  33. jQuery.get (
  34. this._sActionsUrl + 'get_category_form',
  35. oParams,
  36. function(oData) {
  37. if(oButton)
  38. $this.loadingInBlock(oButton, false);
  39. processJsonData(oData);
  40. },
  41. 'json'
  42. );
  43. };
  44. BxAdsForm.prototype.onSelectCategory = function(oData) {
  45. if(!oData || !oData.content && oData.content.length == 0)
  46. return;
  47. var oContent = $(oData.content);
  48. var sFormId = oContent.filter('form').attr('id');
  49. if(!sFormId)
  50. return;
  51. $('form#' + sFormId).replaceWith(oContent);
  52. if(window[this._sObjNameForm] != undefined)
  53. window[this._sObjNameForm].resetChanged();
  54. };
  55. BxAdsForm.prototype.loadFromSource = function(oSource, sSourceTypeId, sSourceId) {
  56. var $this = this;
  57. var oDate = new Date();
  58. var oForm = jQuery(oSource).parents('.bx-form-advanced:first');
  59. var oSourceType = oForm.find("[name='" + sSourceTypeId + "']");
  60. var sSourceType = oSourceType.val();
  61. var bSourceType = sSourceType.length != 0;
  62. var oSource = oForm.find("[name='" + sSourceId + "']");
  63. var sSource = oSource.val();
  64. var bSource = sSource.length != 0;
  65. if(!bSourceType || !bSource)
  66. return;
  67. this.loadingInField(oSource, true);
  68. jQuery.get(
  69. this._sActionsUrl + 'load_entry_from_source',
  70. {
  71. source_type: sSourceType,
  72. source: sSource,
  73. _t: oDate.getTime()
  74. },
  75. function(oData) {
  76. $this.loadingInField(oSource, false);
  77. if(!oData || oData.code == undefined || parseInt(oData.code) != 0)
  78. return;
  79. Object.keys(oData.fields).forEach(sKey => {
  80. var oField = oForm.find("[name='" + sKey + "']");
  81. if(oField.lenght == 0)
  82. return;
  83. switch (oData.fields[sKey].type) {
  84. case 'hidden':
  85. case 'text':
  86. oField.val(oData.fields[sKey].value);
  87. break;
  88. case 'text_html':
  89. bx_editor_insert_html(oField.attr('id'), '', oData.fields[sKey].value);
  90. break;
  91. }
  92. });
  93. },
  94. 'json'
  95. );
  96. };
  97. BxAdsForm.prototype.checkName = function(oSource, sTitleId, sNameId, iId) {
  98. var oDate = new Date();
  99. var oForm = jQuery(oSource).parents('.bx-form-advanced:first');
  100. var oName = oForm.find("[name='" + sNameId + "']");
  101. var sName = oName.val();
  102. var bName = sName.length != 0;
  103. var oTitle = oForm.find("[name='" + sTitleId + "']");
  104. var sTitle = oTitle.val();
  105. var bTitle = sTitle.length != 0;
  106. if(!bName && !bTitle)
  107. return;
  108. var sTitleCheck = '';
  109. if(bName)
  110. sTitleCheck = sName;
  111. else if(bTitle) {
  112. sTitleCheck = sTitle;
  113. sTitle = sTitle.replace(/[^A-Za-z0-9_]/g, '-');
  114. sTitle = sTitle.replace(/[-]{2,}/g, '-');
  115. oName.val(sTitle);
  116. }
  117. jQuery.get(
  118. this._sActionsUrl + 'check_name',
  119. {
  120. title: sTitleCheck,
  121. id: iId && parseInt(iId) > 0 ? iId : 0,
  122. _t: oDate.getTime()
  123. },
  124. function(oData) {
  125. if(!oData || oData.name == undefined)
  126. return;
  127. oName.val(oData.name);
  128. },
  129. 'json'
  130. );
  131. };
  132. BxAdsForm.prototype.loadingInBlock = function(e, bShow) {
  133. var oParent = $(e).length ? $(e).parents('.bx-db-container:first') : $('body');
  134. bx_loading(oParent, bShow);
  135. };
  136. BxAdsForm.prototype.loadingInField = function(e, bShow) {
  137. var oParent = $(e).length ? $(e).parents('.bx-form-element-wrapper:first') : $('body');
  138. bx_loading(oParent, bShow);
  139. };
  140. BxAdsForm.prototype._getDefaultData = function() {
  141. var oDate = new Date();
  142. return jQuery.extend({}, this._oRequestParams, {_t:oDate.getTime()});
  143. };
  144. /** @} */