1class FormSender { 2 3 data = {}; 4 action = ''; 5 $form = {}; 6 successMessage = ''; 7 errorMessage = ''; 8 successUrl = ''; 9 errorUrl = ''; 10 formValid = false; 11 12 13 constructor() { 14 let This = this; 15 $('form[data-action]').submit(async function (e) { 16 This.$form = $(this); 17 This.formNode = this; 18 e.preventDefault(); 19 This.formPrepare(); 20 21 This.formAction(); 22 }); 23 24 25 $('form[data-action] input').on('invalid', function () { 26 if ($(this).closest('form').data('not_required') !== 1) { 27 $(this).css('border', '1px solid red'); 28 $(this).parents('form').before('<style>form input:valid { border: 1px solid silver !important;}</style>'); 29 } 30 }); 31 32 33 $('form[data-action][data-not_required="1"] [type="submit"]').on('click',async function (e) { 34 This.$form = $(this.closest('form')); 35 This.formNode = this.closest('form'); 36 e.preventDefault(); 37 This.formPrepare(); 38 39 This.formAction(); 40 }); 41 42 43 } 44 45 formPrepare() { 46 47 48 let $this = this.$form; 49 let formNode = this.formNode; 50 51 this.action = $this.data('action'); 52 this.successMessage = $this.data('successmessage'); 53 this.errorMessage = $this.data('errormessage'); 54 this.successUrl = $this.data('successurl'); 55 this.errorUrl = $this.data('errorurl'); 56 this.fancyNode = $this.data('fancynode'); 57 this.file = $this.data('file'); 58 59 this.not_required = $this.data('not_required'); 60 61 62 let formData = new FormData(formNode); 63 64 // formData.append('sessid', BX.message('bitrix_sessid')); 65 66 67 68 this.data = formData; 69 //console.log('data', data); 70 } 71 72 /** 73 * ÐÑполнение ajax запÑоÑа 74 * @param string method - action в конÑÑоллеÑе Astonia\Rssparser\Controller\AjaxActions 75 * @param object data - даннÑе Ð´Ð»Ñ Ð¿ÐµÑедаÑи в конÑÑÐ¾Ð»Ð»ÐµÑ 76 */ 77 async ajaxAction(action, data) { 78 let than = this; 79 80 81 console.log({action,data}); 82 83 return await BX.ajax.runComponentAction(`ajax:feedback`, action, { 84 mode: 'ajax', 85 data: data 86 }).then(function (response) { 87 return response.data; 88 }).catch(error => { 89 console.error(error); 90 alert(error.errors[0].message); // Error: Not Found 91 }); 92 } 93 94 95 async formAction() { 96 let This = this; 97 98 99 console.log('formact'); 100 101 let res = await This.ajaxAction(this.action, this.data); 102 console.log(res); 103 104 This.hideMessages(); 105 if (res && res.id) { 106 //this.successMessage = res.message; 107 This.showSuccess(); 108 109 } else if (res && res.error) { 110 This.showError(); 111 alert(res.error); 112 } 113 114 115 116 /* if (response.data.error) 117 This.errorMessage = response.data.error; 118 */ 119 120 121 } 122 123 showSuccess() { 124 let $form = this.$form; 125 let successMessage = this.successMessage; 126 let successUrl = this.successUrl; 127 let fancyNode = this.fancyNode; 128 let file = this.file; 129 130 //console.log('successUrl', successUrl); 131 if (successUrl) { 132 location.href = successUrl; 133 //console.log('locationhref', location.href); 134 } else if (typeof $.fancybox == 'object' && fancyNode) { 135 $.fancybox.open({ 136 src: '#'+fancyNode, 137 type: 'inline' 138 }); 139 $form.trigger("reset"); 140 } else if (successMessage) { 141 $form.html(''); 142 $form.prepend(`<div class="alert alert-success" role="alert">${successMessage}</div>`); 143 if (file) { 144 // Создаем вÑеменнÑÑ ÑÑÑÐ»ÐºÑ 145 var a = document.createElement('a'); 146 a.href = file; 147 a.download = file.split('/').pop(); // ÐÑполÑзÑем Ð¸Ð¼Ñ Ñайла в каÑеÑÑве пÑедложенного имени Ð´Ð»Ñ ÑÐ¾Ñ ÑÐ°Ð½ÐµÐ½Ð¸Ñ 148 149 // СимÑлиÑÑем клик по ÑÑÑлке 150 document.body.appendChild(a);
150 // ÐобавлÑем ÑÑÑÐ»ÐºÑ Ð² Ñело докÑменÑа 151 a.click(); // ÐÑогÑаммно нажимаем на ÑÑÑÐ»ÐºÑ 152 153 // УдалÑем ÑÑÑÐ»ÐºÑ Ð¿Ð¾Ñле ÑимÑлÑÑии клика 154 document.body.removeChild(a); 155 } 156 } 157 } 158 159 160 showError() { 161 let $form = this.$form; 162 let errorMessage = this.errorMessage; 163 let errorUrl = this.errorUrl; 164 165 if (errorUrl) { 166 location.href = errorUrl; 167 } else if (errorMessage) { 168 //$form.html(''); 169 $form.prepend(`<div class="alert alert-danger" role="alert">${errorMessage}</div>`); 170 171 } 172 } 173 174 hideMessages() { 175 $('.errorMessage').remove(); 176 $('.successMessage').remove(); 177 } 178 179 180 181 182} 183 184 185$(document).ready(function () { 186 window.formsender = new FormSender(); 187 console.log('FormSender load'); 188});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.