-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
173 lines (150 loc) · 5.34 KB
/
Copy pathscript.js
File metadata and controls
173 lines (150 loc) · 5.34 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
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
const IMAGES = [
{ title: "Amsel", src: "./assets/images/img_amsel.jpg" },
{ title: "Blaumeise", src: "./assets/images/img_blaumeise.jpg" },
{ title: "Buchfink", src: "./assets/images/img_buchfink.jpg" },
{ title: "Gartenbaumläufer", src: "./assets/images/img_gartenbaumlaeufer.jpg" },
{ title: "Gartenrotschwanz", src: "./assets/images/img_gartenrotschwanz.jpg" },
{ title: "Grünfink", src: "./assets/images/img_gruenfink.jpg" },
{ title: "Haussperling", src: "./assets/images/img_haussperling.jpg" },
{ title: "Kleiber", src: "./assets/images/img_kleiber.jpg" },
{ title: "Kohlmeise", src: "./assets/images/img_kohlmeise.jpg" },
{ title: "Mönchgrasmücke", src: "./assets/images/img_moenchgrasmuecke.jpg" },
{ title: "Nachtigall", src: "./assets/images/img_nachtigall.jpg" },
{ title: "Nebelkrähe", src: "./assets/images/img_nebelkraehe.jpg" },
{ title: "Ringeltaube", src: "./assets/images/img_ringeltaube.jpg" },
{ title: "Rotkelchen", src: "./assets/images/img_rotkelchen.jpg" },
{ title: "Schwanzmeise", src: "./assets/images/img_schwanzmeise.jpg" },
{ title: "Singdrossel", src: "./assets/images/img_singdrossel.jpg" },
{ title: "Stieglitz", src: "./assets/images/img_stieglitz.jpg" },
{ title: "Turmfalke", src: "./assets/images/img_turmfalke.jpg" },
{ title: "Zilpzalp", src: "./assets/images/img_zilpzalp.jpg" }
];
const GRID = document.getElementById("gallery-grid");
const DIRECTION_NEXT = "next";
const DIRECTION_PREV = "prev";
let currentIndex = 0;
/** Sets the title inside the dialog. */
function setImageTitle() {
const dialogTitle = document.getElementById("dialog-img-title");
dialogTitle.textContent = IMAGES[currentIndex].title;
}
/** Sets the image inside the dialog. */
function setDialogImage() {
const dialogImage = document.getElementById("dialog-img");
dialogImage.src = IMAGES[currentIndex].src;
dialogImage.alt = `Ausgewähltes Vogelbild: ${IMAGES[currentIndex].title}`;
}
/** Sets the "current image / total" indicator. */
function setStepIndicator() {
const currentStep = document.getElementById("currentStep");
const totalSteps = document.getElementById("totalSteps");
currentStep.textContent = String(currentIndex + 1);
totalSteps.textContent = String(IMAGES.length);
}
/** Updates dialog content. */
function updateDialog() {
setImageTitle();
setDialogImage();
setStepIndicator();
}
/** Renders a single gallery preview image. */
function renderGridImage(image, index) {
return `
<button class="gallery-btn" data-index="${index}" type="button" aria-label="${image.title} vergrößern">
<img
class="grid-img"
src="${image.src}"
alt="${image.title}">
</button>
`;
}
/** Builds all preview images into the gallery. */
function buildGallery() {
IMAGES.forEach((image, index) => {
GRID.innerHTML += renderGridImage(image, index);
});
}
/** Opens the dialog for a specific image. */
function openDialog(index) {
const dialog = document.getElementById("dialog");
currentIndex = index;
updateDialog();
dialog.showModal();
document.getElementById("dialog-btn-close").focus();
}
/** Handles click events inside the gallery. */
function onGridClick(event) {
const btn = event.target.closest(".gallery-btn");
if (!btn) return;
const index = Number.parseInt(btn.dataset.index, 10);
openDialog(index);
}
/** Updates the current index based on the given direction. */
function updateCurrentIndex(direction) {
if (direction === "next") {
if (currentIndex === IMAGES.length - 1) {
currentIndex = 0;
} else {
currentIndex++;
}
} else if (direction === "prev") {
if (currentIndex === 0) {
currentIndex = IMAGES.length - 1;
} else {
currentIndex--;
}
}
}
/** Shows the previous image. */
function showPreviousImage() {
updateCurrentIndex(DIRECTION_PREV);
updateDialog();
}
/** Shows the next image. */
function showNextImage() {
updateCurrentIndex(DIRECTION_NEXT);
updateDialog();
}
/** Closes the dialog. */
function closeDialog() {
const dialog = document.getElementById("dialog");
dialog.close();
}
/** Keeps the keyboard focus inside the open dialog. */
function trapFocus(dialog, event) {
if (event.key !== "Tab") return;
const focusable = dialog.querySelectorAll("button");
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
}
/** Wires up all dialog button events. */
function initDialog() {
const buttonPrev = document.getElementById("dialog-btn-prev");
const buttonNext = document.getElementById("dialog-btn-next");
const buttonClose = document.getElementById("dialog-btn-close");
buttonPrev.addEventListener("click", showPreviousImage);
buttonNext.addEventListener("click", showNextImage);
buttonClose.addEventListener("click", closeDialog);
const dialog = document.getElementById("dialog");
dialog.addEventListener("keydown", (event) => trapFocus(dialog, event));
dialog.addEventListener("click", (event) => {
if (event.target === dialog) {
closeDialog();
}
});
}
/** Initializes the gallery. */
function init() {
buildGallery();
initDialog();
setStepIndicator();
GRID.addEventListener("click", onGridClick);
}
init();