File size: 12,849 Bytes
3c4f863 |
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
document.addEventListener('DOMContentLoaded', function () {
// Hilfsklasse für Debug-Logging
class Logger {
static debug(message, data = null) {
console.log(`[Debug] ${message}`, data || '');
}
static error(message, error = null) {
console.error(`[Error] ${message}`, error || '');
}
}
// Utility-Funktionen
function showLoading() {
document.body.classList.add('loading');
}
function hideLoading() {
document.body.classList.remove('loading');
}
// Bild-Modal Funktionalität
function openImageModal(img) {
Logger.debug('Opening image modal', img);
const modal = new bootstrap.Modal(document.getElementById('imageModal'));
const modalImg = document.getElementById('modalImage');
const filename = img.dataset.filename;
// Bild und Metadaten setzen
modalImg.src = img.src;
document.getElementById('modalFilename').textContent = filename;
document.getElementById('modalFormat').textContent = img.dataset.format;
document.getElementById('modalTimestamp').textContent = img.dataset.timestamp;
document.getElementById('modalAlbum').textContent = img.dataset.album;
document.getElementById('modalCategory').textContent = img.dataset.category;
document.getElementById('modalPrompt').textContent = img.dataset.prompt;
document.getElementById('modalOptimizedPrompt').textContent = img.dataset.optimized_prompt;
// Click-to-Close Funktionalität
document.querySelector('.image-container').onclick = function(e) {
if (e.target === modalImg) {
modal.hide();
}
};
// Download-Button Funktionalität
document.getElementById('modalDownloadBtn').onclick = async function() {
await downloadSingleImage(filename);
};
modal.show();
}
// Download-Funktionalitäten
// Download functionality
async function downloadSingleImage(filename) {
try {
showLoading();
console.log('Attempting to download:', filename);
const response = await fetch('/flux-pics/single', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename })
});
if (!response.ok) {
const errorText = await response.text();
console.error('Server Error:', errorText);
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
console.log('Download successful, creating blob URL');
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
hideLoading();
} catch (error) {
console.error('Download error:', error);
hideLoading();
alert('Ein Fehler ist aufgetreten: ' + error.message);
}
}
// Main initialization
document.addEventListener('DOMContentLoaded', function () {
// Auswahlfunktionen
function getSelectedImages() {
const selectedImages = [];
const checkboxes = document.querySelectorAll('.select-item:checked');
Logger.debug(`Found ${checkboxes.length} selected images`);
checkboxes.forEach(checkbox => {
const img = checkbox.closest('.card').querySelector('img');
if (img && img.getAttribute('data-filename')) {
selectedImages.push(img.getAttribute('data-filename'));
} else {
Logger.error('Missing image or filename for selected checkbox');
}
});
return selectedImages;
}
// Event Listener für "Alle auswählen" Checkbox
const selectAllCheckbox = document.getElementById('selectAll');
if (selectAllCheckbox) {
const itemCheckboxes = document.querySelectorAll('.select-item');
selectAllCheckbox.addEventListener('change', function () {
itemCheckboxes.forEach(checkbox => {
checkbox.checked = selectAllCheckbox.checked;
});
});
}
// Thumbnail-Galerie Funktionalität
// Thumbnail-Galerie Funktionalität
document.getElementById('thumbgalleryBtn').addEventListener('click', function () {
const selectedImages = getSelectedImages();
if (selectedImages.length === 0) {
alert('Keine Bilder ausgewählt.');
return;
}
const galleryModal = new bootstrap.Modal(document.getElementById('thumbGalleryModal'));
const galleryContainer = document.getElementById('thumbGalleryContainer');
galleryContainer.innerHTML = '';
selectedImages.forEach(filename => {
const container = document.createElement('div');
container.className = 'thumb-container m-2';
const img = document.createElement('img');
img.src = `/flux-pics/${filename}`;
img.className = 'img-thumbnail thumbnail-img';
img.dataset.filename = filename;
img.style.maxWidth = '150px';
img.style.cursor = 'pointer';
const downloadBtn = document.createElement('button');
downloadBtn.className = 'btn btn-sm btn-primary download-thumb';
downloadBtn.innerHTML = '<i class="fas fa-download"></i>';
container.appendChild(img);
container.appendChild(downloadBtn);
galleryContainer.appendChild(container);
// Klick auf Thumbnail öffnet Vollbild
img.addEventListener('click', () => openImageModal(img));
// Download Button
downloadBtn.addEventListener('click', async () => {
await downloadSingleImage(filename);
});
});
galleryModal.show();
});
// Slideshow Funktionalität
document.getElementById('slideshowBtn').addEventListener('click', function () {
const selectedImages = getSelectedImages();
if (selectedImages.length === 0) {
alert('Keine Bilder ausgewählt.');
return;
}
const slideshowModal = new bootstrap.Modal(document.getElementById('slideshowModal'));
const slideshowContainer = document.getElementById('slideshowContainer');
slideshowContainer.innerHTML = '';
let currentSlideIndex = 0;
let slideshowInterval;
const slideInterval = 3000; // 3 Sekunden pro Bild
selectedImages.forEach((filename, index) => {
const div = document.createElement('div');
div.classList.add('carousel-item');
if (index === 0) div.classList.add('active');
const img = document.createElement('img');
img.src = `/flux-pics/${filename}`;
img.classList.add('d-block', 'w-100');
img.dataset.filename = filename;
div.appendChild(img);
slideshowContainer.appendChild(div);
});
const carousel = new bootstrap.Carousel(document.getElementById('carouselExampleControls'), {
interval: false
});
// Play/Pause Funktionalität
const playBtn = document.getElementById('playSlideshow');
const pauseBtn = document.getElementById('pauseSlideshow');
playBtn.addEventListener('click', function() {
slideshowInterval = setInterval(() => {
carousel.next();
}, slideInterval);
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
});
pauseBtn.addEventListener('click', function() {
clearInterval(slideshowInterval);
pauseBtn.style.display = 'none';
playBtn.style.display = 'block';
});
// Vollbild Funktionalität
document.getElementById('fullscreenBtn').addEventListener('click', function() {
const modalElement = document.getElementById('slideshowModal');
if (modalElement.requestFullscreen) {
modalElement.requestFullscreen();
} else if (modalElement.webkitRequestFullscreen) {
modalElement.webkitRequestFullscreen();
} else if (modalElement.msRequestFullscreen) {
modalElement.msRequestFullscreen();
}
});
// Download aktuelles Bild
document.getElementById('downloadCurrentSlide').addEventListener('click', async function() {
const activeSlide = slideshowContainer.querySelector('.carousel-item.active img');
if (activeSlide) {
await downloadSingleImage(activeSlide.dataset.filename);
}
});
slideshowModal.show();
});
// Hilfsfunktion für Einzelbild-Download
async function downloadSingleImage(filename) {
try {
const response = await fetch('/flux-pics/single', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
} catch (error) {
console.error('Fehler beim Download:', error);
alert('Ein Fehler ist aufgetreten: ' + error.message);
}
}
// Grid Layout Funktionalität
document.getElementById('gridLayout')?.addEventListener('change', function () {
const columns = parseInt(this.value);
const imageGrid = document.getElementById('imageGrid');
imageGrid.className = `row row-cols-1 row-cols-md-${columns}`;
});
// Scroll-to-Top Button
const scrollTopBtn = document.getElementById('scrollTopBtn');
if (scrollTopBtn) {
window.addEventListener('scroll', function () {
if (window.scrollY > 300) {
scrollTopBtn.style.display = 'block';
} else {
scrollTopBtn.style.display = 'none';
}
});
scrollTopBtn.addEventListener('click', function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
// Event Listener für Bilddetails
document.querySelectorAll('.image-thumbnail').forEach(function(img) {
img.addEventListener('click', function() {
openImageModal(this);
});
});
// Download ausgewählter Bilder
document.getElementById('downloadSelected')?.addEventListener('click', async function () {
const selectedImages = getSelectedImages();
Logger.debug('Selected images:', selectedImages);
if (selectedImages.length === 0) {
alert('Keine Bilder ausgewählt.');
return;
}
let downloadType = 'single';
if (selectedImages.length > 1) {
const choice = confirm('Möchten Sie die Bilder als ZIP-Datei herunterladen?\nKlicken Sie "OK" für ZIP oder "Abbrechen" für Einzeldownloads.');
if (choice) {
downloadType = 'zip';
}
}
try {
if (downloadType === 'zip') {
showLoading();
const response = await fetch('/flux-pics', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ selectedImages })
});
if (!response.ok) {
const errorText = await response.text();
Logger.error('Server Error:', errorText);
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'images.zip';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
hideLoading();
} else {
for (const filename of selectedImages) {
await downloadSingleImage(filename);
await new Promise(resolve => setTimeout(resolve, 500));
}
}
alert('Download erfolgreich abgeschlossen.');
} catch (error) {
Logger.error('Download error:', error);
hideLoading();
alert('Ein Fehler ist aufgetreten: ' + error.message);
}
});
// Keyboard Navigation
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
const modals = document.querySelectorAll('.modal.show');
modals.forEach(modal => {
const bootstrapModal = bootstrap.Modal.getInstance(modal);
if (bootstrapModal) bootstrapModal.hide();
});
}
});
});
function getImagePath(filename) {
return `/flux-pics/${filename}`;
}
// Und dann verwenden Sie diese Funktion überall:
img.src = getImagePath(filename);
|