aboutsummaryrefslogtreecommitdiff
path: root/templates/gallery.html
blob: d21b5188ed06063ea709d126a1f379f5726db3b0 (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
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

<!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}}];
const thumbs = [{{range .Photos}}'/g/{{$.Name}}/{{$.ThumbPrefix}}/{{.}}',{{end}}];
let currentIndex = -1;

function openLightbox(index) {
	currentIndex = index;
	const fullSrc = photos[index];
	const img = document.getElementById('lightbox-img');

	img.src = thumbs[index];
	img.classList.add('loading');

	const full = new Image();
	full.onload = () => {
		if (currentIndex === index) {
			img.src = fullSrc;
			img.classList.remove('loading');
		}
	};
	full.src = fullSrc;

	document.getElementById('lightbox-download').href = fullSrc;
	document.getElementById('lightbox').classList.add('active');
}
function closeLightbox() {
	document.getElementById('lightbox').classList.remove('active');
	const img = document.getElementById('lightbox-img');
	img.src = '';
	img.classList.remove('loading');
	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);
});

let touchStartX = 0;
const lightbox = document.getElementById('lightbox');
lightbox.addEventListener('touchstart', e => { touchStartX = e.touches[0].clientX; }, { passive: true });
lightbox.addEventListener('touchend', e => {
	if (currentIndex === -1) return;
	const dx = e.changedTouches[0].clientX - touchStartX;
	if (Math.abs(dx) < 50) return;
	if (dx < 0 && currentIndex < photos.length - 1) openLightbox(currentIndex + 1);
	if (dx > 0 && currentIndex > 0) openLightbox(currentIndex - 1);
});
</script>
</body>
</html>