-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
210 lines (172 loc) · 5.13 KB
/
main.js
File metadata and controls
210 lines (172 loc) · 5.13 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Colección principal de secciones disponibles (Set)
const seccionesDisponibles = new Set(["inicio", "servicios", "precios", "contacto"]);
// Orden de navegación (Array)
const ordenMenu = ["inicio", "servicios", "precios", "contacto"];
// Etiquetas amigables para la navegación (Map)
const etiquetas = new Map([
["inicio", "Inicio"],
["servicios", "Servicios"],
["precios", "Precios"],
["contacto", "Contacto"]
]);
const app = document.getElementById("app");
const menu = document.getElementById("menu-principal");
const footer = document.getElementById("footer-dinamico");
const planes = [
{ plan: "Basico", incluye: "1 pagina + formulario", precio: "USD 60", tiempo: "3 dias" },
{ plan: "Pro", incluye: "Hasta 5 paginas + SEO basico", precio: "USD 140", tiempo: "7 dias" },
{ plan: "Premium", incluye: "Tienda simple + soporte", precio: "USD 220", tiempo: "10 dias" }
];
function limpiarApp() {
app.innerHTML = "";
}
function crearCard(titulo) {
const section = document.createElement("section");
section.className = "card";
const h2 = document.createElement("h2");
h2.textContent = titulo;
section.appendChild(h2);
return section;
}
function renderInicio() {
const card = crearCard("Bienvenido");
const texto = document.createElement("p");
texto.textContent = "Explora nuestras secciones usando la barra de navegacion superior.";
card.appendChild(texto);
app.appendChild(card);
}
function renderServicios() {
const card = crearCard("Servicios destacados");
const listaServicios = [
"Landing pages",
"Sitios web corporativos",
"Mantenimiento mensual"
];
const ul = document.createElement("ul");
// Uso de while para recorrer lista de servicios
let indice = 0;
while (indice < listaServicios.length) {
const li = document.createElement("li");
li.textContent = listaServicios[indice];
ul.appendChild(li);
indice += 1;
}
card.appendChild(ul);
app.appendChild(card);
}
function renderPrecios() {
const card = crearCard("Tabla de planes");
const table = document.createElement("table");
const caption = document.createElement("caption");
caption.textContent = "Comparacion de planes mensuales";
table.appendChild(caption);
const thead = document.createElement("thead");
const trHead = document.createElement("tr");
const columnas = ["Plan", "Incluye", "Precio", "Tiempo de entrega"];
// Uso de for clásico
for (let i = 0; i < columnas.length; i += 1) {
const th = document.createElement("th");
th.scope = "col";
th.textContent = columnas[i];
trHead.appendChild(th);
}
thead.appendChild(trHead);
table.appendChild(thead);
const tbody = document.createElement("tbody");
// Uso de for...of
for (const plan of planes) {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${plan.plan}</td>
<td>${plan.incluye}</td>
<td>${plan.precio}</td>
<td>${plan.tiempo}</td>
`;
tbody.appendChild(tr);
}
table.appendChild(tbody);
card.appendChild(table);
app.appendChild(card);
}
function renderContacto() {
const card = crearCard("Formulario de contacto");
const form = document.createElement("form");
form.action = "#";
form.method = "post";
form.innerHTML = `
<label for="nombre">Nombre</label>
<input type="text" id="nombre" name="nombre" placeholder="Tu nombre" required maxlength="50">
<label for="correo">Correo</label>
<input type="email" id="correo" name="correo" placeholder="tucorreo@ejemplo.com" required>
<label for="mensaje">Mensaje</label>
<textarea id="mensaje" name="mensaje" rows="5" minlength="10" maxlength="500" required></textarea>
<div class="acciones">
<button type="submit">Enviar</button>
<button type="reset">Limpiar</button>
</div>
`;
card.appendChild(form);
app.appendChild(card);
}
const renderizadores = new Map([
["inicio", renderInicio],
["servicios", renderServicios],
["precios", renderPrecios],
["contacto", renderContacto]
]);
function marcarActivo(idSeccion) {
const enlaces = menu.querySelectorAll("a");
enlaces.forEach((enlace) => {
if (enlace.dataset.seccion === idSeccion) {
enlace.classList.add("activo");
} else {
enlace.classList.remove("activo");
}
});
}
function cambiarSeccion(idSeccion) {
// Uso de if para validar secciones existentes
if (!seccionesDisponibles.has(idSeccion)) {
return;
}
limpiarApp();
// Uso de switch para decidir renderizado
switch (idSeccion) {
case "inicio":
case "servicios":
case "precios":
case "contacto": {
const render = renderizadores.get(idSeccion);
if (render) {
render();
}
break;
}
default:
renderInicio();
}
marcarActivo(idSeccion);
}
function construirMenu() {
ordenMenu.forEach((idSeccion) => {
const a = document.createElement("a");
a.href = "#";
a.dataset.seccion = idSeccion;
a.textContent = etiquetas.get(idSeccion) || idSeccion;
a.addEventListener("click", (event) => {
event.preventDefault();
cambiarSeccion(idSeccion);
});
menu.appendChild(a);
});
}
function construirFooter() {
const year = new Date().getFullYear();
footer.innerHTML = `<small>${year} - Antonio Vega Studio. Todos los derechos reservados.</small>`;
}
function iniciarApp() {
construirMenu();
construirFooter();
cambiarSeccion("inicio");
}
iniciarApp();