aboutsummaryrefslogtreecommitdiff
path: root/static/widgets/image/index.js
blob: 3e403bd96d2db5a590266c62ff9ba255ffc0c841 (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
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
import * as api from 'api';
import * as sfw from 'sfw';
import Month from '../../month.js';
const { Div, Img, A: Link } = sfw.element.native;

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

export default class Image extends sfw.element.Container {
	#container
	#image
	#menu
	#id
	#month
	#date
	#id_element
	#disabled

	constructor() {
		super ({ css })

		this.#disabled = false;

		this.body.append(
			this.#container = Div.new({
				id: 'container',
				children: [
					this.#image = Img.new({
						onload: () => this.#container.classList.add('loaded'),
					}),
					Div.new({ id: 'loading' }),
					this.#menu = Div.new({
						id: 'menu',
						children: [
							Div.new({
								id: 'label',
								children: [
									Div.new({ id: 'title', innerText: 'Id' }),
									this.#id_element = Div.new({ className: 'element id' }),
								],
							}),
							Div.new({
								id: 'label',
								children: [
									Div.new({ id: 'title', innerText: 'Date' }),
									this.#date = Div.new({ className: 'element' }),
								],
							}),
							Div.new({
								id: 'download',
								innerText: 'Download',
								onclick: () => {
									const a = Link.new({
										href: `/api/image/load/${this.#id}`,
										download: `${this.#id}.jpeg`
									})
									a.click()
								}
							}),
							Div.new({
								id: 'delete',
								innerText: 'Delete',
								onclick: () => {
									api.images.remove(this.#id);
									this.parentNode.removeChild(this);
								}
							}),
						],
						ondblclick: (e) => {
							if (e.target != this.#menu) {
								e.stopPropagation();
							}
						}
					})
				],
				ondblclick: (e) => {
					if (!this.#disabled) {
						this.#menu.classList.toggle('open');
						e.preventDefault();
					}
				}
			})
		);
	}

	set metadata(image) {
		const date = new Date(image.timestamp * 1000);
		this.#id = image.id;
		this.#month = Month.from_unix(image.timestamp);
		this.#container.classList.remove('loaded');
		this.#date.innerText = date.toLocaleString();
		this.#id_element.innerText = image.id;
	}

	load() {
		this.#image.src = `/api/image/load/${this.#id}`;
	}

	get month() {
		return this.#month;
	}

	set disabled(value) {
		this.#disabled = value;
		this.#image.setAttribute('draggable', !value);
	}
}