image_server/frontend.html

105 lines
3.3 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>File Server</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; background: #1a1a1a; }
#container {
position: relative;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
img { max-width: 100%; max-height: 100%; cursor: pointer; }
.chevron {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 48px;
color: rgba(255, 255, 255, 0.5);
cursor: pointer;
padding: 20px;
user-select: none;
transition: color 0.2s;
text-decoration: none;
}
.chevron:hover { color: rgba(255, 255, 255, 0.9); }
.chevron.left { left: 10px; }
.chevron.right { right: 10px; }
</style>
</head>
<body>
<div id="container">
<a href="#" id="img-link"><img id="img" alt="Random image"></a>
<a href="#" class="chevron left" id="prev-btn">&#8249;</a>
<a href="#" class="chevron right" id="next-btn">&#8250;</a>
</div>
<script>
let currentData = null;
function loadImageSrc(hash) {
document.getElementById('img').src = '/' + hash + '/data';
document.getElementById('img-link').href = '#';
history.replaceState(null, '', '#' + hash);
}
async function loadInfo(hash) {
const response = await fetch('/' + hash);
if (!response.ok) {
loadRandom();
return;
}
currentData = await response.json();
loadImageSrc(currentData.img);
document.getElementById('img').title = currentData.filename || '';
document.getElementById('prev-btn').href = '#' + currentData.previous;
document.getElementById('next-btn').href = '#' + currentData.next;
}
async function loadRandom() {
const response = await fetch('/random?' + Date.now());
const data = await response.json();
await loadInfo(data.img);
}
window.addEventListener('hashchange', function() {
const hash = window.location.hash.slice(1);
if (hash) {
loadInfo(hash);
} else {
loadRandom();
}
});
window.addEventListener('hashchange', function() {
const hash = window.location.hash.slice(1);
if (hash) loadInfo(hash);
});
document.addEventListener('keydown', function(e) {
e.preventDefault();
if (e.code === 'Space') {
window.location.hash = '';
} else if (e.code === 'ArrowLeft') {
document.getElementById('prev-btn').click();
} else if (e.code === 'ArrowRight') {
document.getElementById('next-btn').click();
} else {
return;
}
});
const hash = window.location.hash.slice(1);
if (hash) {
loadInfo(hash);
} else {
loadRandom();
}
</script>
</body>
</html>