-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
134 lines (98 loc) · 4.18 KB
/
script.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
// -------------------------------
// VARIABLES PLACE
// -------------------------------
// --- DOM ELEMENTS ---
const
imageContainer = document.querySelector('.unsplash-images'),
spinner = document.querySelector('.spinner');
// --- FOR THE FETCHING TASKS ---
let
photoArray = [],
imageContainerChildElementsCount = imageContainer.childElementCount;
// => API Key
const _0x2d4316=_0x25f1;function _0x25f1(_0x3c8211,_0x51ceaf){const _0x2516f1=_0x2516();return _0x25f1=function(_0x25f17e,_0x599826){_0x25f17e=_0x25f17e-0x138;let _0x43347a=_0x2516f1[_0x25f17e];return _0x43347a;},_0x25f1(_0x3c8211,_0x51ceaf);}(function(_0x3d4d7e,_0x2993da){const _0x1a78eb=_0x25f1,_0x29bb38=_0x3d4d7e();while(!![]){try{const _0x1966da=parseInt(_0x1a78eb(0x13f))/0x1+parseInt(_0x1a78eb(0x13b))/0x2*(parseInt(_0x1a78eb(0x139))/0x3)+parseInt(_0x1a78eb(0x13d))/0x4+-parseInt(_0x1a78eb(0x140))/0x5*(-parseInt(_0x1a78eb(0x142))/0x6)+parseInt(_0x1a78eb(0x138))/0x7+parseInt(_0x1a78eb(0x141))/0x8*(-parseInt(_0x1a78eb(0x13c))/0x9)+-parseInt(_0x1a78eb(0x13a))/0xa;if(_0x1966da===_0x2993da)break;else _0x29bb38['push'](_0x29bb38['shift']());}catch(_0x1726c0){_0x29bb38['push'](_0x29bb38['shift']());}}}(_0x2516,0x30e7e));function _0x2516(){const _0x2fb723=['438996WdVfQQ','EbjZJWCeniA36buH0PyrfXl9t8pfA2Knp-JK1GeiRO4','200057UUXSWI','1137595gZzMOF','40SEcrwY','6OoTLDx','1542457CIuWnt','4083vVQgAV','7985240ztbqmk','502tUxUUn','180801XbTVwS'];_0x2516=function(){return _0x2fb723;};return _0x2516();}const apiKey=_0x2d4316(0x13e);
// --- FOR INFINITE ACTION ---
const windowHeight = window.innerHeight;
let
allNewImages = 0,
newImagesLoaded = 0,
allNewImagesAreLoaded = false;
// -------------------------------
// CALLBACK FUNCTIONS
// -------------------------------
const imageLoaded = ()=>{
newImagesLoaded++;
if(newImagesLoaded === allNewImages){
allNewImagesAreLoaded = true;
spinner.hidden = true;
imageContainerChildElementsCount = imageContainer.childElementCount;
}
}
const setAttributes = (element, attributes)=>{
for(const key in attributes){
element.setAttribute(key, attributes[key]);
}
}
const displayPhotos = async(response)=>{
photoArray = await response.json();
// => reset some variables => it`s another fetching round
newImagesLoaded = 0;
allNewImages = photoArray.length;
photoArray.forEach(photo =>{
// => create an anchor element with attributes
const anchorElement = document.createElement('a');
setAttributes(anchorElement, {
href: photo.links.html,
target: '_blank',
title: photo.links.html,
});
// => create an img
const img = document.createElement('img');
setAttributes(img, {
src: photo.urls.regular,
alt: photo.alt_description
});
// => count loaded image / control the state of the current fetching round
img.addEventListener('loaded', imageLoaded());
// => built the full anchor and put it in the dom
anchorElement.appendChild(img);
imageContainer.appendChild(anchorElement);
});
}
const throwError = (response)=>{
throw new Error(
`Failed to fetch data!
STATUSCODE : ${response.status}
STATUSTEXT: ${response.statusText}
`);
}
// -------------------------------
// Fetching Zone
// -------------------------------
const getPhotos = async()=>{
let apiUrl =
imageContainerChildElementsCount < 1
? `https://api.unsplash.com/photos/random?client_id=${apiKey}&count=5`
: `https://api.unsplash.com/photos/random?client_id=${apiKey}&count=20`;
try{
const response = await fetch(apiUrl);
response.ok
? displayPhotos(response)
: throwError(response);
}
catch(err){
imageContainer.innerHTML = `
<p>${err}</p>
`
}
}
getPhotos();
// -------------------------------
// Events
// -------------------------------
window.addEventListener('scroll', ()=>{
if(windowHeight + window.scrollY >= document.body.offsetHeight - windowHeight * 0.5 && allNewImagesAreLoaded){
allNewImagesAreLoaded = false;
getPhotos();
}
})