-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
193 lines (157 loc) · 6.33 KB
/
app.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
const imageBox = document.querySelector(".images");
const loadmoreBtn = document.querySelector("#btn");
const searchInput = document.querySelector("#searchInput");
const searchBtn = document.querySelector(".searchBtn");
const lightBox = document.querySelector(".light-box");
const closeBtn = lightBox.querySelector(".fa-xmark");
const downloadBtn = lightBox.querySelector(".fa-download");
const api = document.querySelector("#api");
const pexelApiKey = "Su3lhzflU6mYsBsVqqfdXFP7kuoQaMGpH7PrJVYOzx8wLmQlUjOYf8VF";
const splashApiKey ="2BsfBnNAfcAGF3oX4F_fRIlYnOXYBGYyJpeHfo8AWp4";
const perPage = 20;
let currentPageP = 1;
let currentPageS = 1;
let searchTerm = null;
const downloadImg = (url)=>{
fetch(url).then(res=>
res.blob())
.then(file=>{
let aTag = document.createElement("a");
aTag.href = URL.createObjectURL(file);
aTag.download = new Date().getTime()
aTag.click();
})
.catch(()=>
alert("Failed to Download Image!!!")
)
}
const showLightBox = (photographer, imgURL) =>{
lightBox.querySelector("img").src = imgURL;
lightBox.querySelector("span").textContent = photographer;
downloadBtn.setAttribute("data-img", imgURL);
lightBox.style.display = "block";
}
const generateHTMLPexels = (images) =>{
imageBox.innerHTML += images.map(image =>
`<li class="card">
<img src="${image.src.large2x}" alt="" onclick="showLightBox('${image.photographer}', '${image.src.large2x}')">
<div class="details">
<div class="photographer">
<i class="fa-solid fa-camera"></i>
<span>${image.photographer}</span>
</div>
<button onclick="downloadImg('${image.src.large2x}'); event.stopPropagation();">
<i class="fa-solid fa-download" style="color: #000;"></i>
</button>
</div>
</li>`
).join("");
}
const generateHTMLSplash = (images) =>{
imageBox.innerHTML += images.map(image =>
`<li class="card">
<img src="${image.urls.regular}" alt="" onclick="showLightBox('${image.user.name}', '${image.urls.regular}')">
<div class="details">
<div class="photographer">
<i class="fa-solid fa-camera"></i>
<span>${image.user.name}</span>
</div>
<button onclick="downloadImg('${image.urls.regular}'); event.stopPropagation();">
<i class="fa-solid fa-download" style="color: #000;"></i>
</button>
</div>
</li>`
).join("");
}
async function getImagesPexels(apiURL){
loadmoreBtn.textContent = "Loading...";
loadmoreBtn.classList.add("disabled");
await fetch(apiURL, {
headers: {Authorization: pexelApiKey}
}).then(res => res.json()).then(data => {
generateHTMLPexels(data.photos);
loadmoreBtn.textContent = "Load More";
loadmoreBtn.classList.remove("disabled");
}).catch(()=>
alert("Failed to load images!!!")
)
}
async function getImagesSplash(apiURL){
loadmoreBtn.textContent = "Loading...";
loadmoreBtn.classList.add("disabled");
await fetch(apiURL).then(res => res.json()).then(data => {
generateHTMLSplash(data);
loadmoreBtn.textContent = "Load More";
loadmoreBtn.classList.remove("disabled");
}).catch(()=>
alert("Failed to load images!!!")
)
}
async function getImagesSplashsearch(apiURL){
loadmoreBtn.textContent = "Loading...";
loadmoreBtn.classList.add("disabled");
await fetch(apiURL).then(res => res.json()).then(data => {
generateHTMLSplash(data.results);
console.log(data)
loadmoreBtn.textContent = "Load More";
loadmoreBtn.classList.remove("disabled");
}).catch(()=>
alert("Failed to load images!!!")
)
}
const loadmoreImages = () =>{
if(api.value === "pexels"){
currentPageP++;
let apiURL = `https://api.pexels.com/v1/curated?page=${currentPageP}&per_page=${perPage}`;
apiURL = searchTerm ? `https://api.pexels.com/v1/search?query=${searchTerm}&page=${currentPageP}&per_page=${perPage}` : apiURL;
getImagesPexels(apiURL);
}
else{
currentPageS++;
let apiURL = `https://api.unsplash.com/photos?page=${currentPageS}&per_page=${perPage}&client_id=${splashApiKey}`;
apiURL = searchTerm ? `https://api.unsplash.com/search/photos?page=${currentPageS}&query=${searchTerm}&per_page=${perPage}&client_id=${splashApiKey}` : apiURL;
if(searchTerm != null){
getImagesSplashsearch(apiURL);
}
else{getImagesSplash(apiURL);}
}
}
const loadSearchImages = () =>{
if(searchInput.value === "") return searchTerm = null;
if(api.value === "pexels"){
currentPageP = 1;
searchTerm = searchInput.value;
imageBox.innerHTML = "";
getImagesPexels(`https://api.pexels.com/v1/search?query=${searchTerm}&page=${currentPageP}&per_page=${perPage}`)
}
else if(api.value === "splash"){
currentPageS = 1;
searchTerm = searchInput.value;
imageBox.innerHTML = "";
getImagesSplashsearch(`https://api.unsplash.com/search/photos?page=${currentPageS}&query=${searchTerm}&per_page=${perPage}&client_id=${splashApiKey}`)
}
}
getImagesPexels(`https://api.pexels.com/v1/curated?page=${currentPageP}&per_page=${perPage}`);
api.addEventListener("change", ()=>{
if(api.value == 'splash'){
imageBox.innerHTML = "";
searchInput.value = "";
searchInput.placeholder = "Search images in Splash.com";
getImagesSplash(`https://api.unsplash.com/photos?page=${currentPageS}&per_page=${perPage}&client_id=${splashApiKey}`)
}
else{
imageBox.innerHTML = "";
searchInput.value = "";
searchInput.placeholder = "Search images in Pexels.com";
getImagesPexels(`https://api.pexels.com/v1/curated?page=${currentPageP}&per_page=${perPage}`);
}
})
loadmoreBtn.addEventListener("click", (e)=>{
e.preventDefault()
loadmoreImages();
});
searchBtn.addEventListener("click", loadSearchImages);
closeBtn.addEventListener("click", ()=>{
lightBox.style.display = 'none';
})
downloadBtn.addEventListener("click", (e) => downloadImg(e.target.dataset.img));