-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscriptAstroUNMIN.js
223 lines (192 loc) · 7.03 KB
/
scriptAstroUNMIN.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
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
"use strict";
//import data from "./astroData.json" assert { type: "json" }; Do not work in FF
fetch("./astroData.json")
.then((res) => res.json())
.then((data) => {
// do stuff with the data
data.astroObject = data.astroObject.sort((a, b) => {
if (a.distance < b.distance) {
return -1;
}
});
const numbOfObjects = Object.keys(data.astroObject).length;
const selectNumbOfObjectsElement = document.querySelector("#num-obj");
/* console.log(selectNumbOfObjectsElement); */
const numP = document.createElement("p");
numP.textContent = `Number of astronomical objects: ${numbOfObjects}`;
selectNumbOfObjectsElement.appendChild(numP);
/* main = document.querySelector("main");
myH2 = document.createElement("h2");
mySection = document.createElement("section");
myH2.textContent = astroData;
main.appendChild(myH2);
main.appendChild(mySection); */
const selectElementType = document.querySelector("#astro-type");
const selectElementConst = document.querySelector("#astro-const");
const selectElementDist = document.querySelector("#astro-dist");
let main = null;
let myH2 = null;
let mySection = null;
let selectedOption = "";
selectElementType.addEventListener("change", (event) => {
resetSelectElement(selectElementConst);
resetSelectElement(selectElementDist);
clearMainAndSelectOption();
populateType(selectedOption);
lazyLoadFunc();
});
selectElementConst.addEventListener("change", (event) => {
resetSelectElement(selectElementType);
resetSelectElement(selectElementDist);
clearMainAndSelectOption();
populateConst(selectedOption);
lazyLoadFunc();
});
selectElementDist.addEventListener("change", (event) => {
resetSelectElement(selectElementType);
resetSelectElement(selectElementConst);
clearMainAndSelectOption();
populateDist(selectedOption);
lazyLoadFunc();
});
function populateType(astroData) {
createH2inMain(astroData);
Object.entries(data.astroObject).forEach(([key, value]) => {
const words = value.type.split(" ");
if (words[1].toLowerCase() === astroData.toLowerCase()) {
createAstroElement(value);
} else if (value.type === astroData) {
createAstroElement(value);
} else if (astroData === "All objects") {
createAstroElement(value);
}
});
}
function populateConst(astroData) {
createH2inMain(astroData);
Object.entries(data.astroObject).forEach(([key, value]) => {
if (value.constellations === astroData) {
createAstroElement(value);
}
});
}
function populateDist(astroData) {
createH2inMainDistance(astroData);
Object.entries(data.astroObject).forEach(([key, value]) => {
let astroDataNumb = Number(astroData);
if (
value.distance >= Number(astroDataNumb) &&
value.distance <= Number(astroDataNumb) * 10
) {
createAstroElement(value);
}
});
}
function clearMainAndSelectOption() {
let node = document.querySelector("main");
node.querySelectorAll("*").forEach((n) => n.remove());
selectedOption = event.target.value;
}
function createH2inMain(astroData) {
main = document.querySelector("main");
myH2 = document.createElement("h2");
mySection = document.createElement("section");
myH2.textContent = astroData;
main.appendChild(myH2);
main.appendChild(mySection);
}
function createH2inMainDistance(astroData) {
main = document.querySelector("main");
myH2 = document.createElement("h2");
mySection = document.createElement("section");
myH2.textContent =
numberWithCommas(astroData) +
` ly to ` +
numberWithCommas(astroData * 10) +
` ly`;
main.appendChild(myH2);
main.appendChild(mySection);
}
function createAstroElement(value) {
const myFigure = document.createElement("figure");
const myImg = document.createElement("img");
const myFigcaption = document.createElement("figcaption");
const myA = document.createElement("a");
//myImg.src = `${value.url}`;
const imgValue = `${value.url}`;
const attr = document.createAttribute("data-src");
attr.value = imgValue;
myImg.setAttributeNode(attr);
//myImg.loading = `lazy`;
myImg.classList.add("lazy");
myImg.alt = `${value.id} ${value.name}`;
myFigcaption.textContent = ` ${value.type} in ${
value.constellations
} at ${numberWithCommas(value.distance)} ly.`;
/* myA.title = `test`; */
myA.href = `${value.link}`;
myA.target = "_blank";
const linkText = document.createTextNode(`${value.id} ${value.name}.`);
mySection.appendChild(myFigure);
myFigure.appendChild(myImg);
myFigure.appendChild(myA);
myFigure.appendChild(myFigcaption);
myA.appendChild(linkText);
}
function resetSelectElement(selectElement) {
selectElement.selectedIndex = 0; //
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function lazyLoadFunc() {
// document.addEventListener("DOMContentLoaded", function () {
var lazyloadImages;
if ("IntersectionObserver" in window) {
lazyloadImages = document.querySelectorAll(".lazy");
var imageObserver = new IntersectionObserver(function (
entries,
observer
) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
var image = entry.target;
image.src = image.dataset.src;
image.classList.remove("lazy");
imageObserver.unobserve(image);
}
});
});
lazyloadImages.forEach(function (image) {
imageObserver.observe(image);
});
} else {
var lazyloadThrottleTimeout;
lazyloadImages = document.querySelectorAll(".lazy");
function lazyload() {
if (lazyloadThrottleTimeout) {
clearTimeout(lazyloadThrottleTimeout);
}
lazyloadThrottleTimeout = setTimeout(function () {
var scrollTop = window.pageYOffset;
lazyloadImages.forEach(function (img) {
if (img.offsetTop < window.innerHeight + scrollTop) {
img.src = img.dataset.src;
img.classList.remove("lazy");
}
});
if (lazyloadImages.length == 0) {
document.removeEventListener("scroll", lazyload);
window.removeEventListener("resize", lazyload);
window.removeEventListener("orientationChange", lazyload);
}
}, 20);
}
document.addEventListener("scroll", lazyload);
window.addEventListener("resize", lazyload);
window.addEventListener("orientationChange", lazyload);
}
//});
}
});
/* START lazy loading */