Skip to content

Instantly share code, notes, and snippets.

@ffdesousa
Created May 7, 2020 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffdesousa/e27c3753310cffa5e44e4da107fe8279 to your computer and use it in GitHub Desktop.
Save ffdesousa/e27c3753310cffa5e44e4da107fe8279 to your computer and use it in GitHub Desktop.
/**
* Arquivo com funções utilizadas para facilitar o desenvolvimento.
*/
class Utils {
constructor() { throw "Essa Classe Não Pode Ser Instanciada." }
}
Utils.Gerais = class {
constructor() { throw "Essa Classe Não Pode Ser Instanciada." }
static getValueCampo(campo) {
if ($(`#${campo}`).val() == "" || $(`#${campo}`).val() == " " || $(`#${campo}`).val() == null || $(`#${campo}`).val() == undefined) {
return null
}
else { return $(`#${campo}`).val() }
}
static validaObrigatorios(classe) {
let retorno = true
$(`.${classe}`).each(function () {
if ($(this).is("input,select,textarea")) {
if (Utils.Gerais.getValueCampo($(this).attr("id")) == null) {
retorno = false;
return false
}
}
});
return retorno
}
static validarCpf(cpf) {
let 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;
}
}
static bloqueiaElemento(...el) {
el.forEach(e => {
let el = `#${e}`
if ($(el).is("input")) { $(el).attr("readonly", true) }
else if ($(el).is("button")) { $(el).attr("disabled", true) }
else if ($(el).is("select[type='zoom']")) { window[e].disable(true) }
})
}
static liberaElemento(...el) {
el.forEach(e => {
let el = `#${e}`
if ($(el).is("input")) { $(el).attr("readonly", false) }
else if ($(el).is("button")) { $(el).attr("disabled", false) }
else if ($(el).is("select[type='zoom']")) { window[e].disable(false) }
})
}
static setValueCampo(campo, valor) {
$(`#${campo}`).attr("value", `${valor}`);
$(`#${campo}`).val(`${valor}`);
}
static radioSelecionado(campo) { return $(`#${campo}`).prop("checked") }
static desabilitaCamposDiv(...fieldsets) {
fieldsets.forEach(f => {
$(`#${f}`).attr("disabled", true);
});
}
static habilitaCamposDiv(...fieldsets) {
fieldsets.forEach(f => {
$(`#${f}`).attr("disabled", false);
});
}
static estaPreenchido(...div) {
var retorno = true;
div.forEach(d => {
$(`#${d} :input`).not(":button").each(() => {
Facilidades.Fluig.removeErroCampos($(this).attr("id"));
if ($(this).attr("id") == undefined || $(this).attr("id") == "fluig-data-table-input") {
console.warn("Seleção inválida de elemento");
}
else {
if ($(this).val() == null || $(this).val() == "") {
Facilidades.Fluig.addErroCampos($(this).attr("id"));
retorno = false
}
}
});
});
return retorno
}
static mostrarElementos(...elementos) {
elementos.forEach(elemento => {
$(`#${elemento}`).show();
});
}
static esconderElementos(...campos) {
campos.forEach(campo => {
$(`#${campo}`).hide();
});
}
static esconderClasse(...classes) {
classes.forEach(classe => {
$(`.${classe}`).hide();
});
}
static mostrarClasse(...classes) {
classes.forEach(classe => {
$(`.${classe}`).show();
});
}
static limparCampos(...campos) {
campos.forEach(campo => {
$(`#${campo}`).val("");
$(`#${campo}`).attr("value", "");
});
}
static limparDiv(...div) {
div.forEach(d => {
$(`#${d}`).find("input,select").val("");
$(`#${d}`).find("textarea").val("");
$(`#${d} :radio`).attr("checked", false);
});
}
static dataAtual() {
return new Date().toLocaleDateString("pt-BR");
}
static horaAtual() {
return new Date().toLocaleTimeString("pt-BR");
}
static isEmpty(...values) {
let retorno = false;
values.forEach(value => {
(DataAccess.getValueCampo(value) == null) ? retorno = true : null
});
return retorno
}
static transformaTimeStamp(data) {
let dataSplit = data.split("/");
let novaData = dataSplit[1] + "/" + dataSplit[0] + "/" + dataSplit[2];
return new Date(novaData).getTime() / 1000;
}
static timeStampToDate(timeStamp) {
return date = new Date(timeStamp * 1000).toLocaleDateString("pt-BR");
}
static buscaCep(cep) {
let url = "https://viacep.com.br/ws/" + cep + "/json";
return new Promise((resolve, reject) => {
$.ajax({
dataType: "jsonp",
url: url,
async: false,
success: function (data) {
resolve(data);
},
error: function (data) {
reject(data);
}
});
});
}
static click(elemento, funcao) {
$(`#${elemento}`).on('click', () => {
funcao();
});
}
static perdeuFoco(elemento, funcao) {
$(`#${elemento}`).blur(() => {
funcao();
});
}
static maiorQueDataAtual(date) {
let dataRecebida = Facilidades.Gerais.transformaTimeStamp(date);
let dataAtual = Facilidades.Gerais.transformaTimeStamp(Facilidades.Gerais.dataAtual());
if (dataRecebida > dataAtual) { return true } else { return false }
}
static enterPressionado(funcao) {
if (event.keyCode == 13) {
funcao();
}
}
}
Utils.Fluig = class {
constructor() { throw "Essa Classe Não Pode Ser Instanciada." }
static calendario(...campos) {
campos.forEach(campo => {
FLUIGC.calendar(`#${campo}`, {
language: "pt-BR"
});
});
}
static dataTable(nomeTabela, nomeTemplate, colunas, dados) {
FLUIGC.datatable(`#${nomeTabela}`, {
dataRequest: dados,
renderContent: `#${nomeTemplate}`,
header: colunas,
search: {
enabled: false,
},
navButtons: {
enabled: false,
},
tableStyle: 'table-striped',
}, function (err, data) { });
}
static logoffWidget() {
$.ajax({
url: WCMAPI.serverURL + "/portal/p/api/servlet/logout.do",
async: false,
dataType: "application/json; charset=utf-8",
type: "GET",
dataType: "JSON"
});
}
static loading(fn) {
let loading = FLUIGC.loading(window);
loading.show();
setTimeout(function () {
fn();
loading.hide();
}, 300);
}
static toast(titulo, msg, tipo) {
FLUIGC.toast({
title: titulo,
message: msg,
type: tipo
});
}
static desabilitaLimpaZoom(...campos) {
campos.forEach(campo => {
window[`${campo}`].clear();
window[`${campo}`].disable(true);
});
}
static modal(titulo, conteudo, size, botoes) {
return FLUIGC.modal({
title: titulo,
content: conteudo,
size: size,
actions: botoes
}, function (result, el, ev) { });
}
static addErroCampos(...campos) {
campos.forEach(campo => {
$(`#${campo}`).val("");
$(`#${campo}`).parent().addClass("has-error");
});
}
static removeErroCampos(...campos) {
campos.forEach(campo => { $(`#${campo}`).parent().removeClass("has-error") });
}
}
Utils.Browser = class {
constructor() { throw "Essa Classe Não Pode Ser Instanciada." }
static setCookie(nome, valor) {
let qntTempo = new Date();
qntTempo.setTime(qntTempo.getTime() + (1 * 60 * 60 * 1000));
let expires = "expires=" + qntTempo.toUTCString();
document.cookie = "" + nome + "= " + valor + ";" + expires + ";path=/";
}
static getCookie(nome) {
let cookie = nome + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") { c = c.substring(1) }
if (c.indexOf(cookie) == 0) { return c.substring(cookie.length, c.length) }
}
return "";
}
static deleteCookie(nome) {
document.cookie = nome + "=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/;";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment