-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
599 lines (511 loc) · 17.4 KB
/
Copy pathmain.js
File metadata and controls
599 lines (511 loc) · 17.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Variables globales
let friends = [];
let sorteoCounter = 0;
// Elementos del DOM
const nameInput = document.getElementById('nameInput');
const addButton = document.getElementById('addButton');
const sortButton = document.getElementById('sortButton');
const clearAllButton = document.getElementById('clearAllButton');
const newDrawButton = document.getElementById('newDrawButton');
const themeToggle = document.getElementById('themeToggle');
const alertMessage = document.getElementById('alertMessage');
const friendsList = document.getElementById('friendsList');
const resultSection = document.getElementById('resultSection');
const resultName = document.getElementById('resultName');
const friendCount = document.getElementById('friendCount');
const sorteoCount = document.getElementById('sorteoCount');
// Inicialización
document.addEventListener('DOMContentLoaded', function() {
initializeApp();
loadTheme();
loadFriendsFromStorage();
// Asegurar que el tema se aplique correctamente después de cargar
setTimeout(() => {
const currentTheme = document.documentElement.getAttribute('data-theme') || 'light';
document.documentElement.setAttribute('data-theme', currentTheme);
}, 100);
});
// Función de inicialización
function initializeApp() {
updateFriendsList();
updateStats();
// Event listeners
addButton.addEventListener('click', addFriend);
sortButton.addEventListener('click', drawFriend);
clearAllButton.addEventListener('click', clearAllFriends);
newDrawButton.addEventListener('click', hideResult);
themeToggle.addEventListener('click', toggleTheme);
// Permitir agregar amigos con Enter
nameInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addFriend();
}
});
// Limpiar alerta cuando el usuario empiece a escribir
nameInput.addEventListener('input', function() {
hideAlert();
// Validar en tiempo real
validateInput();
});
// Focus inicial en el input
nameInput.focus();
}
// Validación de entrada en tiempo real
function validateInput() {
const value = nameInput.value;
const isValid = isValidName(value);
nameInput.style.borderColor = value.length > 0 && !isValid ?
'var(--accent-error)' : 'var(--border-color)';
}
// Función para validar nombre
function isValidName(name) {
// Verificar que solo contenga letras, espacios, acentos y caracteres especiales válidos
const nameRegex = /^[a-zA-ZÀ-ÿ\u00f1\u00d1\s]+$/;
// Verificar longitud mínima y máxima
const minLength = 2;
const maxLength = 50;
// Verificar que no sea solo espacios
const trimmedName = name.trim();
return nameRegex.test(name) &&
trimmedName.length >= minLength &&
trimmedName.length <= maxLength &&
trimmedName.length > 0;
}
// Función para agregar amigo
function addFriend() {
const name = nameInput.value.trim();
// Validar entrada vacía
if (name === '') {
showAlert('⚠️ Por favor, ingresa un nombre válido', 'warning');
nameInput.focus();
return;
}
// Validar formato del nombre
if (!isValidName(name)) {
showAlert('⚠️ El nombre solo puede contener letras, espacios y acentos', 'error');
nameInput.focus();
return;
}
// Validar longitud mínima
if (name.length < 2) {
showAlert('⚠️ El nombre debe tener al menos 2 caracteres', 'warning');
nameInput.focus();
return;
}
// Validar nombre duplicado (case insensitive)
if (friends.some(friend => friend.toLowerCase() === name.toLowerCase())) {
showAlert('⚠️ Este amigo ya está en la lista', 'warning');
nameInput.focus();
return;
}
// Validar límite máximo de amigos
if (friends.length >= 50) {
showAlert('⚠️ Has alcanzado el límite máximo de 50 amigos', 'warning');
return;
}
// Capitalizar primera letra de cada palabra
const formattedName = capitalizeWords(name);
// Agregar amigo a la lista
friends.push(formattedName);
nameInput.value = '';
nameInput.style.borderColor = 'var(--border-color)';
nameInput.focus();
// Actualizar interfaz
updateFriendsList();
updateStats();
hideAlert();
hideResult();
saveFriendsToStorage();
// Mostrar mensaje de éxito
showSuccessMessage(`✅ ${formattedName} ha sido agregado a la lista`);
}
// Función para capitalizar palabras
function capitalizeWords(str) {
return str.toLowerCase().replace(/\b\w/g, char => char.toUpperCase());
}
// Función para actualizar la lista de amigos
function updateFriendsList() {
const sortButton = document.getElementById('sortButton');
const clearButton = document.getElementById('clearAllButton');
if (friends.length === 0) {
friendsList.innerHTML = `
<div class="empty-state">
<i class="fas fa-user-friends empty-icon"></i>
<p>Aún no has agregado ningún amigo.</p>
<p class="empty-subtitle">¡Comienza escribiendo un nombre arriba!</p>
</div>
`;
sortButton.disabled = true;
clearButton.classList.remove('visible');
} else {
friendsList.innerHTML = friends.map((friend, index) => `
<div class="friend-item" data-index="${index}">
<span class="friend-name">${friend}</span>
<div class="friend-actions">
<span class="friend-number">${index + 1}</span>
<button class="btn-remove-friend" onclick="removeFriend(${index})" title="Eliminar amigo">
<i class="fas fa-times"></i>
</button>
</div>
</div>
`).join('');
sortButton.disabled = false;
clearButton.classList.add('visible');
}
}
// Función para eliminar un amigo específico
function removeFriend(index) {
if (index >= 0 && index < friends.length) {
const removedFriend = friends[index];
friends.splice(index, 1);
updateFriendsList();
updateStats();
saveFriendsToStorage();
hideResult();
showSuccessMessage(`❌ ${removedFriend} ha sido eliminado de la lista`);
}
}
// Función para realizar el sorteo
function drawFriend() {
if (friends.length === 0) {
showAlert('⚠️ Necesitas agregar al menos un amigo para realizar el sorteo', 'warning');
return;
}
// Animación de sorteo
showDrawAnimation();
setTimeout(() => {
// Selección aleatoria
const randomIndex = Math.floor(Math.random() * friends.length);
const selectedFriend = friends[randomIndex];
// Mostrar resultado
resultName.textContent = selectedFriend;
resultSection.classList.add('show');
// Actualizar contador de sorteos
sorteoCounter++;
updateStats();
saveSorteoCount();
// Scroll suave al resultado
resultSection.scrollIntoView({
behavior: 'smooth',
block: 'center'
});
// Efectos de confeti
createConfetti();
}, 2000);
}
// Animación de sorteo
function showDrawAnimation() {
sortButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Sorteando...';
sortButton.disabled = true;
let counter = 0;
const animationInterval = setInterval(() => {
if (friends.length > 0) {
const randomFriend = friends[Math.floor(Math.random() * friends.length)];
resultName.textContent = randomFriend;
resultSection.classList.add('show');
}
counter++;
if (counter >= 10) {
clearInterval(animationInterval);
sortButton.innerHTML = '<i class="fas fa-dice-six"></i> Sortear Amigo';
sortButton.disabled = false;
}
}, 200);
}
// Función para crear efecto confeti
function createConfetti() {
const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#f9ca24', '#f0932b', '#eb4d4b', '#6c5ce7'];
for (let i = 0; i < 50; i++) {
setTimeout(() => {
const confetti = document.createElement('div');
confetti.style.cssText = `
position: fixed;
top: -10px;
left: ${Math.random() * 100}vw;
width: 10px;
height: 10px;
background: ${colors[Math.floor(Math.random() * colors.length)]};
border-radius: 50%;
animation: confetti-fall 3s linear forwards;
z-index: 9999;
pointer-events: none;
`;
document.body.appendChild(confetti);
setTimeout(() => {
confetti.remove();
}, 3000);
}, i * 50);
}
}
// CSS para animación de confeti
const style = document.createElement('style');
style.textContent = `
@keyframes confetti-fall {
0% {
transform: translateY(-100vh) rotate(0deg);
opacity: 1;
}
100% {
transform: translateY(100vh) rotate(720deg);
opacity: 0;
}
}
.btn-remove-friend {
background: var(--accent-error);
color: white;
border: none;
border-radius: 50%;
width: 25px;
height: 25px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.8rem;
transition: var(--transition);
margin-left: 0.5rem;
opacity: 0;
visibility: hidden;
}
.friend-item:hover .btn-remove-friend {
opacity: 1;
visibility: visible;
}
.btn-remove-friend:hover {
transform: scale(1.1);
box-shadow: var(--shadow-light);
}
.friend-actions {
display: flex;
align-items: center;
}
`;
document.head.appendChild(style);
// Función para limpiar toda la lista
function clearAllFriends() {
if (friends.length === 0) return;
if (confirm(`¿Estás seguro de que quieres eliminar todos los ${friends.length} amigos de la lista?`)) {
friends = [];
sorteoCounter = 0;
updateFriendsList();
updateStats();
hideResult();
saveFriendsToStorage();
saveSorteoCount();
showSuccessMessage('🗑️ Lista limpiada completamente');
}
}
// Función para actualizar estadísticas
function updateStats() {
friendCount.textContent = friends.length;
sorteoCount.textContent = sorteoCounter;
// Animar números
animateNumber(friendCount);
animateNumber(sorteoCount);
}
// Animación para números
function animateNumber(element) {
element.style.transform = 'scale(1.2)';
setTimeout(() => {
element.style.transform = 'scale(1)';
}, 200);
}
// Funciones para mostrar alertas y mensajes
function showAlert(message, type = 'error') {
alertMessage.textContent = message;
alertMessage.className = `alert show ${type}`;
// Auto-ocultar después de 5 segundos
setTimeout(() => {
hideAlert();
}, 5000);
}
function hideAlert() {
alertMessage.classList.remove('show');
}
function showSuccessMessage(message) {
// Crear elemento de mensaje temporal
const successDiv = document.createElement('div');
successDiv.textContent = message;
successDiv.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: var(--accent-success);
color: white;
padding: 1rem 1.5rem;
border-radius: var(--border-radius);
box-shadow: var(--shadow-medium);
z-index: 10000;
animation: slideInRight 0.5s ease;
max-width: 300px;
`;
document.body.appendChild(successDiv);
setTimeout(() => {
successDiv.style.animation = 'slideOutRight 0.5s ease forwards';
setTimeout(() => {
successDiv.remove();
}, 500);
}, 3000);
}
// CSS para animaciones de mensajes
const messageStyle = document.createElement('style');
messageStyle.textContent = `
@keyframes slideInRight {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slideOutRight {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
}
`;
document.head.appendChild(messageStyle);
function hideResult() {
resultSection.classList.remove('show');
}
// Funciones de tema
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
// Animación del botón con rotación
themeToggle.style.transform = 'rotate(360deg)';
setTimeout(() => {
themeToggle.style.transform = 'rotate(0deg)';
}, 300);
// Debug para verificar el cambio
console.log('Tema cambiado a:', newTheme);
}
function loadTheme() {
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
console.log('Tema cargado:', savedTheme);
}
// Funciones de almacenamiento local
function saveFriendsToStorage() {
try {
localStorage.setItem('amigoSecreto_friends', JSON.stringify(friends));
} catch (error) {
console.warn('No se pudo guardar en localStorage:', error);
}
}
function loadFriendsFromStorage() {
try {
const savedFriends = localStorage.getItem('amigoSecreto_friends');
const savedSorteoCount = localStorage.getItem('amigoSecreto_sorteoCount');
if (savedFriends) {
friends = JSON.parse(savedFriends);
}
if (savedSorteoCount) {
sorteoCounter = parseInt(savedSorteoCount);
}
updateFriendsList();
updateStats();
} catch (error) {
console.warn('No se pudo cargar desde localStorage:', error);
friends = [];
sorteoCounter = 0;
}
}
function saveSorteoCount() {
try {
localStorage.setItem('amigoSecreto_sorteoCount', sorteoCounter.toString());
} catch (error) {
console.warn('No se pudo guardar el contador de sorteos:', error);
}
}
// Funciones de utilidad adicionales
function exportFriendsList() {
if (friends.length === 0) {
showAlert('⚠️ No hay amigos en la lista para exportar', 'warning');
return;
}
const dataStr = JSON.stringify({
friends: friends,
sorteoCount: sorteoCounter,
exportDate: new Date().toISOString()
}, null, 2);
const dataBlob = new Blob([dataStr], {type: 'application/json'});
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'amigo-secreto-lista.json';
link.click();
URL.revokeObjectURL(url);
showSuccessMessage('📁 Lista exportada exitosamente');
}
function importFriendsList(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = JSON.parse(e.target.result);
if (data.friends && Array.isArray(data.friends)) {
friends = data.friends;
sorteoCounter = data.sorteoCount || 0;
updateFriendsList();
updateStats();
saveFriendsToStorage();
saveSorteoCount();
showSuccessMessage('📁 Lista importada exitosamente');
} else {
showAlert('⚠️ Formato de archivo inválido', 'error');
}
} catch (error) {
showAlert('⚠️ Error al leer el archivo', 'error');
}
};
reader.readAsText(file);
}
// Atajos de teclado
document.addEventListener('keydown', function(e) {
// Ctrl/Cmd + Enter para sortear
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter' && !sortButton.disabled) {
drawFriend();
}
// Escape para cerrar resultado
if (e.key === 'Escape' && resultSection.classList.contains('show')) {
hideResult();
}
// Ctrl/Cmd + L para limpiar lista
if ((e.ctrlKey || e.metaKey) && e.key === 'l') {
e.preventDefault();
nameInput.focus();
nameInput.select();
}
});
// Prevenir envío accidental del formulario
document.addEventListener('submit', function(e) {
e.preventDefault();
});
// Manejo de errores globales
window.addEventListener('error', function(e) {
console.error('Error en la aplicación:', e.error);
showAlert('⚠️ Ha ocurrido un error inesperado', 'error');
});
// Service Worker para funcionamiento offline
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js')
.then(function(registration) {
console.log('SW registrado: ', registration);
})
.catch(function(registrationError) {
console.log('SW falló: ', registrationError);
});
});
}