-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
301 lines (248 loc) · 9.76 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
let wallpapers = [];
let currentPage = 1;
let wallpapersPerPage = calculateWallpapersPerPage();
let slideshowInterval = null;
let currentSearchResults = null;
let cache = {
data: null,
timestamp: null,
cacheDuration: 60 * 60 * 1000,
};
document.getElementById('currentYear').textContent = new Date().getFullYear();
function calculateWallpapersPerPage() {
const screenWidth = window.innerWidth;
const baseWidth = 1920;
const baseCount = 12;
const minCount = 5;
const ratio = screenWidth / baseWidth;
if (ratio < 1) {
return Math.max(minCount, Math.floor(baseCount * ratio));
} else {
const imageWidth = 300;
const gap = 30;
const availableWidth = screenWidth - (2 * gap);
const imagesPerRow = Math.floor(availableWidth / (imageWidth + gap));
const rows = Math.floor(window.innerHeight / (imageWidth + gap));
const totalImages = imagesPerRow * rows;
if (Math.abs(screenWidth - baseWidth) < 10) {
return baseCount;
}
return Math.max(minCount, totalImages);
}
}
window.addEventListener('resize', () => {
wallpapersPerPage = calculateWallpapersPerPage();
displayWallpapers(getPaginatedWallpapers(currentPage, currentSearchResults || wallpapers));
updatePagination(currentSearchResults || wallpapers);
});
async function loadWallpapers() {
const repoUrl = 'https://api.github.com/repos/TeenAgeTechBD/wallpapers/contents/wallpapers';
if (cache.data && Date.now() - cache.timestamp < cache.cacheDuration) {
wallpapers = cache.data;
displayWallpapers(getPaginatedWallpapers(currentPage));
updatePagination();
return;
}
try {
const response = await fetch(repoUrl);
if (!response.ok) throw new Error('Failed to fetch wallpapers');
const files = await response.json();
wallpapers = files.filter(file =>
file.name.endsWith('.jpg') || file.name.endsWith('.jpeg') || file.name.endsWith('.png') || file.name.endsWith('.gif')
);
cache.data = wallpapers;
cache.timestamp = Date.now();
shuffleArray(wallpapers);
displayWallpapers(getPaginatedWallpapers(currentPage));
updatePagination();
} catch (error) {
console.error('Error:', error);
document.getElementById('gallery').innerHTML = '<p style="color:white;">Failed to load wallpapers.</p>';
}
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function displayWallpapers(files) {
const gallery = document.getElementById('gallery');
gallery.innerHTML = '';
if (files.length === 0) {
gallery.innerHTML = '<p style="color:white;">No wallpapers found.</p>';
return;
}
files.forEach(file => {
const imgElement = document.createElement('img');
imgElement.src = file.download_url;
imgElement.alt = file.name;
imgElement.loading = 'lazy';
imgElement.onclick = () => {
openFullscreen(file.download_url);
};
const fullscreenButton = document.createElement('button');
fullscreenButton.classList.add('fullscreen-button');
fullscreenButton.textContent = '⛶';
fullscreenButton.onclick = (event) => {
event.stopPropagation();
openFullscreen(file.download_url);
};
const div = document.createElement('div');
div.classList.add('wallpaper');
div.appendChild(imgElement);
div.appendChild(fullscreenButton);
gallery.appendChild(div);
});
}
function openFullscreen(url) {
const fullscreenContainer = document.getElementById('fullscreen-container');
const imgElement = document.getElementById('fullscreen-image');
const closeButton = document.getElementById('closeButton');
const downloadButton = document.getElementById('downloadBtn');
imgElement.src = url;
fetch(url)
.then(response => response.blob())
.then(blob => {
const blobUrl = URL.createObjectURL(blob);
downloadButton.href = blobUrl;
const filename = url.split('/').pop();
downloadButton.download = filename;
})
.catch(error => console.error('Error fetching the image:', error));
fullscreenContainer.style.display = 'block';
fullscreenContainer.requestFullscreen().catch(err => {
console.error('Error attempting to enable fullscreen mode:', err);
});
}
function closeFullscreen() {
const fullscreenContainer = document.getElementById('fullscreen-container');
fullscreenContainer.style.display = 'none';
document.exitFullscreen();
displayWallpapers(getPaginatedWallpapers(currentPage, currentSearchResults || wallpapers));
updatePagination(currentSearchResults || wallpapers);
}
function searchWallpapers() {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
currentSearchResults = wallpapers.filter(file => file.name.toLowerCase().includes(searchTerm));
currentPage = 1;
displayWallpapers(getPaginatedWallpapers(currentPage, currentSearchResults));
updatePagination(currentSearchResults);
}
function clearSearch() {
document.getElementById('searchInput').value = '';
currentSearchResults = null;
currentPage = 1;
displayWallpapers(getPaginatedWallpapers(currentPage));
updatePagination();
}
document.getElementById('searchInput').addEventListener('input', function() {
const clearButton = document.getElementById('clearButton');
if (this.value.length > 0) {
clearButton.style.display = 'block';
} else {
clearButton.style.display = 'none';
}
});
document.getElementById('clearButton').addEventListener('click', function() {
const searchInput = document.getElementById('searchInput');
searchInput.value = '';
searchInput.dispatchEvent(new Event('input'));
});
function getPaginatedWallpapers(page, data = wallpapers) {
const startIndex = (page - 1) * wallpapersPerPage;
const endIndex = startIndex + wallpapersPerPage;
return data.slice(startIndex, endIndex);
}
function updatePagination(data = wallpapers) {
const totalPages = Math.ceil(data.length / wallpapersPerPage);
const pageInfo = document.getElementById('pageInfo');
const prevButton = document.getElementById('prevButton');
const nextButton = document.getElementById('nextButton');
pageInfo.textContent = `Page ${currentPage} of ${totalPages}`;
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === totalPages;
}
function startSlideshow() {
if (slideshowInterval) {
clearInterval(slideshowInterval);
slideshowInterval = null;
document.getElementById('slideshowButton').textContent = 'Slideshow';
document.exitFullscreen();
document.getElementById('slideshow-container').style.display = 'none';
document.body.style.cursor = 'auto';
return;
}
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen().catch(err => {
console.error('Error attempting to enable fullscreen mode:', err);
});
}
let currentIndex = 0;
const slideshowContainer = document.getElementById('slideshow-container');
const imgElement = document.getElementById('slideshow-image');
const closeButton = document.getElementById('closeSlideshowButton');
slideshowContainer.style.display = 'block';
const loadNextWallpaper = () => {
if (currentIndex >= wallpapers.length) {
currentIndex = 0;
}
const wallpaper = wallpapers[currentIndex];
imgElement.classList.remove('fade-in');
imgElement.src = wallpaper.download_url;
imgElement.alt = wallpaper.name;
currentIndex++;
setTimeout(() => {
imgElement.classList.add('fade-in');
}, 50);
};
loadNextWallpaper();
slideshowInterval = setInterval(() => {
imgElement.classList.remove('fade-in');
setTimeout(() => {
loadNextWallpaper();
}, 950);
}, 6000);
document.body.style.cursor = 'none';
imgElement.addEventListener('click', () => {
window.open(imgElement.src, '_blank');
});
setTimeout(() => {
popup.style.display = 'none';
}, 2000);
document.getElementById('slideshowButton').textContent = 'Stop Slideshow';
}
function stopSlideshow() {
clearInterval(slideshowInterval);
slideshowInterval = null;
document.getElementById('slideshowButton').textContent = 'Slideshow';
document.exitFullscreen();
document.getElementById('slideshow-container').style.display = 'none';
document.body.style.cursor = 'auto';
if (document.fullscreenElement) {
document.exitFullscreen().catch(err => {
console.error('Error attempting to exit fullscreen mode:', err);
});
}
}
document.getElementById('searchInput').addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
searchWallpapers();
}
});
document.getElementById('prevButton').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
displayWallpapers(getPaginatedWallpapers(currentPage, currentSearchResults || wallpapers));
updatePagination(currentSearchResults || wallpapers);
}
});
document.getElementById('nextButton').addEventListener('click', () => {
const totalPages = Math.ceil((currentSearchResults || wallpapers).length / wallpapersPerPage);
if (currentPage < totalPages) {
currentPage++;
displayWallpapers(getPaginatedWallpapers(currentPage, currentSearchResults || wallpapers));
updatePagination(currentSearchResults || wallpapers);
}
});
loadWallpapers();