aboutsummaryrefslogtreecommitdiff
path: root/static/index.js
blob: 74386a160daf5dac041eb19e9fe379987f8972fa (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
128
129
130
131
132
133
134
135
136
137
138
139
import * as sfw from 'sfw';
import * as api from 'api';
import Month from './month.js';
import * as service_worker from './service-worker/index.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 => {
		const last = Month.from_unix(images[0].timestamp);
		let first = Month.from_unix(images.findLast(i => i.timestamp).timestamp);
		month_select.months = first.to(last);

		for (const image of images) {
			image_viewer.add(image);
		}

		image_viewer.preload_all();
	});
}

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({
	onmonth: (month) => image_viewer.jump_to(month),
});
const upload_bar = UploadBar.new();

const settings = SettingsView.new({
	onlogout: async () => {
		await api.session.drop();
		login.show();
		main.active_view = image_viewer;
		settings.profile = null;
	},
});

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: async () => {
		if (!settings.profile) {
			settings.profile = await api.session.current();
		}

		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_online()) {
	login.hide();
	reload();
} if (await api.session.is_valid()) {
	login.hide();
	reload();
} else {
	login.focus();
}