379 lines
12 KiB
HTML
Raw Permalink Normal View History

{% extends "base.html" %}
{% block title %}Image Archive{% endblock %}
{% block head %}
<style>
h1 {
text-align: center;
margin-bottom: 2rem;
}
.gallery {
display: grid;
2025-04-01 16:56:10 +01:00
grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
gap: 20px;
}
.gallery img {
width: 100%;
height: auto;
border-radius: 10px;
cursor: pointer;
transition: transform 0.2s ease;
}
.gallery img:hover {
transform: scale(1.02);
}
/* Lightbox styles */
.lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.9);
justify-content: center;
align-items: center;
2025-04-01 16:51:43 +01:00
flex-direction: column;
z-index: 999;
2025-06-06 12:32:49 +01:00
padding: 20px 0;
}
.lightbox img {
max-width: 90%;
max-height: 80%;
border-radius: 10px;
}
.lightbox .close {
position: absolute;
top: 20px;
right: 30px;
font-size: 30px;
color: white;
cursor: pointer;
}
2025-04-01 16:51:43 +01:00
.arrow {
position: absolute;
top: 50%;
font-size: 40px;
color: white;
cursor: pointer;
user-select: none;
transform: translateY(-50%);
}
2025-04-01 16:51:43 +01:00
.arrow.left {
left: 20px;
}
2025-04-01 16:51:43 +01:00
.arrow.right {
right: 20px;
}
#lightbox-prompt {
color: #ccc;
font-family: monospace;
white-space: pre-wrap;
background: rgba(0, 0, 0, 0.6);
padding: 10px 20px;
border-radius: 10px;
max-width: 80%;
text-align: left;
margin-top: 20px;
2025-05-27 18:43:58 +01:00
max-height: 25vh;
overflow-y: auto;
}
2025-05-17 11:38:23 +01:00
.home-button {
position: fixed;
top: 20px;
right: 20px;
z-index: 500;
}
.favourites-button {
position: fixed;
top: 20px;
right: 120px;
z-index: 500;
}
.favourite-heart {
position: absolute;
top: 20px;
left: 30px;
font-size: 30px;
color: white;
cursor: pointer;
}
2025-05-17 10:37:10 +01:00
@media (max-width: 600px) {
body {
padding: 1rem;
font-size: 14px;
}
.gallery {
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 12px;
}
.gallery img {
border-radius: 8px;
}
.lightbox .close {
font-size: 36px;
top: 10px;
right: 15px;
}
.arrow {
font-size: 48px;
top: 50%;
}
#lightbox-prompt {
font-size: 14px;
max-width: 90%;
padding: 8px 16px;
2025-05-27 18:43:58 +01:00
max-height: 20vh;
overflow-y: auto;
2025-05-17 10:37:10 +01:00
}
.button-link {
font-size: 14px;
padding: 8px 16px;
}
}
</style>
{% endblock %}
{% block content %}
2025-05-17 11:38:23 +01:00
<a href="/" class="button-link home-button">Home</a>
<button class="button-link favourites-button" id="favourites-button" onclick="toggleFavouritesView()">Show Favourites</button>
2025-05-17 11:38:23 +01:00
2025-06-30 13:16:45 +01:00
<h1 id="page-title">Image Archive</h1>
2025-05-17 10:32:28 +01:00
<div class="gallery" id="gallery"></div>
2025-06-30 14:28:27 +01:00
<div class="lightbox" id="lightbox" tabindex="-1" onkeyup="handleLightboxKeys(event)">
<span class="close" onclick="closeLightbox()">&times;</span>
<span class="favourite-heart" id="favourite-heart" onclick="toggleFavourite()">&#9825;</span>
2025-04-01 16:51:43 +01:00
<span class="arrow left" onclick="prevImage()">&#10094;</span>
2025-05-17 10:32:28 +01:00
<img id="lightbox-img" src="" />
<p id="lightbox-prompt"></p>
2025-04-01 16:51:43 +01:00
<span class="arrow right" onclick="nextImage()">&#10095;</span>
</div>
{% endblock %}
{% block scripts %}
2025-05-17 10:32:28 +01:00
<script>
2025-06-30 13:16:45 +01:00
let allImages = JSON.parse(`[
2025-05-17 10:32:28 +01:00
{% for image in images %}
2025-06-30 13:16:45 +01:00
{ "filename": "{{ image.filename }}", "favourited": {{ 'true' if image.favourited else 'false' }} }{% if not loop.last %},{% endif %}
{% endfor %}
2025-06-30 13:16:45 +01:00
]`);
2025-05-17 10:32:28 +01:00
</script>
<script>
2025-05-17 10:32:28 +01:00
const gallery = document.getElementById('gallery');
const batchSize = 9;
2025-05-17 10:32:28 +01:00
let loadedCount = 0;
let currentIndex = 0;
const detailsCache = {};
let showingFavourites = false;
let filteredImages = allImages;
2025-05-17 10:32:28 +01:00
function createImageElement(image) {
const img = document.createElement('img');
img.src = `/images/thumbnails/${image.filename}`;
img.dataset.fullsrc = `/images/${image.filename}`;
img.dataset.filename = image.filename;
img.dataset.favourited = image.favourited;
2025-05-17 10:32:28 +01:00
img.loading = 'lazy';
img.style.cursor = 'pointer';
img.style.borderRadius = '10px';
img.addEventListener('click', () => openLightbox(img));
return img;
}
function loadNextBatch() {
const imagesToLoad = showingFavourites ? filteredImages : allImages;
const nextImages = imagesToLoad.slice(loadedCount, loadedCount + batchSize);
2025-05-17 10:32:28 +01:00
nextImages.forEach(image => {
const imgEl = createImageElement(image);
gallery.appendChild(imgEl);
});
loadedCount += nextImages.length;
}
function renderGallery() {
gallery.innerHTML = '';
loadedCount = 0;
loadNextBatch();
}
function toggleFavouritesView() {
showingFavourites = !showingFavourites;
const button = document.getElementById('favourites-button');
2025-06-30 13:16:45 +01:00
const pageTitle = document.getElementById('page-title');
if (showingFavourites) {
filteredImages = allImages.filter(img => img.favourited);
button.textContent = 'Show All';
2025-06-30 13:16:45 +01:00
pageTitle.textContent = 'Favourites';
} else {
filteredImages = allImages;
button.textContent = 'Show Favourites';
2025-06-30 13:16:45 +01:00
pageTitle.textContent = 'Image Archive';
}
renderGallery();
}
renderGallery();
2025-05-17 10:32:28 +01:00
window.addEventListener('scroll', () => {
const imagesToLoad = showingFavourites ? filteredImages : allImages;
if (loadedCount >= imagesToLoad.length) return;
2025-05-17 10:32:28 +01:00
if ((window.innerHeight + window.scrollY) >= (document.body.offsetHeight - 100)) {
loadNextBatch();
}
});
function getGalleryImages() {
return Array.from(gallery.querySelectorAll('img'));
}
function openLightbox(imgEl) {
2025-05-17 10:32:28 +01:00
const images = getGalleryImages();
currentIndex = images.indexOf(imgEl);
showImageAndLoadDetails(currentIndex);
2025-06-30 14:28:27 +01:00
const lightbox = document.getElementById("lightbox");
lightbox.style.display = "flex";
lightbox.focus();
}
function updateFavouriteHeart(isFavourited) {
const heart = document.getElementById('favourite-heart');
heart.innerHTML = isFavourited ? '&#9829;' : '&#9825;';
heart.style.color = isFavourited ? 'red' : 'white';
}
function toggleFavourite() {
const images = getGalleryImages();
const imgEl = images[currentIndex];
const filename = imgEl.dataset.filename;
2025-06-30 14:28:27 +01:00
fetch('/favourites/toggle', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ filename: filename })
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
2025-06-30 14:28:27 +01:00
const isFavourited = data.favourited;
imgEl.dataset.favourited = isFavourited;
updateFavouriteHeart(isFavourited);
2025-06-30 14:28:27 +01:00
const originalImage = allImages.find(img => img.filename === filename);
if (originalImage) {
originalImage.favourited = isFavourited;
2025-06-30 14:28:27 +01:00
}
if (showingFavourites) {
filteredImages = allImages.filter(img => img.favourited);
renderGallery();
}
}
});
}
function showImageAndLoadDetails(index) {
2025-05-17 10:32:28 +01:00
const images = getGalleryImages();
const imgEl = images[index];
const filename = imgEl.dataset.filename;
const fullsrc = imgEl.dataset.fullsrc;
const isFavourited = imgEl.dataset.favourited === 'true';
document.getElementById("lightbox-img").src = fullsrc;
updateFavouriteHeart(isFavourited);
if (detailsCache[filename]) {
document.getElementById("lightbox-prompt").textContent =
2025-06-06 12:28:32 +01:00
`Model:${detailsCache[filename].model} - Created:${detailsCache[filename].date}\n\n${detailsCache[filename].prompt}`;
} else {
document.getElementById("lightbox-prompt").textContent = "Loading…";
fetch(`/image-details/${encodeURIComponent(filename)}`)
.then(response => {
if (!response.ok) throw new Error("Network response was not ok");
return response.json();
})
.then(data => {
detailsCache[filename] = data;
document.getElementById("lightbox-prompt").textContent =
2025-06-06 12:28:32 +01:00
`Model:${data.model} - Created:${data.date}\n\n${data.prompt}`;
})
.catch(() => {
document.getElementById("lightbox-prompt").textContent = "Couldnt load details.";
});
}
}
function nextImage() {
2025-05-17 10:32:28 +01:00
const images = getGalleryImages();
const imagesToLoad = showingFavourites ? filteredImages : allImages;
if (currentIndex + 1 >= images.length && loadedCount < imagesToLoad.length) {
loadNextBatch();
setTimeout(() => {
const updatedImages = getGalleryImages();
if (currentIndex + 1 < updatedImages.length) {
currentIndex++;
showImageAndLoadDetails(currentIndex);
}
}, 100);
} else {
currentIndex = (currentIndex + 1) % images.length;
showImageAndLoadDetails(currentIndex);
}
}
function prevImage() {
2025-05-17 10:32:28 +01:00
const images = getGalleryImages();
currentIndex = (currentIndex - 1 + images.length) % images.length;
showImageAndLoadDetails(currentIndex);
}
function closeLightbox() {
document.getElementById("lightbox").style.display = "none";
if (showingFavourites) {
const currentImage = getGalleryImages()[currentIndex];
const wasFavourited = currentImage.dataset.favourited === 'true';
const originalImage = allImages.find(img => img.filename === currentImage.dataset.filename);
if (originalImage && !originalImage.favourited) {
renderGallery();
}
}
}
2025-06-30 14:28:27 +01:00
function handleLightboxKeys(e) {
if (e.key === 'f') {
toggleFavourite();
} else if (e.key === 'Escape') {
closeLightbox();
} else if (e.key === 'ArrowLeft') {
prevImage();
} else if (e.key === 'ArrowRight') {
nextImage();
}
}
</script>
{% endblock %}