$(document).ready(function() {
$('.select2').select2({ width: '100%', placeholder: "Situação"});
$('.menu-toggler-sidemenu').attr('onclick', 'showMenu()');
$(document).on('click', '.group-checkable', checkAllCheckboxOnDataTable);
jqueryMask();
jQuery('.maskTelefone').change(customMaskPhone);
$(document).on('click', '#limparFiltros', limparFiltros);
configDatePicker();
//click in element with data-confirm attribute
$(document).on('click', '[data-confirm]', function(e){
e.preventDefault();
let url = $(this).attr('href');
confirmBox($(this).attr('data-confirm'), function(retorno) {
if (retorno) {
window.location.href = url;
}
});
});
});
/**
* Função para fazer o sistema mimi
* @param {integer} delay
* @returns {undefined}
*/
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
String.prototype.InsertAt=function(CharToInsert,Position){
return this.slice(0,Position) + CharToInsert + this.slice(Position)
}
function customMaskPhone(){
var phone = this.value;
var type_format = phone.length;
if (type_format == 14) {
$(this).val(phone.InsertAt('-',10));
} else if (type_format == 13) {
$(this).val(phone.InsertAt('-',9));
}
}
/**
* Função para mostrar o menu
* @returns {undefined}
*/
function showMenu() {
if (!$('.page-sidebar-wrapper').is(':visible')) {
$('.page-content').removeClass('extra-class');
} else {
$('.page-content').addClass('extra-class');
}
$('.page-sidebar-wrapper').toggle();
}
/**
* Class para mascaras dos formulários
* @returns {undefined}
*/
function jqueryMask() {
$('.maskDate').mask('00/00/0000');
$('.maskTime').mask('00:00:00');
$('.maskDateTime').mask('00/00/0000 00:00:00');
$('.maskCep').mask('00000-000');
$('.maskTelefone').mask('(00) 000000000');
$('.maskTelefoneUs').mask('(000) 000-0000');
$('.maskCpf').mask('000.000.000-00');
$('.maskCnpj').mask('00.000.000/0000-00');
$('.maskUasg').mask('000000');
$('.maskMoney').mask('000.000.000.000.000,00', {reverse: true});
$('.maskPercent').mask('##0,00%', {reverse: true});
$('.maskAtributo').mask('YYYYYYYYYY', {
'translation': {
Y: {pattern: /[A-Za-z0-9\-]/}
}
});
$('.maskIpAdress').mask('0ZZ.0ZZ.0ZZ.0ZZ', {
translation: {
'Z': {
pattern: /[0-9]/, optional: true
}
}
});
}
/**
* Poe todos os checkbox na mesma situção do que tem no header do datatables
* @returns {undefined}
*/
function checkAllCheckboxOnDataTable() {
if ($(this).prop('checked')) {
$('.checkbox-datatables').prop('checked', true);
} else {
$('.checkbox-datatables').prop('checked', false);
}
}
function createFlashMesseger(message, target, success){
var css = (success) ? 'success' : 'danger';
var icon = (success) ? 'check' : 'remove';
var container = jQuery(target);
if($("div:contains("+ message +")").length > 0)
return false;
container.append(jQuery.parseHTML(
'
';
structure += '
';
structure += '
';
structure += '';
structure += '
';
structure += data.message;
structure += '
';
structure += '';
structure += '
';
structure += '
';
return structure;
};
return {
closeLastModal: function(){
$('#modal-personalizado' + data.id).modal("hide");
},
closeModal: function(element){
$('#modal-personalizado' + number).hide("hide");
},
show: function(options){
data = $.extend( data, options );
data.id = $('.modalCont').length + 1;
//structure_btn_no = '
';
//Ao setar a modal excluir a antiga, se houver
$('#modal-personalizado' + data.id).modal("hide");
//inclui a modal na pagina
$("body").append(createStructure());
//Eventos dos botoes
if (isFunction(data.callbackNo) === true)
$('#modal-personalizado' + data.id + ' #structure_btn_no').on('click', data.callbackNo);
if (isFunction(data.callbackYes) === true)
$('#modal-personalizado' + data.id + ' #structure_btn_yes').on('click', data.callbackYes);
if (isFunction(data.callback) === true)
$('#modal-personalizado' + data.id + ' #structure_btn').on('click', data.callback);
//Exibe a modal
$('#modal-personalizado' + data.id).modal("show");
//Evento ao fechar a modal
$('#modal-personalizado' + data.id).on('hidden.bs.modal', function (e) {
$('#modal-personalizado' + data.id).modal("hide");
$('#modal-personalizado' + data.id).remove();
});
console.log(data.id);
}
};
}();
var message =
{
options: {
"closeButton": true,
"debug": false,
"positionClass": "toast-top-right",
"onclick": null,
"showDuration": "1000",
"hideDuration": "1000",
"timeOut": "8000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
},
show: function (options) {
var type = "";
var message = "";
var title = "";
if (isObject(options) === true) {
//[INI] Verificações --------------------------------------------------------------------------------------------
//posição da message
if (options.Posicao === "centro")
options.positionClass = "toast-top-center";
else if (options.Posicao === "esquerda")
this.options.positionClass = "toast-top-left";
else if (options.Posicao === "direita")
this.options.positionClass = "toast-top-right";
else if (options.Posicao === "abaixo")
this.options.positionClass = "toast-bottom-center";
else if (options.Posicao === "esquerda-abaixo")
this.options.positionClass = "toast-bottom-left";
else if (options.Posicao === "direita-abaixo")
this.options.positionClass = "toast-bottom-right";
else
this.options.positionClass = "toast-top-right";
//type de message
if (options.type === "info")
type = "info";
else if (options.type === "success")
type = "success";
else if (options.type === "warning")
type = "warning";
else if (options.type === "error")
type = "error";
else
type = "success";
//title da message
if (options.title)
title = options.title;
else
title = "Termo de Acesso";
//message
message = options.message;
if (options.timeOut) {
this.options.timeOut = options.timeOut;
}
if (options.Onclick) {
this.options.onclick = options.Onclick;
}
if (options.Fechar !== "true" && options.Fechar !== undefined) {
this.options.closeButton = options.Fechar;
}
else {
this.options.closeButton = "true";
}
//[FIM] Verificações --------------------------------------------------------------------------------------------
}
else {
type = "success";
message = options;
title = "Termo de Acesso";
}
//Seta as opções e executa o toast
toastr.options = this.options;
toastr[type](message, title);
//Exclui o array, em caso de lixo de memória
delete options;
}
}
var ajax = function(){
var data = {
loader: "body",
headers: { 'csrftoken' : $('meta[name=_token]').attr('content'), 'request' : 'ajax' },
url: "",
method: "POST",
cache: true,
crossdomain: true,
async: true,
data: "",
timeout: 50000,
success: "",
error: "",
beforeSend: "",
complete: "",
dataType: ""
};
return {
send: function(options){
data = $.extend( data, options );
app.blockUI(data.loader);
$.ajaxSetup({ headers: data.headers });
$.ajax({
type: data.method,
url: data.url,
//dataType: "text/html/json",
cache: data.cache,
crossDomain: data.crossdomain,
async: data.async,
data: data.data,
timeout: data.timeout,
success: function (rs) {
if(rs)
if(rs.refreshToken) $("input[name=_token]").val(rs.refreshToken);
if (isFunction(data.success) === true)
eval(data.success(rs));
app.unblockUI(data.loader);
//Limpa os loaders
return true;
},
beforeSend: data.beforeSend,
error: function (rs) {
if(rs){
if(rs.refreshToken){
$("input[name=_token]").val(rs.refreshToken);
}
}
// se erro em todas as tentativas retorna tip + message de erro
console.log("Erro: " + rs);
if (isFunction(data.error) === true)
eval(data.error(data));
else if (data.error === false)
return false;
else{
message.show({ timeOut: 8000, type: "error",title: "Ops!",message: "Isso é meio constrangedor, mas não conseguimos executar a ação desejada."});
}
app.unblockUI(data.loader);
return false;
}
});
}
}
}();
function isCPF(cpf) {
var cpf = cpf.replace(/[^\d]+/g, '');
var numeros, digitos, soma, i, resultado, digitos_iguais;
digitos_iguais = 1;
if (cpf.length < 11)
return false;
for (i = 0; i < cpf.length - 1; i++)
if (cpf.charAt(i) != cpf.charAt(i + 1)) {
digitos_iguais = 0;
break;
}
if (!digitos_iguais) {
numeros = cpf.substring(0, 9);
digitos = cpf.substring(9);
soma = 0;
for (i = 10; i > 1; i--)
soma += numeros.charAt(10 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0))
return false;
numeros = cpf.substring(0, 10);
soma = 0;
for (i = 11; i > 1; i--)
soma += numeros.charAt(11 - i) * i;
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1))
return false;
return true;
}
else
return false;
}
function isEmail(email) {
var regex = /^([a-z0-9_.+-])+\@(([a-z0-9-])+\.)+([a-z0-9]{2,4})+$/;
return regex.test(email);
}
function isCNPJ(cnpj) {
cnpj = cnpj.replace(/[^\d]+/g,'');
if(cnpj == '') return false;
if (cnpj.length != 14)
return false;
// Elimina CNPJs invalidos conhecidos
if (cnpj == "00000000000000" ||
cnpj == "11111111111111" ||
cnpj == "22222222222222" ||
cnpj == "33333333333333" ||
cnpj == "44444444444444" ||
cnpj == "55555555555555" ||
cnpj == "66666666666666" ||
cnpj == "77777777777777" ||
cnpj == "88888888888888" ||
cnpj == "99999999999999")
return false;
// Valida DVs
tamanho = cnpj.length - 2
numeros = cnpj.substring(0,tamanho);
digitos = cnpj.substring(tamanho);
soma = 0;
pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0))
return false;
tamanho = tamanho + 1;
numeros = cnpj.substring(0,tamanho);
soma = 0;
pos = tamanho - 7;
for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(1))
return false;
return true;
}
function limparFlashMesseger(time) {
var tempo = (time ? time : 7000);
$('.alert').delay(tempo).fadeOut();
}
function validaCPF(cpf){
if (isCPF(cpf.value) == false) {
createFlashMesseger("CPF Inválido!", "#flashMensager", false);
$('#' + cpf.id).val('');
return false;
}
}
function validaCnpj(cnpj){
if (isCNPJ(cnpj.value) == false) {
createFlashMesseger("CNPJ Inválido!", "#flashMensager", false);
$('#' + cnpj.id).val('');
return false;
}
}
function validaEmail(email){
if (isEmail(email.value) == false) {
$(window).scrollTop(0);
createFlashMesseger("E-mail Inválido!", "#flashMensager", false);
$('#' + email.id).val('');
$('#confirma_email').val('');
return false;
}
}