summaryrefslogtreecommitdiff
path: root/static/src/pages.js
blob: 0576684df381e61347bec9d67788c9e1a778bdad (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
export default class Page {
	static pages = {}
	static currentPage = null

	static pathnameCallback() {
		const path = window.location.pathname.replace(/\/$/, "")
		const page = Page.pages[path]

		if (!page)
			throw new Error(`Path '${path}' does not exist`)

		if (Page.currentPage)
			Page.currentPage.setVisible(false)

		page.setVisible(true)
		Page.currentPage = page
	}

	static softRedirect(path) {
		window.history.pushState({}, "", path)
		Page.pathnameCallback()
	}

	constructor(pageContainer, endpoints, f_initialize) {
		for (const endpoint of endpoints)
			Page.pages[endpoint] = this

		this.pageContainer = pageContainer
		this.endpoints = endpoints
		this.initialize = f_initialize
		this.visible = false
		this.initialized = false
	}

	setVisible(visible) {
		this.pageContainer.style.display = visible ? null : "none"
		this.visible = visible
		if (visible && !this.initialized && this.initialize)
			this.initialized = this.initialize(this.pageContainer)
	}
}