Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
109 lines (91 loc) · 3.56 KB
/
Copy pathscript.js
File metadata and controls
109 lines (91 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ==========================================================================
CopyCenter — Centro de Cópias | script.js
Front-end only: menu mobile e validação do formulário.
========================================================================== */
(function () {
"use strict";
/* ---------- Menu mobile ---------- */
var navToggle = document.getElementById("nav-toggle");
var mainNav = document.getElementById("main-nav");
if (navToggle && mainNav) {
navToggle.addEventListener("click", function () {
var open = mainNav.classList.toggle("open");
navToggle.setAttribute("aria-expanded", String(open));
});
// Fecha o menu ao clicar numa ligação
mainNav.addEventListener("click", function (e) {
if (e.target.closest("a")) {
mainNav.classList.remove("open");
navToggle.setAttribute("aria-expanded", "false");
}
});
// Fecha o menu com Escape
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && mainNav.classList.contains("open")) {
mainNav.classList.remove("open");
navToggle.setAttribute("aria-expanded", "false");
navToggle.focus();
}
});
}
/* ---------- Formulário de orçamento (validação client-side) ---------- */
var form = document.getElementById("quote-form");
var success = document.getElementById("form-success");
if (form) {
var messages = {
nome: "Indique o seu nome.",
telefone: "Indique um telefone de contacto.",
email: "Indique um email válido.",
tipo: "Escolha o tipo de trabalho.",
quantidade: "Indique a quantidade.",
formato: "Indique o formato ou tamanho."
};
function setError(field, msg) {
var wrap = field.closest(".form-field");
var errorEl = wrap ? wrap.querySelector(".field-error") : null;
if (wrap) wrap.classList.toggle("invalid", Boolean(msg));
if (errorEl) errorEl.textContent = msg || "";
field.setAttribute("aria-invalid", msg ? "true" : "false");
}
function validateField(field) {
var value = field.value.trim();
var msg = "";
if (field.name !== "data" && !value) {
msg = messages[field.name] || "Este campo é obrigatório.";
} else if (field.name === "email" && value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
msg = messages.email;
} else if (field.name === "telefone" && value && !/^[+\d][\d\s().-]{5,}$/.test(value)) {
msg = "Indique um número de telefone válido.";
}
setError(field, msg);
return !msg;
}
// Marca a data mínima como hoje (campo "data pretendida")
var dateField = form.querySelector("#f-data");
if (dateField) {
dateField.min = new Date().toISOString().split("T")[0];
}
form.addEventListener("submit", function (e) {
e.preventDefault();
var fields = form.querySelectorAll("input[required], select[required]");
var firstInvalid = null;
fields.forEach(function (field) {
var ok = validateField(field);
if (!ok && !firstInvalid) firstInvalid = field;
});
if (firstInvalid) {
firstInvalid.focus();
return;
}
// PLACEHOLDER: sem backend — nada é enviado.
form.hidden = true;
success.hidden = false;
success.scrollIntoView({ behavior: "smooth", block: "center" });
});
// Revalida ao corrigir
form.addEventListener("input", function (e) {
var wrap = e.target.closest(".form-field");
if (wrap && wrap.classList.contains("invalid")) validateField(e.target);
});
}
})();