aboutsummaryrefslogtreecommitdiff
path: root/static/index.js
blob: 2fcca887b21069244d7279aab5d74f7426bb0e67 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as sfw from 'sfw';

import * as api from './api/index.js';

import { literal as m } from './month.js';

import LoginView from './pages/login/index.js';
import MainView from './pages/main/index.js';
import ImageViewer from './pages/image-viewer/index.js';
import Search from './widgets/search/index.js';
import MonthSelect from './widgets/month-select/index.js';
import SettingsView from './pages/settings/index.js';
import ShuffleView from './pages/shuffle/index.js';
import UploadBar from './widgets/upload-bar/index.js';

sfw.theme.add_css(await sfw.css(import.meta.url, './index.css'));

const image_viewer = ImageViewer.new();

const reload = () => {
	image_viewer.clear();
	api.images.list().then(images => {
		for (const image of images) {
			image_viewer.add(`/api/image/load/${image.id}`);
		}
	});
}

const login = LoginView.new({
	onlogin: async (user, password) => {
		if (await api.auth.login(user, password)) {
			reload();
			login.hide();
		} else {
			login.comment = 'Incorrect username or password.';
		}
	},

	onpassword: async (user) => {
		if (await api.auth.is_first_login(user)) {
			login.comment = 'Please enter a new password.';
		} else {
			login.comment = '';
		}
	},
});

const search = Search.new({
	onsubmit: (content) => console.log(content),
	onhide: () => main.show(),
});

const month_select = MonthSelect.new({
	months: m`2019-08`.to(m`2025-11`),
});

const upload_bar = UploadBar.new();

const settings = SettingsView.new({
	onlogout: () => {
		login.show();
		main.active_view = image_viewer;
	},
});

const shuffle = ShuffleView.new({ });

const main = MainView.new({
	active_view: image_viewer,
	active_kind: MainView.Kind.upload,
	onsearch: () => {
		main.active_kind = MainView.Kind.upload;
		main.active_view = image_viewer;
		search.toggle();
		main.hide();
	},
	onmonth: () => {
		main.active_kind = MainView.Kind.upload;
		main.active_view = image_viewer;
		month_select.show();
	},
	onupload: async () => {
		const uploader = await api.images.upload_to_timeline();

		upload_bar.progress = 0;
		upload_bar.show();

		uploader.onprogress = (count, total) => {
			upload_bar.progress = count / total;
		}

		uploader.ondone = () => {
			upload_bar.hide();
			reload();
		}

		uploader.send();
	},
	onshuffle: () => {
		main.active_kind = MainView.Kind.home;
		main.active_view = shuffle;
	},
	onsettings: () => {
		main.active_kind = MainView.Kind.home;
		main.active_view = settings;
	},
	onhome: () => {
		main.active_kind = MainView.Kind.upload;
		main.active_view = image_viewer;
	},
});

document.body.append(
	login,
	main,
	search,
	month_select,
	upload_bar,
);


if (await api.session.is_valid()) {
	login.hide();
	reload();
} else {
	login.focus();
}