Browse Source

Some improvements

Herton 3 years ago
parent
commit
a71a36ab81
3 changed files with 99 additions and 67 deletions
  1. 20 8
      index.html
  2. 78 58
      script.js
  3. 1 1
      style.css

+ 20 - 8
index.html

@@ -1,18 +1,29 @@
 <!DOCTYPE html>
 <html lang="en">
-
 <head>
     <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <link rel="preconnect" href="https://fonts.gstatic.com">
-    <link href="https://fonts.googleapis.com/css2?family=Signika:wght@300&display=swap" rel="stylesheet">
-    <link rel="stylesheet" href="style.css">
     <title>Bankline</title>
-
+    <meta name="description" content="Simgle page basic server optimized for speed and PWA">
+    <!-- Above the fold styles -->
+    <style></style>
+    <!-- Non blocking styles -->
+    <link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
+    <noscript><link rel="stylesheet" href="styles.css"></noscript>
+    
+    <!-- PWA stuff -->
+    <link rel="manifest" href="manifest.json">
+    <meta name="theme-color" content="white">
+    <link rel="apple-touch-icon" href="apple-touch-icon.png">
+    <link rel="preconnect" href="https://fonts.gstatic.com">
+    <!-- Non blocking font -->
+    <link rel="preload" href="https://fonts.googleapis.com/css2?family=Signika:wght@300&display=swap" as="style" onload="this.onload=null;this.rel='stylesheet'">
+    <noscript><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Signika:wght@300&display=swap"></noscript>
     <link href="crop_square.svg" rel="shortcut icon" type="image/x-icon">
 </head>
-
 <body>
+
     <header>
         <p> Banco Fontineli </p>
     </header>
@@ -83,7 +94,8 @@
     <footer>
         <p>Direitos reservados a Gabriel Fontineli</p>
     </footer>
-    <script type="text/javascript" src="script.js"></script>
+    
+<!-- Assync scripts -->
+<script type="text/javascript" src="scripts.js" async></script>
 </body>
-
 </html>

+ 78 - 58
script.js

