GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — taskToDo/script.js at master · D4RCD4RC/taskToDo

Expand file tree

/

script.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
208 lines (167 loc) · 5.41 KB
/

script.js

Copy path

File metadata and controls

208 lines (167 loc) · 5.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'use strict';
/* ==========================================================================
Selección de elementos del DOM
========================================================================== */
const form = document.forms[0];
const taskInput = form.task;
const addButton = document.querySelector('#add');
const cleanButton = document.querySelector('#clean');
const emptyButton = document.querySelector('#delete');
const prioritySelect = document.querySelector('#prio');
form.addEventListener('submit', (e) => {
e.preventDefault();
addTask();
});
const taskUl = document.querySelector('ul');
/* ==========================================================================
Utilidades de Fecha y Prioridad
========================================================================== */
/**
* Devuelve la fecha actual formateada como DD/MM/YYYY
*/
const getFormattedDate = () => {
const date = new Date();
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
};
/**
* Devuelve la prioridad seleccionada
* (normal o importante)
*/
const getPriority = () => {
return prioritySelect.value === 'importante' ? ' 🔴' : '';
};
/**
* Devuelve el texto final que se guardará como tarea
*/
const buildTaskText = () => {
return `${taskInput.value} ► Creada el: ${getFormattedDate()}${getPriority()}`;
};
/* ==========================================================================
LocalStorage
========================================================================== */
// Cargar tareas almacenadas
let taskArr = JSON.parse(localStorage.getItem('tasks')) || [];
/**
* Guarda el array de tareas en localStorage
*/
const saveTasks = () => {
localStorage.setItem('tasks', JSON.stringify(taskArr));
};
/* ==========================================================================
Renderizado del DOM
========================================================================== */
/**
* Renderiza la lista completa en el DOM
*/
const updateList = () => {
taskUl.innerHTML = '';
saveTasks();
const html = taskArr
.map(
({ text, done }, index) => `
<li class="${done ? 'done' : ''}">
<input type="checkbox" data-index="${index}" ${done ? 'checked' : ''}>
<p>${text}</p>
</li>
`
)
.join('');
taskUl.innerHTML = html;
};
// Render inicial
updateList();
/* ==========================================================================
Acciones de la aplicación (CRUD)
========================================================================== */
/**
* Añade una tarea nueva
*/
const addTask = () => {
if (taskInput.value.trim().length < 3) {
return alert('El texto debe tener al menos 3 caracteres');
}
taskArr.unshift({
text: buildTaskText(),
done: false,
});
taskInput.value = '';
updateList();
};
/**
* Alterna entre completada / no completada
*/
const toggleTaskDone = (e) => {
if (!e.target.matches('input')) return;
const index = Number(e.target.dataset.index);
taskArr[index].done = !taskArr[index].done;
updateList();
};
/**
* Elimina únicamente las tareas que estén marcadas como completadas
*/
const cleanCompleted = () => {
taskArr = taskArr.filter((task) => !task.done);
updateList();
};
/**
* Borra todas las tareas si el usuario acierta un pequeño código
*/
const deleteAll = () => {
const answer = prompt('¿Cuánto es 2 + 8?');
if (answer === '10') {
taskArr = [];
updateList();
}
};
// Crear contenedor del toggle
const themeToggleContainer = document.createElement('div');
themeToggleContainer.classList.add('theme-toggle');
// Crear input checkbox
const toggleInput = document.createElement('input');
toggleInput.type = 'checkbox';
toggleInput.id = 'toggleTheme';
// Crear label
const toggleLabel = document.createElement('label');
toggleLabel.classList.add('toggleSwitch');
toggleLabel.setAttribute('for', 'toggleTheme');
// Crear íconos sol/luna
const sunIcon = document.createElement('span');
sunIcon.classList.add('icon','sun');
sunIcon.textContent = '☀️';
const moonIcon = document.createElement('span');
moonIcon.classList.add('icon','moon');
moonIcon.textContent = '🌙';
// Insertar íconos en label
toggleLabel.appendChild(sunIcon);
toggleLabel.appendChild(moonIcon);
// Insertar input y label en el contenedor
themeToggleContainer.appendChild(toggleInput);
themeToggleContainer.appendChild(toggleLabel);
// Insertar toggle en body (puedes cambiar a header si quieres)
document.body.appendChild(themeToggleContainer);
// Evento para cambiar tema
toggleInput.addEventListener('change', () => {
document.body.classList.toggle('dark');
if(document.body.classList.contains('dark')){
localStorage.setItem('theme','dark');
} else {
localStorage.setItem('theme','light');
}
});
// Recuperar tema al cargar página
window.addEventListener('DOMContentLoaded', () => {
if(localStorage.getItem('theme') === 'dark'){
document.body.classList.add('dark');
toggleInput.checked = true;
}
});
/* ==========================================================================
Eventos
========================================================================== */
addButton.addEventListener('click', addTask);
cleanButton.addEventListener('click', cleanCompleted);
emptyButton.addEventListener('click', deleteAll);
taskUl.addEventListener('click', toggleTaskDone);
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.