2024-10-22 21:58:06 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
let viewerOpen = false;
|
2024-11-19 10:36:33 +01:00
|
|
|
let currentIndex = -1;
|
|
|
|
let imageList = [];
|
|
|
|
|
|
|
|
// Initialize imageList with all images on the page
|
|
|
|
function initializeImageList() {
|
|
|
|
imageList = Array.from(document.querySelectorAll('.image img.clickable'));
|
|
|
|
}
|
2024-10-22 21:58:06 +02:00
|
|
|
|
|
|
|
const viewerOverlay = document.getElementById('image-viewer-overlay');
|
2024-12-02 14:36:08 +01:00
|
|
|
|
|
|
|
// Set the innerHTML of viewerOverlay
|
2024-10-22 21:58:06 +02:00
|
|
|
viewerOverlay.innerHTML = `
|
|
|
|
<div id="image-viewer" class="image_view image_hide">
|
|
|
|
<div class="image-view-close">
|
2024-11-19 10:36:33 +01:00
|
|
|
<button class="btn-nostyle" id="viewer-prev-button">
|
2024-12-02 18:40:41 +01:00
|
|
|
<div class="material-icons-round icon_visibility clickable image-before"></div> <!-- navigate_before -->
|
2024-11-19 10:36:33 +01:00
|
|
|
</button>
|
|
|
|
<button class="btn-nostyle" id="viewer-next-button">
|
2024-12-02 18:40:41 +01:00
|
|
|
<div class="material-icons-round icon_visibility clickable image-next"></div> <!-- navigate_next -->
|
2024-10-22 21:58:06 +02:00
|
|
|
</button>
|
2024-11-19 10:36:33 +01:00
|
|
|
<button class="btn-nostyle" id="viewer-close-button">
|
2024-12-02 18:40:41 +01:00
|
|
|
<div class="material-icons-round icon_visibility clickable image-close"></div> <!-- close -->
|
2024-10-22 21:58:06 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
2024-12-02 14:36:08 +01:00
|
|
|
<div class="view-image" id="viewer-image-container">
|
|
|
|
<img id="viewer-image" class="view-image-img" src="" alt="">
|
|
|
|
</div>
|
2024-10-22 21:58:06 +02:00
|
|
|
<p class="image-alt" id="viewer-title"></p>
|
|
|
|
<p>
|
2024-11-19 10:36:33 +01:00
|
|
|
<a class="full-size" id="viewer-full-size-link" href="#" target="_blank">Show source website</a>
|
|
|
|
<a class="proxy-size" id="viewer-proxy-size-link" href="#" target="_blank">Show in fullscreen</a>
|
2024-10-22 21:58:06 +02:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2024-12-02 14:36:08 +01:00
|
|
|
const imageView = viewerOverlay.querySelector('#image-viewer');
|
|
|
|
if (!imageView) {
|
|
|
|
console.error('imageView is null');
|
|
|
|
}
|
|
|
|
|
|
|
|
const imagesContainer = document.querySelector('.images');
|
|
|
|
if (!imagesContainer) {
|
|
|
|
console.error('imagesContainer is null');
|
|
|
|
}
|
2024-10-22 21:58:06 +02:00
|
|
|
|
|
|
|
function openImageViewer(element) {
|
2024-11-19 10:36:33 +01:00
|
|
|
initializeImageList(); // Update the image list
|
|
|
|
|
2024-10-22 21:58:06 +02:00
|
|
|
const parentImageDiv = element.closest('.image');
|
|
|
|
if (!parentImageDiv) return;
|
|
|
|
|
2024-11-19 10:36:33 +01:00
|
|
|
currentIndex = imageList.findIndex(img => img === parentImageDiv.querySelector('img.clickable'));
|
|
|
|
if (currentIndex === -1) return;
|
2024-10-22 21:58:06 +02:00
|
|
|
|
2024-11-19 10:36:33 +01:00
|
|
|
displayImage(currentIndex);
|
2024-10-22 21:58:06 +02:00
|
|
|
viewerOpen = true;
|
|
|
|
|
2024-12-02 14:36:08 +01:00
|
|
|
imagesContainer.classList.remove('images_viewer_hidden');
|
|
|
|
document.body.classList.add('viewer-open');
|
|
|
|
viewerOverlay.style.display = 'block';
|
|
|
|
|
2024-11-19 10:36:33 +01:00
|
|
|
imageView.classList.remove('image_hide');
|
|
|
|
imageView.classList.add('image_show');
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayImage(index) {
|
|
|
|
if (index < 0 || index >= imageList.length) return;
|
2024-12-02 14:36:08 +01:00
|
|
|
|
2024-11-19 10:36:33 +01:00
|
|
|
const imgElement = imageList[index];
|
|
|
|
const parentImageDiv = imgElement.closest('.image');
|
2024-12-02 14:36:08 +01:00
|
|
|
|
2024-12-02 10:45:50 +01:00
|
|
|
if (!parentImageDiv) {
|
|
|
|
console.warn('Parent image div not found');
|
|
|
|
return;
|
|
|
|
}
|
2024-12-02 14:36:08 +01:00
|
|
|
|
2024-12-02 10:45:50 +01:00
|
|
|
// Use the `data-full` attribute for the full image URL
|
|
|
|
let fullImageUrl = imgElement.getAttribute('data-full') || imgElement.src;
|
|
|
|
const title = imgElement.alt || 'Untitled';
|
|
|
|
|
2024-12-02 14:36:08 +01:00
|
|
|
// Get the source URL from the data-source attribute
|
|
|
|
const sourceUrl = imgElement.getAttribute('data-source');
|
2024-12-02 10:45:50 +01:00
|
|
|
|
|
|
|
// Fallback logic: if sourceUrl is null, use `data-proxy-full` or a meaningful default
|
|
|
|
const proxyFullUrl = imgElement.getAttribute('data-proxy-full') || fullImageUrl;
|
|
|
|
|
|
|
|
// Check if full image is missing, fallback to proxy size
|
|
|
|
if (fullImageUrl === '/static/images/missing.svg') {
|
|
|
|
fullImageUrl = proxyFullUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elements in the viewer
|
2024-10-22 21:58:06 +02:00
|
|
|
const viewerImage = document.getElementById('viewer-image');
|
|
|
|
const viewerTitle = document.getElementById('viewer-title');
|
2024-11-19 10:36:33 +01:00
|
|
|
const fullSizeLink = document.getElementById('viewer-full-size-link');
|
|
|
|
const proxySizeLink = document.getElementById('viewer-proxy-size-link');
|
2024-12-02 14:36:08 +01:00
|
|
|
|
|
|
|
viewerImage.src = fullImageUrl;
|
2024-10-22 21:58:06 +02:00
|
|
|
viewerTitle.textContent = title;
|
2024-12-02 14:36:08 +01:00
|
|
|
|
|
|
|
fullSizeLink.href = sourceUrl || proxyFullUrl;
|
|
|
|
proxySizeLink.href = fullImageUrl;
|
2024-10-22 21:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.body.addEventListener('click', function(e) {
|
|
|
|
let target = e.target;
|
|
|
|
let clickableElement = target.closest('img.clickable, .img_title.clickable');
|
|
|
|
|
|
|
|
if (clickableElement) {
|
|
|
|
e.preventDefault();
|
|
|
|
openImageViewer(clickableElement);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function closeImageViewer() {
|
|
|
|
imageView.classList.remove('image_show');
|
|
|
|
imageView.classList.add('image_hide');
|
|
|
|
viewerOpen = false;
|
2024-11-19 10:36:33 +01:00
|
|
|
currentIndex = -1;
|
2024-12-02 14:36:08 +01:00
|
|
|
|
|
|
|
imagesContainer.classList.add('images_viewer_hidden');
|
|
|
|
document.body.classList.remove('viewer-open');
|
|
|
|
viewerOverlay.style.display = 'none';
|
2024-10-22 21:58:06 +02:00
|
|
|
}
|
|
|
|
|
2024-11-19 10:36:33 +01:00
|
|
|
// Navigation functions
|
|
|
|
function showPreviousImage() {
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
currentIndex--;
|
|
|
|
displayImage(currentIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showNextImage() {
|
|
|
|
if (currentIndex < imageList.length - 1) {
|
|
|
|
currentIndex++;
|
|
|
|
displayImage(currentIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event listeners for navigation and closing
|
2024-10-22 21:58:06 +02:00
|
|
|
document.getElementById('viewer-close-button').addEventListener('click', closeImageViewer);
|
2024-11-19 10:36:33 +01:00
|
|
|
document.getElementById('viewer-prev-button').addEventListener('click', showPreviousImage);
|
|
|
|
document.getElementById('viewer-next-button').addEventListener('click', showNextImage);
|
|
|
|
|
|
|
|
// Close viewer when clicking outside the image
|
2024-12-02 14:36:08 +01:00
|
|
|
document.addEventListener('click', function(e) {
|
|
|
|
if (viewerOpen) {
|
|
|
|
const target = e.target;
|
|
|
|
const clickedInsideViewer = viewerOverlay.contains(target) || target.closest('.image');
|
|
|
|
if (!clickedInsideViewer) {
|
|
|
|
closeImageViewer();
|
|
|
|
}
|
2024-10-22 21:58:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle keyboard events for closing and navigation
|
|
|
|
document.addEventListener('keydown', function(e) {
|
2024-12-02 14:36:08 +01:00
|
|
|
if (viewerOpen) {
|
2024-10-22 21:58:06 +02:00
|
|
|
if (e.key === 'Escape') {
|
|
|
|
closeImageViewer();
|
2024-11-19 10:36:33 +01:00
|
|
|
} else if (e.key === 'ArrowLeft') {
|
|
|
|
showPreviousImage();
|
|
|
|
} else if (e.key === 'ArrowRight') {
|
|
|
|
showNextImage();
|
2024-10-22 21:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
2024-12-02 14:36:08 +01:00
|
|
|
});
|
2024-10-22 21:58:06 +02:00
|
|
|
});
|