-
Notifications
You must be signed in to change notification settings - Fork 0
/
genus.js
executable file
·144 lines (126 loc) · 5.27 KB
/
genus.js
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
if ('IntersectionObserver' in window) {
async function init() {
const url = 'species.json';
const res = await fetch(url);
data = await res.json();
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
if (!data) {
return;
}
const div = entry.target;
//div.removeAttribute('height');
div.style.height = 'unset';
const id = div.id;
let [order, family, genus] = id.split(' ');
let species = data[order][family][genus];
//console.log(species);
for (const name in species) {
//console.log(name);
const record = species[name];
const canonicalName = record['la'];
const error = record['e'];
const width = record['w'];
const height = record['h'];
const e = record['en'];
const d = record['da'];
let imgFileName = 'error.png';
if (!error) {
imgFileName = `species/${canonicalName}.jpg`;
}
div.insertAdjacentHTML('beforeend', `
<figure>
<a href='http://www.google.com/search?q=${canonicalName}' target='_blank'>
<img src='${imgFileName}' width=${width} height=${height} loading='lazy'>
</a>
<figcaption>
<a href='https://en.wikipedia.org/wiki/${canonicalName}' target='_blank'>
<span class='la'>${canonicalName}</span>
</a>
<br>
<span class='en'>${e}</span><br>
<span class='da'>${d}</span>
</figcaption>
</figure>
`);
}
imageObserver.unobserve(div);
}
});
},
{
rootMargin: "100% 0px 100% 0px"
}
);
const divs = Array.from(document.querySelectorAll('.genus'));
divs.forEach(div => imageObserver.observe(div));
}
init();
}
function clear_filters() {
for (const f of ['','la','en','da']) {
document.getElementById(`filter ${f}`).value = '';
}
const results = document.getElementById('results');
results.innerHTML = '';
}
async function filter_figures(lang = '') {
const results = document.getElementById('results');
results.innerHTML = '';
const input = document.getElementById(`filter ${lang}`).value;
//console.log(input);
if (input == '') {
return;
}
const re = new RegExp(input, 'i');
if (lang) {
lang = [lang];
} else {
lang = ['la', 'en', 'da'];
}
for (const order in data) {
for (const family in data[order]) {
for (const genus in data[order][family]) {
for (const species in data[order][family][genus]) {
const record = data[order][family][genus][species];
for (const l of lang) {
let str = record[l];
if (!str) {
str = '';
}
//console.log(str);
if (str.match(re)) {
const canonicalName = record['la'];
const error = record['e'];
const width = record['w'];
const height = record['h'];
const e = record['en'];
const d = record['da'];
let imgFileName = 'error.png';
if (!error) {
imgFileName = `species/${canonicalName}.jpg`;
}
results.insertAdjacentHTML('beforeend', `
<figure>
<a href='http://www.google.com/search?q=${canonicalName}' target='_blank'>
<img src='${imgFileName}' width=${width} height=${height} loading='lazy'>
</a>
<figcaption>
<a href='https://en.wikipedia.org/wiki/${canonicalName}' target='_blank'>
<span class='la'>${canonicalName}</span>
</a>
<br>
<span class='en'>${e}</span><br>
<span class='da'>${d}</span>
</figcaption>
</figure>
`);
break;
}
}
}
}
}
}
}