BxDolForm.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolForm(oOptions)
  9. {
  10. if(typeof oOptions === 'undefined')
  11. return;
  12. this._sObjName = oOptions.sObjName === undefined ? 'oForm' : oOptions.sObjName; // javascript object name, to run current object instance from onTimer
  13. this._sId = oOptions.sId; // form HTML ID
  14. this._sName = oOptions.sName; // form name
  15. this._sObject = oOptions.sObject; // form object
  16. this._sDisplay = oOptions.sDisplay; // form display
  17. this._sActionsUri = 'form.php';
  18. this._sActionsUrl = oOptions.sRootUrl + this._sActionsUri; // actions url address
  19. this._bInitCodeMirror = oOptions.bInitCodeMirror == undefined ? false : oOptions.bInitCodeMirror;
  20. this._bLeavePageConfirmation = oOptions.bLeavePageConfirmation == undefined ? false : oOptions.bLeavePageConfirmation;
  21. this._sTxtLeavePageConfirmation = oOptions.sTxtLeavePageConfirmation === undefined ? _t('_sys_leave_page_confirmation') : oOptions.sTxtLeavePageConfirmation;
  22. this._bChanged = false;
  23. this._sAnimationEffect = 'fade';
  24. this._iAnimationSpeed = 'slow';
  25. this._aHtmlIds = oOptions.aHtmlIds;
  26. if(this._bLeavePageConfirmation)
  27. this.initLeavePageConfirmation();
  28. }
  29. /**
  30. * Note. Is called anytime a form was loaded, even if previously created object of the form is used.
  31. */
  32. BxDolForm.prototype.init = function()
  33. {
  34. if(this._bInitCodeMirror) {
  35. setTimeout(() => {
  36. this.initCodeMirror();
  37. }, 500);
  38. }
  39. };
  40. BxDolForm.prototype.initCodeMirror = function()
  41. {
  42. var oSelector = $('#' + this._sId + ' .bx-form-input-code');
  43. for(var i = 0; i < oSelector.length; i++) {
  44. CodeMirror.fromTextArea(oSelector.get(i), {
  45. lineNumbers: true,
  46. mode: "htmlmixed",
  47. htmlMode: true,
  48. matchBrackets: true
  49. }).on('blur', function(oEditor) {
  50. oEditor.save();
  51. });
  52. }
  53. };
  54. BxDolForm.prototype.initLeavePageConfirmation = function()
  55. {
  56. var $this = this;
  57. var bName = this._sName != undefined && this._sName.length > 0;
  58. var bObject = this._sObject != undefined && this._sObject.length > 0;
  59. if(!bName && !bObject)
  60. return;
  61. var sForm = '';
  62. var oForm = null;
  63. if(bName) {
  64. sForm = this._sName;
  65. oForm = $('#' + sForm);
  66. }
  67. if(!oForm.length && bObject) {
  68. sForm = this._sObject;
  69. oForm = $('#' + sForm);
  70. }
  71. if(!oForm.length)
  72. return;
  73. if(oForm.find('.bx-form-warn:visible').length > 0) {
  74. oForm.find(':text').each(function() {
  75. if($(this).val() > 0)
  76. $this.setChanged();
  77. });
  78. }
  79. document.getElementById(sForm).addEventListener('input', (event) => {
  80. if(!event?.inputType || !event.inputType.length)
  81. return;
  82. $this.setChanged();
  83. });
  84. document.getElementById(sForm).addEventListener('submit', (event) => {
  85. $this.resetChanged();
  86. });
  87. window.addEventListener('beforeunload', (event) => {
  88. if(!$this._bChanged)
  89. return;
  90. event.preventDefault();
  91. event.returnValue = '';
  92. });
  93. $('a').bind('click', function() {
  94. if(!$this._bChanged)
  95. return;
  96. var oLink = $(this);
  97. var sHref = oLink.attr('href');
  98. var sOnclick = oLink.attr('onclick');
  99. if(!sHref || (sOnclick != undefined && sOnclick.trim().length > 0))
  100. return;
  101. if(sHref.indexOf(sUrlRoot) == -1)
  102. sHref = sUrlRoot + sHref;
  103. var oRegExp = new RegExp('^' + bx_get_regexp('url') + '$', 'i');
  104. if(!sHref.trim().match(oRegExp))
  105. return;
  106. event.preventDefault();
  107. bx_confirm($this._sTxtLeavePageConfirmation, function() {
  108. $this.resetChanged();
  109. oLink.get(0).click();
  110. });
  111. });
  112. };
  113. BxDolForm.prototype.setChanged = function() {
  114. this._bChanged = true;
  115. };
  116. BxDolForm.prototype.resetChanged = function() {
  117. this._bChanged = false;
  118. };
  119. BxDolForm.prototype.showHelp = function(oLink, sInputName)
  120. {
  121. var oData = this._getDefaultParams();
  122. oData['a'] = 'get_help';
  123. oData['input'] = sInputName;
  124. $(oLink).dolPopupAjax({
  125. id: {value:this._aHtmlIds['help_popup'] + sInputName, force:1},
  126. url: bx_append_url_params(this._sActionsUri, oData),
  127. closeOnOuterClick: true,
  128. removeOnClose: true,
  129. onBeforeShow: function(oPopup) {
  130. oPopup.addClass('bx-popup-help');
  131. }
  132. });
  133. };
  134. BxDolForm.prototype.pgcTogglePopup = function(oLink, iInputId, sPrivacyObject)
  135. {
  136. var oData = this._getDefaultParams();
  137. oData['a'] = 'get_privacy_group_chooser';
  138. oData['input_id'] = iInputId;
  139. oData['privacy_object'] = sPrivacyObject;
  140. $(oLink).dolPopupAjax({
  141. id: {value:this._aHtmlIds['pgc_popup'] + iInputId, force:1},
  142. url: bx_append_url_params(this._sActionsUri, oData),
  143. closeOnOuterClick: false,
  144. removeOnClose: true,
  145. });
  146. };
  147. BxDolForm.prototype.pgcOnSelectGroup = function(oData)
  148. {
  149. if(oData && parseInt(oData.code) != 0)
  150. return;
  151. if(oData.form_id && oData.chooser_id && oData.icon)
  152. $('#' + oData.form_id + ' #' + oData.chooser_id + ' .bx-form-input-pgc-current .sys-icon').removeClass().addClass('sys-icon ' + oData.icon);
  153. };
  154. BxDolForm.prototype._getDefaultParams = function()
  155. {
  156. var oDate = new Date();
  157. return {
  158. o: this._sObject,
  159. d: this._sDisplay,
  160. _t: oDate.getTime()
  161. };
  162. };
  163. BxDolForm.setCheckBoxValue = function (obj) {
  164. var oHidden = $(obj).parent('div').find('INPUT[type=hidden]')
  165. var val = 0;
  166. if ($(obj).attr("checked") == 'checked') {
  167. val = 1;
  168. }
  169. oHidden.val(val);
  170. }
  171. /** @} */