Wikincat:Structure e MediaWiki:AddressFromCep.js: mudanças entre as páginas

De Wikincat
(Diferenças entre páginas)
Ir para navegação Ir para pesquisar
imp>Jaider
Sem resumo de edição
 
imp>Jaider
(version without jQuery)
 
Linha 1: Linha 1:
// Adapted from https://viacep.com.br/exemplo/javascript/
=== Wikincat main structure ===
function cleanAddressesFields() {
==== Main templates ====
// It cleans values from addresses fields.
* [[Template:BibRecord]]
document.getElementById("input_6").value = ("");
** [[Template:BibHeader]]
document.getElementById("input_7").value = ("");
** [[Template:BibIndex]]
document.getElementById("input_8").value = ("");
** [[Template:BibRules]]
document.getElementById("input_9").value = ("");
** [[Template:CardCatalog]]
}
* [[Template:AutRecord]]
function myCallback(dados) {
** [[Template:AutHeader]]
if (!("erro" in dados)) {
** [[Template:AutIndex]]
// It updates the addresses fields with the data from the query.
** [[Template:AutRules]]
document.getElementById("input_6").value = (dados.logradouro);
* [[Template:ItemRecord]]
document.getElementById("input_7").value = (dados.bairro);
* [[Template:Classification]]
document.getElementById("input_8").value = (dados.localidade);
* [[Template:EndOfRecord]]
document.getElementById("input_9").value = (dados.uf);
* [[Template:Field]]
}
==== Added templates ====
else {
* [[Template:Classify]]
// The CEP searched was not found.
* [[Template:GetAuhorNotation]]
cleanAddressesFields();
* [[Template:GetBookCover]]
alert("CEP não encontrado.");
==== Helper templates ====
}
* [[Template:Exist]]
}
* [[Template:Select]]
function searchCep(value) {
* [[Template:Field data string]]
// A new variable "cep" is made with only digits.
* [[Template:Field data trim]]
let cep = value.replace(/\D/g, "");
* [[Template:Converter]]
// Testing if the CEP variable has any value.
* [[Template:GenerateCheckDigit]]
if (cep !== "") {
* [[Template:Print ISBN]]
// Regular expression to validate CEP.
==== Exporting and importing ====
let cepValidator = /^[0-9]{8}$/;
* [[Template:MARCtags]]
// Testing CEP validation. If true...
** [[Template:MARCtags/FieldBreaker]]
if (cepValidator.test(cep)) {
* [[Template:Marcxml]]
// It fills the address fields with "..." while the web service is busy.
** [[Template:Marcxml/FieldBreaker]]
document.getElementById("input_6").value = "...";
** [[Template:Marcxml/PrettyFieldBreaker]]
document.getElementById("input_7").value = "...";
* [[Template:MARCXMLexporter]]
document.getElementById("input_8").value = "...";
* [[Template:ISO2709]]
document.getElementById("input_9").value = "...";
** [[Template:Directory]]
// It creates a javaScript element.
** [[Template:ISO2709/ControlFieldDirectoryBuilder]]
let script = document.createElement("script");
** [[Template:ISO2709/DataFieldDirectoryBuilder]]
// It sincronizes with callback.
* [[Template:MARCimporter]]
script.src = "https://viacep.com.br/ws/" + cep + "/json/?callback=myCallback";
==== Main forms ====
// It inserts the script into the document and load the contents.
* [[Form:BibRecord]]
document.body.appendChild(script);
* [[Form:AutRecord]]
}
* [[Form:ItemRecord]]
else {
==== Added forms ====
// The CEP is invalid.
* [[Form:Classify]]
cleanAddressesFields();
* [[Form:Converter]]
alert("Formato de CEP inválido.");
* [[Form:GetAuhorNotation]]
}
* [[Form:GetBookCover]]
}
* [[Form:MARCXMLexporter]]
else {
* [[Form:MARCimporter]]
// The CEP is empty, it cleans the form fields.
* [[Form:Search]]
cleanAddressesFields();
==== Search ====
}
* [[Template:Search]]
}
* [[Template:Result list]]
document.getElementById("input_4").setAttribute("onblur", "searchCep(this.value)");
* [[Template:AgentSearch]]
* [[Template:SubjectSearch]]
==== Categories ====
* [[:Category:Obra]]
==== Modules ====
* [[Module:Directory]]
* [[Module:MARCimporter]]
==== JavaScript ====
* [[MediaWiki:CardCatalog.js]]
* [[MediaWiki:CardToDocx.js]]
* [[MediaWiki:CardToImage.js]]
* [[MediaWiki:CreateAuthorityLink.js]]
* [[MediaWiki:Export2Doc.js]]
* [[MediaWiki:FileSaver.js]]
* [[MediaWiki:Html2canvas.js]]
* [[MediaWiki:Iso2709.js]]
* [[MediaWiki:MARCimporter.js]]

Edição das 16h38min de 11 de julho de 2020

// Adapted from https://viacep.com.br/exemplo/javascript/
function cleanAddressesFields() {
    // It cleans values from addresses fields.
    document.getElementById("input_6").value = ("");
    document.getElementById("input_7").value = ("");
    document.getElementById("input_8").value = ("");
    document.getElementById("input_9").value = ("");
}
function myCallback(dados) {
    if (!("erro" in dados)) {
        // It updates the addresses fields with the data from the query.
        document.getElementById("input_6").value = (dados.logradouro);
        document.getElementById("input_7").value = (dados.bairro);
        document.getElementById("input_8").value = (dados.localidade);
        document.getElementById("input_9").value = (dados.uf);
    }
    else {
        // The CEP searched was not found.
        cleanAddressesFields();
        alert("CEP não encontrado.");
    }
}
function searchCep(value) {
    // A new variable "cep" is made with only digits.
    let cep = value.replace(/\D/g, "");
    // Testing if the CEP variable has any value.
    if (cep !== "") {
        // Regular expression to validate CEP.
        let cepValidator = /^[0-9]{8}$/;
        // Testing CEP validation. If true...
        if (cepValidator.test(cep)) {
            // It fills the address fields with "..." while the web service is busy.
            document.getElementById("input_6").value = "...";
            document.getElementById("input_7").value = "...";
            document.getElementById("input_8").value = "...";
            document.getElementById("input_9").value = "...";
            // It creates a javaScript element.
            let script = document.createElement("script");
            // It sincronizes with callback.
            script.src = "https://viacep.com.br/ws/" + cep + "/json/?callback=myCallback";
            // It inserts the script into the document and load the contents.
            document.body.appendChild(script);
        }
        else {
            // The CEP is invalid.
            cleanAddressesFields();
            alert("Formato de CEP inválido.");
        }
    }
    else {
        // The CEP is empty, it cleans the form fields.
        cleanAddressesFields();
    }
}
document.getElementById("input_4").setAttribute("onblur", "searchCep(this.value)");