aboutsummaryrefslogtreecommitdiff
path: root/templates/gallery.html
blob: 51a71961330ab857961ab29fb5590c33548d8b13 (plain) (blame)
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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.Name}}</title>
<style>{{.CSS}}</style>
</head>
<body>
<div class="container">
<h1>📷 {{.Name}}</h1>

<div class="actions">
<a href="/" class="btn btn-back">← Back</a>
<a href="/g/{{.Name}}/zip" class="btn">⬇ Download all as ZIP</a>
</div>

<!-- Upload -->
<div class="upload-area" id="upload-area">
<p>Drag & drop photos here, or <label for="file-input" class="link">browse</label></p>
<input type="file" id="file-input" multiple accept="image/*" hidden>
<div id="upload-progress"></div>
</div>

<!-- Gallery grid -->
<div class="grid">
{{range $i, $p := .Photos}}
<div class="grid-item" onclick="openLightbox({{$i}})">
<img src="/g/{{$.Name}}/{{$.ThumbPrefix}}/{{$p}}" alt="{{$p}}" loading="lazy">
</div>
{{end}}
</div>
{{if not .Photos}}<p class="empty">No photos yet. Upload some!</p>{{end}}
</div>

<!-- Lightbox -->
<div id="lightbox" class="lightbox" onclick="closeLightbox()">
<img id="lightbox-img" src="">
<a id="lightbox-download" class="lightbox-download" download onclick="event.stopPropagation()">⬇</a>
</div>

<script>
const galleryName = {{.Name}};
const uploadArea = document.getElementById('upload-area');
const fileInput = document.getElementById('file-input');
const progress = document.getElementById('upload-progress');

uploadArea.addEventListener('dragover', e => { e.preventDefault(); uploadArea.classList.add('dragover'); });
uploadArea.addEventListener('dragleave', () => uploadArea.classList.remove('dragover'));
uploadArea.addEventListener('drop', e => {
	e.preventDefault();
	uploadArea.classList.remove('dragover');
	uploadFiles(e.dataTransfer.files);
});
fileInput.addEventListener('change', () => uploadFiles(fileInput.files));

function uploadFiles(files) {
	if (!files.length) return;
	const fd = new FormData();
	for (const f of files) fd.append('photos', f);

	progress.textContent = 'Uploading ' + files.length + ' file(s)…';

	const xhr = new XMLHttpRequest();
	xhr.open('POST', '/g/' + galleryName + '/upload');

	xhr.upload.onprogress = e => {
		if (e.lengthComputable) {
			const pct = Math.round(e.loaded / e.total * 100);
			progress.textContent = 'Uploading… ' + pct + '%';
		}
	};
	xhr.onload = () => {
		if (xhr.status === 200) {
			const resp = JSON.parse(xhr.responseText);
			progress.textContent = resp.uploaded + ' file(s) uploaded ✓';
			setTimeout(() => location.reload(), 800);
		} else {
			progress.textContent = 'Upload failed: ' + xhr.statusText;
		}
	};
	xhr.onerror = () => { progress.textContent = 'Upload failed (network error)'; };
	xhr.send(fd);
}

const photos = [{{range .Photos}}'/g/{{$.Name}}/photo/{{.}}',{{end}}];
let currentIndex = -1;

function openLightbox(index) {
	currentIndex = index;
	const src = photos[index];
	document.getElementById('lightbox-img').src = src;
	document.getElementById('lightbox-download').href = src;
	document.getElementById('lightbox').classList.add('active');
}
function closeLightbox() {
	document.getElementById('lightbox').classList.remove('active');
	document.getElementById('lightbox-img').src = '';
	currentIndex = -1;
}
document.addEventListener('keydown', e => {
	if (e.key === 'Escape') { closeLightbox(); return; }
	if (currentIndex === -1) return;
	if (e.key === 'ArrowRight' && currentIndex < photos.length - 1) openLightbox(currentIndex + 1);
	if (e.key === 'ArrowLeft' && currentIndex > 0) openLightbox(currentIndex - 1);
});
</script>
</body>
</html>