-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (128 loc) · 4.8 KB
/
Copy pathscript.js
File metadata and controls
147 lines (128 loc) · 4.8 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
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
const imagesContainer = document.querySelector('.gallery');
const modal = document.querySelector('.modal');
const form = document.querySelector('form');
const searchInput = document.querySelector('.search');
const loadMoreBtn = document.querySelector('.load-more');
let currentPage = 1;
const APIKEY = 'BG4YID4S5cqAl5olIIXVh44pmPwAk0r4kJdaiw5QXiCBb3SpWcM7KHzF';
const APIURL = `https://api.pexels.com/v1/curated?page=${currentPage}&per_page=15`;
// const SEARCHURL = 'https://api.pexels.com/v1/search?query=nature&per_page=15'
let searchValue = null;
fetchAPI(APIURL);
loadMoreBtn.addEventListener('click', () => {
currentPage++;
let APIURL = null;
if(searchValue) {
APIURL = `https://api.pexels.com/v1/search?query=${searchValue}&page=${currentPage}&per_page=15`;
} else {
APIURL = `https://api.pexels.com/v1/curated?page=${currentPage}&per_page=15`;
}
fetchAPI(APIURL, false);
});
function fetchAPI(url,empty=true) {
if(empty === true) {
imagesContainer.innerHTML = '';
}
loadMoreBtn.innerHTML = '';
const timer = setInterval(setLoading, 500);
loadMoreBtn.disabled = true;
fetch(url, {
headers: {Authorization: APIKEY}
}).then(res => res.json())
.then(resData => {
console.log(resData);
fetchPhotos(resData.photos);
clearInterval(timer);
loadMoreBtn.innerHTML = 'load more';
loadMoreBtn.disabled = false;
}).catch(err => {
alert(err);
clearInterval(timer);
loadMoreBtn.innerHTML = 'load more';
loadMoreBtn.disabled = false;
fetchAPI(APIURL);
});
}
form.addEventListener('submit', (e) => {
e.preventDefault();
if(searchInput.value.trim() != '') {
fetchAPI(`https://api.pexels.com/v1/search?query=${searchInput.value}&page=${currentPage}&per_page=15`)
searchValue = searchInput.value;
searchInput.value = '';
console.log(searchValue);
}
});
function fetchPhotos(photos) {
photos.forEach(photo => {
const {photographer, src} = photo;
const cardEl = document.createElement('div');
cardEl.className = 'card';
cardEl.innerHTML = `
<img src="${src.portrait}" loading="lazy">
<div class="details">
<div class="photographer">
<div><i class="fas fa-camera"></i></div>
<p>${photographer}</p>
</div>
<button class="download icon">
<i class="fas fa-cloud-download"></i>
</button>
</div>
`
const cardDownloadEl = cardEl.querySelector('.download');
const cardimage = cardEl.querySelector('img').src;
cardDownloadEl.addEventListener('click', () => {
downloadImage(cardimage);
});
imagesContainer.appendChild(cardEl);
cardEl.querySelector('img').addEventListener('click', () => {
modal.classList.add('active');
modal.innerHTML = `
<div class="modal-close-overlay"></div>
<div class="modal-content">
<div class="modal-header">
<div class="photographer">
<div><i class="fas fa-camera"></i></div>
<p>${[photographer]}</p>
</div>
<div class="download-close">
<button class="download icon">
<i class="fas fa-cloud-download"></i>
</button>
<button class="modal-close icon">
<i class="fas fa-times"></i>
</button>
</div>
</div>
<img src="${src.portrait}" loading="lazy">
</div>
`
modal.querySelector('.modal-close-overlay').addEventListener('click', closeModal);
modal.querySelector('.modal-close').addEventListener('click', closeModal);
const downloadEl = modal.querySelector('.download');
const image = modal.querySelector('img').src;
downloadEl.addEventListener('click', () => {
downloadImage(image);
})
function closeModal() {
modal.classList.remove('active');
}
})
})
}
function setLoading() {
loadMoreBtn.innerHTML += '.';
if(loadMoreBtn.innerHTML.length > 3) {
loadMoreBtn.innerHTML = '.';
}
}
function downloadImage(image) {
fetch(image)
.then(res => res.blob())
.then(file => {
const a = document.createElement('a');
a.href = URL.createObjectURL(file);
a.download = `image.${file.type.slice(file.type.indexOf('/') + 1)}`;
a.click();
})
}