zeev-form-js
Version:
Repositório com diversos códigos reaproveitáveis e estruturados para utilizar em projetos.
765 lines (627 loc) • 34.1 kB
JavaScript
import { ZeevResources } from './zeev-form-resources.js';
import { ZeevMapping } from './zeev-form-mapping.js';
import { ZeevIntegrations } from './zeev-form-integrations.js';
export var ZeevForm = {
Functions: {
IsNullOrEmptySpace: ((obj) => {
var result = true;
if (obj != undefined && obj != null) {
if (obj.constructor == String && obj.trim() != '' && obj.trim().length > 0)
result = false;
if (obj.constructor == Object && obj != {})
result = false;
if (obj.constructor == Array && obj.length > 0)
result = false;
}
return result;
}),
WriteLogConsole: ((data) => {
if (ZeevIntegrations.Settings.IsDebug)
console.log(data);
}),
ZeevClosest: ((obj, el) => {
if (obj) {
if (obj.nodeName == el.toUpperCase())
return obj;
else
return ZeevForm.Functions.ZeevClosest(obj.parentElement, el);
} else {
return null;
}
}),
ZeevHideTables: ((tableIds, clean) => {
clean = clean || false;
function hideTablesProcess(id, clean) {
var i = 0;
var tbl = document.getElementById(id);
if (tbl) {
tbl.style.display = "none";
if (clean) {
if (tbl.getAttribute("mult") == "S") {
//Apaga as linhas da tabela exceto a primeira.
Array.from(tbl.tBodies[0].rows).forEach(row => {
if (i > 1)
execv2.form.multipletable.deleteRow(row.querySelector('[id="btnDeletewRow"]'));
i++;
});
}
//Apaga os valores dos campos dentro da tabela.
Array.from(tbl.tBodies[0].rows).forEach(row => {
var inputs = row.querySelectorAll('input');
var selects = row.querySelectorAll('select');
var textareas = row.querySelectorAll('textarea');
//Apaga os valores dos inputs
if (inputs.length > 0) {
Array.from(inputs).forEach(obj => {
var type = obj.getAttribute("type");
var xtype = obj.getAttribute("xtype");
//Se o elemento for diferente de botão ou hidden
if (type && type.toUpperCase() != "BUTTON" && type.toUpperCase() != "HIDDEN") {
if (type.toUpperCase() == "TEXT")
obj.value = '';
if (type.toUpperCase() == "RADIO" || type.toUpperCase() == "CHECKBOX")
obj.checked = false;
if (xtype && xtype.toUpperCase() == "FILE") {
var btnDelFile = row.querySelector("[title='Excluir']");
if (btnDelFile)
btnDelFile.click();
}
}
});
}
//Faz as regras para os selects
if (selects.length > 0) {
Array.from(selects).forEach(obj => {
obj.value = '';
});
}
//Faz as regras para os textareas
if (textareas.length > 0) {
Array.from(textareas).forEach(obj => {
obj.value = '';
});
}
});
}
//Remove obrigatoriedade dos campos
Array.from(tbl.tBodies[0].rows).forEach(row => {
var inputs = row.querySelectorAll('input');
var selects = row.querySelectorAll('select');
var textareas = row.querySelectorAll('textarea');
var isrequired = '';
//Faz as regras para os inputs
if (inputs.length > 0) {
Array.from(inputs).forEach(obj => {
var type = obj.getAttribute("type");
isrequired =
(
(obj.getAttribute("data-required") != null && obj.getAttribute("data-required") == "true") ||
(obj.getAttribute("required") != null && obj.getAttribute("required") == "S")
) ? true : false;
//Se o elemento for diferente de botão ou hidden
if (type && type.toUpperCase() != "BUTTON" && type.toUpperCase() != "HIDDEN") {
obj.setAttribute("data-required", isrequired);
obj.setAttribute("required", "N");
}
});
}
//Faz as regras para os selects
if (selects.length > 0) {
Array.from(selects).forEach(obj => {
isrequired =
(
(obj.getAttribute("data-required") != null && obj.getAttribute("data-required") == "true") ||
(obj.getAttribute("required") != null && obj.getAttribute("required") == "S")
) ? true : false;
obj.setAttribute("data-required", isrequired);
obj.setAttribute("required", "N");
});
}
//Faz as regras para os textareas
if (textareas.length > 0) {
Array.from(textareas).forEach(obj => {
isrequired =
(
(obj.getAttribute("data-required") != null && obj.getAttribute("data-required") == "true") ||
(obj.getAttribute("required") != null && obj.getAttribute("required") == "S")
) ? true : false;
obj.setAttribute("data-required", isrequired);
obj.setAttribute("required", "N");
});
}
//Faz as regras para as linhas
if (row.getAttribute("class") != "group" && tbl.getAttribute("mult") != "S")
row.setAttribute("class", "");
});
}
}
if (tableIds != "") {
if (tableIds.indexOf(',') >= 0) {
var arrayIds = tableIds.split(',');
Array.from(arrayIds).forEach(id => {
hideTablesProcess(id.trim(), clean);
});
} else {
hideTablesProcess(tableIds.trim(), clean);
}
}
}),
ZeevShowTables: ((tableIds) => {
function showTablesProcess(id) {
var i = 0;
var tbl = document.getElementById(id);
if (tbl) {
tbl.style.display = "";
//Faz as regras de obrigatoriedade para cada campo da tabela
Array.from(tbl.tBodies[0].rows).forEach(row => {
var inputs = row.querySelectorAll('input');
var selects = row.querySelectorAll('select');
var textareas = row.querySelectorAll('textarea');
var wasrequired = '';
//Faz as regras para os inputs
if (inputs.length > 0) {
Array.from(inputs).forEach(obj => {
var type = obj.getAttribute("type");
wasrequired = obj.getAttribute("data-required");
//Se o elemento for diferente de botão ou hidden
if (type && type.toUpperCase() != "BUTTON" && type.toUpperCase() != "HIDDEN") {
if (wasrequired != null && wasrequired == "true")
obj.setAttribute("required", "S");
}
});
}
//Faz as regras para os selects
if (selects.length > 0) {
Array.from(selects).forEach(obj => {
wasrequired = obj.getAttribute("data-required");
if (wasrequired != null && wasrequired == "true")
obj.setAttribute("required", "S");
});
}
//Faz as regras para os selects
if (textareas.length > 0) {
Array.from(textareas).forEach(obj => {
wasrequired = obj.getAttribute("data-required");
if (wasrequired != null && wasrequired == "true")
obj.setAttribute("required", "S");
});
}
//Adiciona a classe obrigatorio na linha caso necessario
if (row.getAttribute("class") != "group" && tbl.getAttribute("mult") != "S" && wasrequired == "true")
row.setAttribute('class', 'execute-required');
});
}
}
if (tableIds != "") {
if (tableIds.indexOf(',') >= 0) {
var arrayIds = tableIds.split(',');
Array.from(arrayIds).forEach(id => {
showTablesProcess(id.trim());
});
} else {
showTablesProcess(tableIds.trim());
}
}
}),
ZeevHideFields: ((fieldID) => {
if (fieldID != "" && fieldID != null && fieldID != undefined) {
var field;
var radioOrCheckFields;
var fieldType;
var fieldXType;
var tr;
var td0;
var td1;
var isrequired;
//Verifica se existe mais de 1 id
if (fieldID.indexOf(",") >= 0) {
var Ids = fieldID.replace(' ', '');
var arrayIds = Ids.split(',');
Array.from(arrayIds).forEach(id => {
field = document.querySelector('[xname="inp' + id.trim() + '"]');
fieldType = field.getAttribute("type") != null ? field.getAttribute("type") : field.type;
fieldXType = field.getAttribute("xtype");
tr = ZeevForm.Functions.ZeevClosest(field, "tr");
td0 = document.getElementById(`td0${id.trim()}`);
td1 = document.getElementById(`td1${id.trim()}`);
isrequired =
(
(field.getAttribute("data-required") != null && field.getAttribute("data-required") == "true") ||
(field.getAttribute("required") != null && field.getAttribute("required") == "S")
) ? true : false;
//Guarda a obrigatoriedade no novo atributo.
if (field.getAttribute('data-required') == undefined)
field.setAttribute('data-required', isrequired);
if (fieldType != "hidden")
field.setAttribute('required', 'N');
//Se o elemento for diferente de botão ou hidden
if (fieldType && fieldType.toUpperCase() != "BUTTON" && fieldType.toUpperCase() != "HIDDEN") {
if (fieldType.toUpperCase() == "TEXT")
field.value = '';
if (fieldType.toUpperCase() == "SELECT")
field.value = '';
if (fieldType.toUpperCase() == "RADIO" || fieldType.toUpperCase() == "CHECKBOX") {
radioOrCheckFields = document.querySelectorAll('[xname="inp' + id.trim() + '"]');
Array.from(radioOrCheckFields).forEach(f => {
f.checked = false;
});
}
if (fieldType.toUpperCase() == "TEXTAREA")
field.value = '';
if (fieldXType && fieldXType.toUpperCase() == "FILE") {
var btnDelFile = tr.querySelector("[title='Excluir']");
if (btnDelFile)
btnDelFile.click();
}
}
if (fieldType.toUpperCase() == "HIDDEN") {
if (td0 && td1) {
td0.style.display = "none";
td1.style.display = "none";
}
} else {
if (tr)
tr.style.display = "none";
}
if (tr && tr.getAttribute('class') != "group")
tr.setAttribute('class', '');
});
} else {
field = document.querySelector('[xname="inp' + fieldID + '"]');
fieldType = field.getAttribute("type") != null ? field.getAttribute("type") : field.type;
fieldXType = field.getAttribute("xtype");
tr = ZeevForm.Functions.ZeevClosest(field, "tr");
td0 = document.getElementById(`td0${fieldID.trim()}`);
td1 = document.getElementById(`td1${fieldID.trim()}`);
isrequired =
(
(field.getAttribute("data-required") != null && field.getAttribute("data-required") == "true") ||
(field.getAttribute("required") != null && field.getAttribute("required") == "S")
) ? true : false;
//Guarda a obrigatoriedade no novo atributo.
if (field.getAttribute('data-required') == undefined)
field.setAttribute('data-required', isrequired);
//Remove a obrigatoriedade do campo
if (fieldType != "hidden")
field.setAttribute('required', 'N');
//Se o elemento for diferente de botão ou hidden
if (fieldType && fieldType.toUpperCase() != "BUTTON" && fieldType.toUpperCase() != "HIDDEN") {
if (fieldType.toUpperCase() == "TEXT")
field.value = '';
if (fieldType.toUpperCase() == "SELECT" || fieldType.toUpperCase() == "SELECT-ONE")
field.value = '';
if (fieldType.toUpperCase() == "RADIO" || fieldType.toUpperCase() == "CHECKBOX") {
radioOrCheckFields = document.querySelectorAll('[xname="inp' + fieldID.trim() + '"]');
Array.from(radioOrCheckFields).forEach(f => {
f.checked = false;
});
}
if (fieldType.toUpperCase() == "TEXTAREA")
field.value = '';
if (fieldXType && fieldXType.toUpperCase() == "FILE") {
var btnDelFile = tr.querySelector("[title='Excluir']");
if (btnDelFile)
btnDelFile.click();
}
}
if (fieldType.toUpperCase() == "HIDDEN") {
if (td0 && td1) {
td0.style.display = "none";
td1.style.display = "none";
}
} else {
if (tr)
tr.style.display = "none";
}
if (tr && tr.getAttribute('class') != "group")
tr.setAttribute('class', '');
}
}
}),
ZeevShowFields: ((fieldID) => {
if (fieldID != "" && fieldID != null && fieldID != undefined) {
var field;
var fieldType;
var tr;
var td0;
var td1;
var isrequired;
//Verifica se existe mais de 1 id
if (fieldID.indexOf(",") >= 0) {
var Ids = fieldID.replace(' ', '');
var arrayIds = Ids.split(',');
Array.from(arrayIds).forEach(id => {
field = document.querySelector('[xname="inp' + id.trim() + '"]');
fieldType = field.getAttribute("type") != null ? field.getAttribute("type") : field.type;
tr = ZeevForm.Functions.ZeevClosest(field, "tr");
td0 = document.getElementById(`td0${id.trim()}`);
td1 = document.getElementById(`td1${id.trim()}`);
isrequired = field.getAttribute("data-required");
if (isrequired == "true" && fieldType.toUpperCase() != "HIDDEN")
field.setAttribute('required', 'S');
if (fieldType.toUpperCase() == "HIDDEN") {
if (td0 && td1) {
td0.style.display = "";
td1.style.display = "";
}
} else {
if (tr)
tr.style.display = "";
}
if (tr) {
if (isrequired == "true" && tr.getAttribute('class') != "group")
tr.setAttribute('class', 'execute-required');
}
});
} else {
field = document.querySelector('[xname="inp' + fieldID + '"]');
fieldType = field.getAttribute("type") != null ? field.getAttribute("type") : field.type;
tr = ZeevForm.Functions.ZeevClosest(field, "tr");
td0 = document.getElementById(`td0${fieldID.trim()}`);
td1 = document.getElementById(`td1${fieldID.trim()}`);
isrequired = field.getAttribute("data-required");
//Remove a obrigatoriedade do campo
if (isrequired == "true" && fieldType.toUpperCase() != "HIDDEN")
field.setAttribute('required', 'S');
if (fieldType.toUpperCase() == "HIDDEN") {
if (td0 && td1) {
td0.style.display = "";
td1.style.display = "";
}
} else {
if (tr)
tr.style.display = "";
}
if (tr) {
if (isrequired == "true" && tr.getAttribute('class') != "group")
tr.setAttribute('class', 'execute-required');
}
}
}
}),
ZeevShowOrHideSelectOptions: ((selectId, selectValues, op, hasMultiple, tableId) => {
var tbl;
var select;
var arrayOptValues;
if (hasMultiple) {
tbl = document.getElementById(tableId);
Array.from(tbl.tBodies[0].rows).forEach(row => {
select = row.querySelector('[xname="inp' + selectId + '"]');
if (select && select.length > 0) {
//Loop para cada opcao do meu select.
Array.from(select).forEach(option => {
//Verifica se existe mais de uma opcao para ocultar ou mostrar.
if (selectValues.indexOf(",") >= 0) {
arrayOptValues = selectValues.split(",");
//Loop para cada opcao que desejo esconder.
Array.from(arrayOptValues).forEach(arrayOpt => {
if (arrayOpt.toUpperCase() == option.text.toUpperCase()) {
if (op.toUpperCase() == "HIDE") {
option.disabled = true;
option.style.display = 'none';
} else {
option.disabled = false;
option.style.display = 'block';
}
}
});
} else {
if (option.text.toUpperCase() == selectValues.toUpperCase()) {
if (op.toUpperCase() == "HIDE") {
option.disabled = true;
option.style.display = 'none';
} else {
option.disabled = false;
option.style.display = 'block';
}
}
}
});
}
});
} else {
select = document.querySelector('[xname="inp' + selectId + '"]');
if (select.length > 0) {
//Loop para cada option do meu select.
Array.from(select).forEach(option => {
//Verifica se existe mais de uma opcao para ocultar ou mostrar.
if (selectValues.indexOf(",") >= 0) {
arrayOptValues = selectValues.split(",");
//Loop para cada opcao que desejo esconder.
Array.from(arrayOptValues).forEach(arrayOpt => {
if (arrayOpt.toUpperCase() == option.text.toUpperCase()) {
if (op.toUpperCase() == "HIDE") {
option.disabled = true;
option.style.display = 'none';
} else {
option.disabled = false;
option.style.display = 'block';
}
}
});
} else {
if (option.text.toUpperCase() == selectValues.toUpperCase()) {
if (op.toUpperCase() == "HIDE") {
option.disabled = true;
option.style.display = 'none';
} else {
option.disabled = false;
option.style.display = 'block';
}
}
}
});
}
}
}),
ZeevIsRequired: ((fieldId, isRequired) => {
var obj = document.querySelector("[xname='inp" + fieldId + "']");
var tr = ZeevClosest(obj, "tr");
if (isRequired) {
obj.setAttribute('required', 'S');
tr.setAttribute('class', 'execute-required');
} else {
obj.setAttribute('required', 'N');
tr.setAttribute("class", "");
}
}),
ZeevFormatDate: ((strDate) => {
return String(strDate)
.replace(/\D/g, "") //Remove todos os caracteres e deixa apenas numeros;
.slice(0, 8) //Pega somente os 8 caracteres que compoem a data completa dd/MM/YYYY;
.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3"); //Pega somente digitos separando por blocos e fazendo o replace concatenando com a "/".
}),
ZeevAppendMessageField: ((Obj, message, idMessage, isValid) => {
var objOldMsg = document.getElementById(idMessage);
var objNewMsg = document.createElement("span");
objNewMsg.setAttribute("id", idMessage);
//Verifica se existe mensagem de erro.
if (message && idMessage) {
objNewMsg.innerText = message;
objNewMsg.style.fontWeight = "bold";
objNewMsg.style.color = "red";
}
if (Obj.value == "") {
Obj.style.border = "";
if (objOldMsg)
objOldMsg.remove();
} else {
if (isValid) {
if (objOldMsg)
objOldMsg.remove();
Obj.style.border = "1px solid green";
} else {
if (objOldMsg)
objOldMsg.remove();
Obj.style.border = "1px solid red";
Obj.parentElement.appendChild(objNewMsg);
}
}
}),
ZeevCheckCPF: ((Objcpf) => {
var cpf = Objcpf.value;
cpf = cpf.replace(/[^\d]+/g, '');
if (cpf == '') {
ZeevForm.Functions.ZeevAppendMessageField(Objcpf, "CPF invalido!", "spanCpfMessage", false);
Objcpf.value = '';
}
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 ||
cpf == "00000000000" ||
cpf == "11111111111" ||
cpf == "22222222222" ||
cpf == "33333333333" ||
cpf == "44444444444" ||
cpf == "55555555555" ||
cpf == "66666666666" ||
cpf == "77777777777" ||
cpf == "88888888888" ||
cpf == "99999999999") {
ZeevForm.Functions.ZeevAppendMessageField(Objcpf, "CPF invalido!", "spanCpfMessage", false);
Objcpf.value = '';
} else {
// Valida 1o digito
add = 0;
for (i = 0; i < 9; i++) {
add += parseInt(cpf.charAt(i)) * (10 - i);
}
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9))) {
ZeevForm.Functions.ZeevAppendMessageField(Objcpf, "CPF invalido!", "spanCpfMessage", false);
Objcpf.value = '';
} else {
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i++) {
add += parseInt(cpf.charAt(i)) * (11 - i);
}
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10))) {
ZeevForm.Functions.ZeevAppendMessageField(Objcpf, "CPF invalido!", "spanCpfMessage", false);
Objcpf.value = '';
} else {
ZeevForm.Functions.ZeevAppendMessageField(Objcpf, "", "spanCpfMessage", true);
}
}
}
}),
ZeevCheckCNPJ: ((Obj) => {
cnpj = Obj.value.replace(/\./gi, "").replace(/\//gi, "").replace(/-/gi, "");
ZeevForm.Functions.WriteLogConsole(cnpj);
if (!cnpj || cnpj.length != 14
|| cnpj == "00000000000000"
|| cnpj == "11111111111111"
|| cnpj == "22222222222222"
|| cnpj == "33333333333333"
|| cnpj == "44444444444444"
|| cnpj == "55555555555555"
|| cnpj == "66666666666666"
|| cnpj == "77777777777777"
|| cnpj == "88888888888888"
|| cnpj == "99999999999999") {
ZeevForm.Functions.ZeevAppendMessageField(Obj, "CNPJ invalido!", "spanCnpjMessage", false);
Obj.value = '';
return false;
}
var tamanho = cnpj.length - 2;
var numeros = cnpj.substring(0, tamanho);
var digitos = cnpj.substring(tamanho);
var soma = 0;
var pos = tamanho - 7;
for (var i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2)
pos = 9;
}
var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if (resultado != digitos.charAt(0)) {
ZeevForm.Functions.ZeevAppendMessageField(Obj, "CNPJ invalido!", "spanCnpjMessage", false);
Obj.value = '';
return false;
}
tamanho = tamanho + 1;
numeros = cnpj.substring(0, tamanho);
soma = 0;
pos = tamanho - 7;
for (var 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)) {
ZeevForm.Functions.ZeevAppendMessageField(Obj, "CNPJ invalido!", "spanCnpjMessage", false);
Obj.value = '';
return false;
} else {
ZeevForm.Functions.ZeevAppendMessageField(Obj, "", "spanCnpjMessage", true);
return true;
}
}),
ZeevHideBoxCommands: (() => {
//Esconde a box do canto esquerdo.
document.getElementById("commands").parentNode.style.display = "none";
}),
ZeevIncreaseWindowSize: (() => {
//Aumenta o tamanho da tela
document.getElementById("ContainerForm").parentNode.classList.remove("col-lg-10");
document.getElementById("ContainerForm").parentNode.classList.add("col-lg-12");
}),
ZeevMapNativeResources: (() => {
ZeevResources.NativeResources.MessageContainer = new ZeevMapping.Functions.CreateElementMapping('#containerMessages');
ZeevResources.NativeResources.ECMLibraries = new ZeevMapping.Functions.CreateElementMapping('#ContainerRelatedLibraries');
ZeevResources.NativeResources.Loader = new ZeevMapping.Functions.CreateElementMapping('.app-overlay');
ZeevResources.NativeResources.Task = new ZeevMapping.Functions.CreateElementMapping('#inpDsFlowElementAlias');
ZeevResources.NativeResources.CodFlowExecuteUID = new ZeevMapping.Functions.CreateElementMapping('#inpCodFlowExecuteUID');
ZeevResources.NativeResources.CodFlowExecuteTask = new ZeevMapping.Functions.CreateElementMapping('#inpCodFlowExecuteTask');
ZeevResources.NativeResources.CodFlow = new ZeevMapping.Functions.CreateElementMapping('#inpcodigoProcessoModelo');
}),
Init: (() => {
ZeevForm.Functions.ZeevMapNativeResources();
})
}
}