1function AlertSwalBT(mensagem, subMensagem, retorno) { 2 const icone = ['success', 'error', 'warning', 'info'].includes(retorno) ? retorno : 'error'; 3 if (window.Swal && typeof window.Swal.fire === 'function') { 4 window.Swal.fire({ 5 title: mensagem || 'Aviso', 6 text: subMensagem || '', 7 icon: icone, 8 confirmButtonText: 'OK', 9 confirmButtonColor: '#e51c3b', 10 }); 11 return; 12 } 13 window.alert([mensagem, subMensagem].filter(Boolean).join('\n')); 14} 15 16function ValidaForm(formId) { 17 const form = document.getElementById(formId); 18 if (!form) return false; 19 form.classList.add('was-validated'); 20 if (form.checkValidity()) return true; 21 const invalido = form.querySelector(':invalid'); 22 if (invalido) invalido.focus(); 23 return false; 24} 25 26export function initContactForms() { 27 const $ = window.jQuery; 28 if (!$ || $('.FormSubmit').length === 0) return; 29 30 $('.FormSubmit').each(function () { 31 const $form = $(this); 32 if ($form.data('an-form-ready')) return; 33 $form.data('an-form-ready', true); 34 35 $form.find('[name="whatsapp"]').on('input', function () { 36 this.value = this.value.replace(/[^0-9()+\s-]/g, '').slice(0, 20); 37 }); 38 39 $form.on('submit', function (e) { 40 e.preventDefault(); 41 42 const formId = $form.attr('id'); 43 const validacao = ValidaForm(formId); 44 if (validacao === false) return false; 45 46 const API_URL = $form.attr('action'); 47 const bt = $('#' + formId + ' [type="submit"]'); 48 const status = $form.find('[data-form-status]'); 49 const formElement = $form[0]; 50 if (window.FormState) { 51 if (!window.FormState.start(formElement, { button: bt[0], text: 'Enviando...', timeout: 18000 })) return false; 52 } else { 53 bt.html('<i class="fa-solid fa-circle-notch fa-spin" aria-hidden="true"></i> Enviando...'); 54 bt.prop('disabled', true); 55 } 56 status.text('Enviando mensagem...'); 57 58 const data = new FormData($('#' + formId)[0]); 59 $.ajax({ 60 type: 'POST', 61 processData: false, 62 mimeType: 'multipart/form-data', 63 contentType: false, 64 url: API_URL, 65 data, 66 timeout: 15000, 67 success: function (resposta) { 68 let result; 69 try { 70 result = typeof resposta === 'string' ? JSON.parse(resposta) : resposta; 71 } catch (_erro) { 72 result = null; 73 } 74 75 if (result && result.retorno === 'success') { 76 AlertSwalBT(result.mensagem, result.sub_mensagem, 'success'); 77 $('#' + formId)[0].reset(); 78 $form.removeClass('was-validated'); 79 status.text(result.mensagem || 'Mensagem enviada.'); 80 } else if (result && result.mensagem) { 81 AlertSwalBT(result.mensagem, result.sub_mensagem, result.retorno); 82 status.text(result.mensagem); 83 } else { 84 AlertSwalBT('Erro ao executar ação.', 'Entre em contato com o suporte.', 'error'); 85 status.text('Não foi possÃvel enviar a mensagem.'); 86 } 87 }, 88 error: function (xhr, textStatus) { 89 const tempoEsgotado = textStatus === 'timeout'; 90 let resposta = xhr && xhr.responseJSON ? xhr.responseJSON : null; 91 if (!resposta && xhr && typeof xhr.responseText === 'string') { 92 try { resposta = JSON.parse(xhr.responseText); } catch (_erroResposta) { resposta = null; } 93 } 94 const titulo = tempoEsgotado ? 'O envio demorou demais.' : (resposta && resposta.mensagem ? resposta.mensagem : 'Erro ao executar ação.'); 95 const detalhe = tempoEsgotado ? 'Confira sua conexão e tente novamente.' : (resposta && resposta.sub_mensagem ? resposta.sub_mensagem : 'Verifique sua conexão e tente novamente.'); 96 AlertSwalBT(titulo, detalhe, resposta && resposta.retorno ? resposta.retorno : 'error'); 97 status.text(tempoEsgotado ? 'Tempo de envio esgotado. Tente novamente.' : (resposta && resposta.mensagem ? resposta.mensagem : 'Falha de comunicação. Tente novamente.')); 98 }, 99 complete: function () { 100 if (window.FormState) window.FormState.stop(formElement); 101 else bt.prop('disabled', false).text('Enviar mensagem'); 102 }, 103 }); 104 105 return false;
106 }); 107 }); 108}
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.