-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (102 loc) · 2.63 KB
/
script.js
File metadata and controls
122 lines (102 loc) · 2.63 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
// DOM ELEMENTS
const elements = {
imageContainer: document.getElementById('image-container'),
loader: document.getElementById('loader'),
queryInput: document.getElementById('query'),
};
let photosArr = [];
let ready = false; // all pics loaded
let imagesLoaded = 0;
let totalImages = 0;
// In phone first load is 5 pics, otherwise 15
const initialCount = window.innerWidth > 500 ? 15 : 5;
const regularCount = 30;
let isFirstLoad = true;
/*********
* HELPERS
*/
// Set Attributes on DOM Elements
function setAttributes(element, attributes) {
for (const key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
// Check if all images were loaded
function imageLoaded() {
imagesLoaded++;
if (imagesLoaded === totalImages) {
isFirstLoad = false;
ready = true;
// Hide the Loader
elements.loader.hidden = true;
}
}
/***************
* UNSPLASH API
*/
const apiKey = 'VVm7jT9quhW1_3g6v5GXhxzj2IW28dysBnw_Rrh1HXk';
async function getPhotos(query = 'nature') {
let count;
if (isFirstLoad) {
count = initialCount;
} else {
count = regularCount;
}
const apiUrl = `https://api.unsplash.com/photos/random/?client_id=${apiKey}&count=${count}&query=${query}`;
try {
const response = await fetch(apiUrl);
photosArr = await response.json();
displayPhotos();
} catch (error) {
console.log(error);
}
}
function displayPhotos() {
// Reset number of images loaded
imagesLoaded = 0;
// Update Total Nº of Images
totalImages = photosArr.length;
photosArr.forEach((photo) => {
// Create <a> that links to Unsplash
const item = document.createElement('a');
setAttributes(item, {
href: photo.links.html,
target: '_blank',
});
// Create <img> for photo
const img = document.createElement('img');
setAttributes(img, {
src: photo.urls.regular,
alt: photo.alt_description || 'unknown',
title: photo.alt_description || 'unknown',
});
// Put <img> inside of <a> and place them inside imageContainer
item.appendChild(img);
elements.imageContainer.appendChild(item);
// Image was loaded
imageLoaded();
});
}
/*********
* EVENT LISTENERS
*/
window.addEventListener('scroll', () => {
if (
window.innerHeight + window.scrollY >= document.body.offsetHeight - 1000 &&
ready === true
) {
getPhotos();
ready = false;
}
});
elements.queryInput.addEventListener('keypress', (ev) => {
if (ev.keyCode === 13) {
elements.loader.hidden = false;
query = elements.queryInput.value;
totalImages = 0;
elements.imageContainer.innerHTML = '';
getPhotos(query);
}
});
// On Load
getPhotos();