-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (137 loc) · 5.58 KB
/
Copy pathscript.js
File metadata and controls
162 lines (137 loc) · 5.58 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
const fecha = new Date();
const actual = fecha.getFullYear();
document.getElementById("anio").innerText = "Desde 2025 a " + actual;
const input = document.getElementById("input-ave");
const suggestion = document.getElementById("idSuggestion");
const suggestionBox = document.getElementById("idSuggestionBox");
const notes = document.getElementById("notes");
input.addEventListener("input", () => {
const busqueda = input.value.toLowerCase();
suggestion.innerHTML = '';
if (busqueda) {
fetch('https://corsproxy.io/?https://raw.githubusercontent.com/DiegoCMJava/aviario-cartagenero/main/aves.json') /* fetch("aves.json")*/
.then(res => res.json())
.then(data => {
// Crear un array de nombres únicos
const nombresUnicos = [...new Set(data.map(ave => ave.nombre_comun))];
// Filtrar por coincidencias con lo escrito
const coincidencias = nombresUnicos.filter(nombre =>
nombre.toLowerCase().includes(busqueda)
);
// Mostrar sugerencias sin repetir
coincidencias.forEach(nombre => {
const li = document.createElement('li');
li.textContent = nombre;
li.style.cursor = "pointer";
li.addEventListener("click", () => {
input.value = nombre;
suggestion.innerHTML = '';
suggestionBox.style.display = 'none'
});
suggestion.appendChild(li);
});
if (coincidencias.length === 0){
suggestionBox.style.display = 'none';
console.log( "vale cero");
} else {
suggestionBox.style.display = 'block';
console.log( "block");
}
})
.catch(err => { console.error("Error cargando sugerencias:", err);
suggestionBox.style.display = 'none';
});
} else {
suggestionBox.style.display = 'none';
}
});
document.getElementById("sbm-input-ave").addEventListener("click", function (e) {
e.preventDefault();
const busqueda = document.getElementById("input-ave").value.toLowerCase();
fetch('https://corsproxy.io/?https://raw.githubusercontent.com/DiegoCMJava/aviario-cartagenero/main/aves.json') /* fetch("aves.json")*/
.then(res => res.json())
.then(data => {
const ave = data.find(ave =>
ave.nombre_comun.toLowerCase().includes(busqueda)
);
if (ave) {
document.getElementById("nombre-comun").textContent = ave.nombre_comun;
document.getElementById("nombre-cientifico").textContent = ave.nombre_cientifico;
document.getElementById("orden").textContent = ave.orden;
document.getElementById("descripcion").textContent = ave.descripcion;
document.getElementById("estado-conserv").textContent = ave.estado_conservacion;
document.getElementById("img-ave").src = ave.imagen_url;
document.getElementById("img-ave").alt = `Imagen de ${ave.nombre_comun}`;
} else {
alert("Ave no encontrada.");
}
})
.catch(err => console.error("Error al cargar el JSON:", err));
});
// cargar listado de órdenes de las aves registradas
fetch('https://corsproxy.io/?https://raw.githubusercontent.com/DiegoCMJava/aviario-cartagenero/main/aves.json') /*fetch('aves.json')*/
.then(response => {
if (!response.ok) {
throw new Error('No se pudo cargar el archivo JSON');
}
return response.json();
})
.then(aves => {
const conteoPorOrden = aves.reduce((acc, ave) => {
const orden = ave.orden;
acc[orden] = (acc[orden] || 0) + 1;
return acc;
}, {});
const ul = document.getElementById('listado-ordenes');
for (const orden in conteoPorOrden) {
const li = document.createElement('li');
li.textContent =`${orden} ( ${conteoPorOrden[orden]} )`;
ul.appendChild(li);
}
})
.catch(error => {
console.error('Error al procesar los datos de aves:', error);
});
// captura de notas sobre observación de aves
document.addEventListener("DOMContentLoaded", () => {
fetch('https://corsproxy.io/?https://raw.githubusercontent.com/DiegoCMJava/aviario-cartagenero/main/notes.json')
.then(response => {
if (!response.ok) {
throw new Error("No se pudo cargar el archivo JSON");
}
return response.json();
})
.then(data => {
const ramdonNote = data[Math.floor(Math.random() * data.length)];
document.getElementById("space-notes").textContent = ramdonNote.note;
document.getElementById("source").textContent = ramdonNote.source;
})
.catch(error => {
alert("Error al cargar el archivo JSON")
console.error("Error al cargar las notas: ", error);
})
});
/*
document.getElementById("btn-input-ave").addEventListener("click", function (e) {
e.preventDefault();
const busqueda = document.getElementById("input-ave").value.toLowerCase();
fetch("https://api.avesdecolombia.com/aves")
.then(res => res.json())
.then(data => {
const ave = data.find(ave =>
ave.nombreComun.toLowerCase().includes(busqueda)
);
if (ave) {
document.getElementById("nombre-comun").textContent = ave.nombreComun;
document.getElementById("nombre-cientifico").textContent = ave.nombreCientifico;
document.getElementById("descripcion").textContent = ave.descripcion;
document.getElementById("estado-conserv").textContent = ave.estadoConservacion;
document.getElementById("img-ave").src = ave.imagen;
document.getElementById("img-ave").alt = `Imagen de ${ave.nombreComun}`;
} else {
alert("Ave no encontrada.");
}
})
.catch(err => console.error("Error al consumir la API:", err));
});
*/