Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (91 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
123 lines (91 loc) · 2.76 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
let wallpapers = [];
const gallery = document.getElementById('gallery');
const themeToggle = document.getElementById('themeToggle');
const themeLabel = document.getElementById('themeLabel');
const modal = document.getElementById('modal');
const modalImage = document.getElementById('modalImage');
const modalDownload = document.getElementById('modalDownload');
const closeModal = document.getElementById('closeModal');
let theme = 'Light';
function fileName(name) {
return `${name}.png`;
}
function rawUrl(folder, name) {
return `${folder}/${encodeURIComponent(fileName(name))}`;
}
function buildCard(folder, name) {
const card = document.createElement('div');
card.className = 'card';
const img = document.createElement('img');
img.className = 'thumb';
img.src = rawUrl(folder, name);
img.alt = name;
img.loading = 'lazy';
img.addEventListener('click', () => openModal(folder, name));
card.appendChild(img);
const meta = document.createElement('div');
meta.className = 'meta';
const title = document.createElement('div');
title.className = 'name';
title.textContent = name;
const actions = document.createElement('div');
actions.className = 'actions';
const dl = document.createElement('a');
dl.href = rawUrl(folder, name);
dl.download = fileName(name);
dl.textContent = 'Download';
dl.setAttribute('aria-label', `Download ${name}`);
actions.appendChild(dl);
meta.appendChild(title);
meta.appendChild(actions);
card.appendChild(meta);
return card;
}
function render() {
gallery.innerHTML = '';
const folder = theme === 'Light' ? 'Light' : 'Dark';
wallpapers.forEach(name => {
gallery.appendChild(buildCard(folder, name));
});
}
themeToggle.addEventListener('change', (e) => {
theme = e.target.checked ? 'Dark' : 'Light';
document.body.classList.toggle(
'theme-dark',
theme === 'Dark'
);
themeLabel.textContent = theme;
render();
});
function openModal(folder, name) {
modalImage.src = rawUrl(folder, name);
modalDownload.href = rawUrl(folder, name);
modalDownload.download = fileName(name);
modalDownload.textContent = 'Download';
modal.setAttribute('aria-hidden', 'false');
}
function closeModalFn() {
modal.setAttribute('aria-hidden', 'true');
modalImage.src = '';
}
closeModal.addEventListener('click', closeModalFn);
modal.addEventListener('click', (e) => {
if (e.target === modal) {
closeModalFn();
}
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeModalFn();
}
});
fetch("wallpapers.json")
.then(response => response.json())
.then(data => {
wallpapers = data;
render();
})
.catch(error => {
console.error("Failed to load wallpapers.json:", error);
gallery.innerHTML = "<p>Failed to load wallpapers.</p>";
});