@@ -1,4 +1,26 @@
-const validatecpf = (cpf) => {
+'use strict';
+
+window.onload = () => {
+    if ('serviceWorker' in navigator) {
+        navigator.serviceWorker.register('./sw.js');
+        console.info('serviceWorker loaded!');
+    }
+}
+
+var formulario = {};
+(function(formulario){
+    formulario.cpf = document.getElementById('cpf');
+    formulario.bairro = document.getElementById('bairro');
+    formulario.endereco = document.getElementById('endereco')
+    formulario.cidade = document.getElementById('cidade');
+    formulario.estado = document.getElementById('estado');
+    formulario.celular = document.getElementById('celular');
+    formulario.required = document.querySelectorAll(".required");
+    formulario.invalidElement = document.querySelector("span");
+	console.log(formulario);
+})(formulario);
+
+function validatecpf(cpf) {
     if (cpf.length != 11) return false;
 
     let numbers = cpf.substring(0, 9)
@@ -25,34 +47,31 @@ const validatecpf = (cpf) => {
 }
 
 function validate() {
-
-
-    const cpf = document.getElementById("cpf").value.replace(/[^a-z0-9 ]/g, "")
-    const validInputs = document.querySelectorAll(".required")
-    let inputIsValid = validatecpf(cpf)
-    const cpfIsValid = validatecpf(cpf)
-
-    for (i = 0; i < validInputs.length; i++) {
-        if (validInputs[i].value === "") validInputs[i].style["border"] = "1px solid red"
-    }
-    for (i = 0; i < validInputs.length; i++) {
-        if (validInputs[i].value === "") inputIsValid = false;
-        else {
+    const cpf = formulario.cpf.value.replace(/[^a-z0-9 ]/g, "")
+    const requiredInputs = formulario.required;
+    let inputIsValid = false;
+    const cpfIsValid = validatecpf(cpf);
+
+    for (let i = 0; i < requiredInputs.length; i++) {
+        if (requiredInputs[i].value === "")  {
+            requiredInputs[i].style["border"] = "1px solid red"
+            inputIsValid = false;
+        } else {
             inputIsValid = true
-            validInputs[1].style["border"] = "none"
-            document.querySelector("span").style["display"] = "none"
-            validInputs[i].style["border"] = "none"
+            requiredInputs[1].style["border"] = "none"
+            formulario.invalidElement.style["display"] = "none"
+            requiredInputs[i].style["border"] = "none"
         }
     }
 
     if (!inputIsValid || !cpfIsValid) {
         window.alert("Opss! Aconteceu um erro no seu cadastro, verifique se você preencheu todos espaços necessários")
         cpfIsValid ? (
-            validInputs[1].style["border"] = "none",
-            document.querySelector("span").style["display"] = "none"
+            requiredInputs[1].style["border"] = "none",
+            formulario.invalidElement.style["display"] = "none"
         ) : (
-            validInputs[1].style["border"] = "1px solid red",
-            document.querySelector("span").style["display"] = "inline"
+            requiredInputs[1].style["border"] = "1px solid red",
+            formulario.invalidElement.style["display"] = "inline"
         );
         return false
     }
@@ -60,70 +79,71 @@ function validate() {
     return true
 }
 function clearCepInputs() {
-    document.getElementById('endereco').value = ("");
-    document.getElementById('bairro').value = ("");
-    document.getElementById('cidade').value = ("");
-    document.getElementById('estado').value = ("");
+    formulario.endereco.value = ("");
+    formulario.bairro.value = ("");
+    formulario.cidade.value = ("");
+    dformulario.estado.value = ("");
 }
 
 function retrievingCepInputs(cepInputs) {
     if ('erro' in cepInputs) {
         clearCepInputs();
         alert("CEP não encontrado.");
-    }else {
-        document.getElementById('endereco').value = (cepInputs.logradouro);
-        document.getElementById('bairro').value = (cepInputs.bairro);
-        document.getElementById('cidade').value = (cepInputs.localidade);
-        document.getElementById('estado').value = (cepInputs.uf);
+        return;
     }
+    formulario.endereco.value = (cepInputs.logradouro);
+    formulario.bairro.value = (cepInputs.bairro);
+    formulario.cidade.value = (cepInputs.localidade);
+    formulario.estado.value = (cepInputs.uf);
 }
 
 function searchCep(valor) {
 
     const cep = valor.replace(/\D/g, '');
 
-    if (cep === "") clearCepInputs();
-    else {
-        const validateCep = /^[0-9]{8}$/;
-        if (validateCep.test(cep)) {
-            let script = document.createElement('script');
-            script.src = 'https://viacep.com.br/ws/' + cep + '/json/?callback=retrievingCepInputs';
-            document.body.appendChild(script);
-
-        } else {
-            clearCepInputs();
-            alert("Formato de CEP inválido.");
-        }
+    if (cep === "")  {
+        clearCepInputs(); 
+        return;
     }
+
+    const validateCep = /^[0-9]{8}$/;
+    if (! validateCep.test(cep)) {
+        clearCepInputs();
+        alert("Formato de CEP inválido.");
+        return;
+    } 
+
+    const script = document.createElement('script');
+    script.src = 'https://viacep.com.br/ws/' + cep + '/json/?callback=retrievingCepInputs';
+    document.body.appendChild(script);
+
 };
 
 function cpfMask() {
-    let i = document.getElementById("cpf").value.length;
-    if (i === 3 || i === 7)
-        document.getElementById("cpf").value = document.getElementById("cpf").value + ".";
-    else if (i === 11)
-        document.getElementById("cpf").value = document.getElementById("cpf").value + "-";
-    else if (i===14) i = 0
-    }
+    const length = formulario.cpf.value.length;
+    if (length === 3 || length === 7)
+        formulario.cpf.value = formulario.cpf.value + ".";
+    else if (length === 11)
+        formulario.cpf.value = formulario.cpf.value + "-";
+    else if (length===14) length = 0
+}
 
 function phoneMask() {
-    let i = document.getElementById('celular').value.length;
-    switch (i) {
+    const length = formulario.celular.value.length;
+    switch (length) {
         case 0:
-            document.getElementById('celular').value = '('
+            formulario.celular.value = '('
             break
         case 3:
-            document.getElementById('celular').value += ')'
+            formulario.celular.value += ')'
             break
         case 5:
-            document.getElementById('celular').value += ' ';
+            formulario.celular.value += ' ';
             break
         case 10:
-            document.getElementById('celular').value += '-'
+            formulario.celular.value += '-'
             break
         case 15:
             i = 0
-            break
     }
-}
-
+}

+ 1 - 1
style.css

@@ -80,7 +80,7 @@ div{
 }
 
 button {
-    background-color: #ff6737;
+    background-color: #004990;
     color: white;
     display: flex;
     align-items: center;