aboutsummaryrefslogtreecommitdiff
path: root/static/pages/image-viewer/index.js
blob: 73b824ec9bf1da5366111e0c9574474755a37b76 (plain)
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
import * as sfw from 'sfw';
const { Div, Img } = sfw.element.native;

import Image from '../../widgets/image/index.js';

const css = await sfw.css(import.meta.url, './index.css')

export default class ImageViewer extends sfw.element.Container {
	#container
	#subtitle
	#images

	constructor() {
		super({ css });

		this.#images = [];

		this.body.append(
			this.#container = Div.new({
				id: 'container',
				onscroll: () => this.#images.forEach(image => this.#preload(image)),
				children: [
					this.#subtitle = Div.new({
						id: 'subtitle',
						innerHTML: 'Powered by <i>Memora<i>'
					})
				],
			})
		);
	}

	add(metadata) {
		const image = Image.new({ metadata });
		this.#images.push(image);
		this.#container.insertBefore(image, this.#subtitle);

		this.#preload(image);
	}

	clear() {
		this.#container.innerHTML = '';
		this.#container.append(this.#subtitle);
	}

	jump_to(month) {
		const image = this.#images.find(i => i.month == month || i.month.is_same(month));
		image.scrollIntoView({ behavior: 'instant' })
	}

	#preload(image) {
		const loading_zone_after = (this.#container.scrollTop + this.#container.offsetHeight) * 2;
		const loading_zone_before = this.#container.scrollTop - (this.#container.offsetHeight - image.offsetHeight) * 2;

		if (loading_zone_before < image.offsetTop && image.offsetTop < loading_zone_after) {
			image.load();
		}
	}
}