summaryrefslogtreecommitdiff
path: root/js/navigation.ts
blob: c1af99f577ca3c4ec374d614122eeb65d5d75c9e (plain) (blame)
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
namespace Ach {
	export async function loadPage(page_name: string, anchor?: string) {
		console.log(`loading page \`${page_name}\``);

		window.scrollTo({
			top:      0.0,
			left:     undefined,
			behavior: "smooth",
		});

		let url = `/html/${page_name}.html`;
		console.log(`note: page is at "${url}"`);

		window.history.pushState(page_name, "", url);

		let response = await fetch(url);

		if (!response.ok) {
			throw new Error(`unable to load page: \"${response.status}\"`);
		}

		let markup = await response.text();

		let parser = new DOMParser();
		let dom = parser.parseFromString(markup, "text/html");

		let body = Ach.getFirstElement(document, "body");

		let title = Ach.getFirstElement(document, "title");
		let page  = Ach.getOnlyElement(document, "page");

		let newTitle = Ach.getFirstElement(dom, "title");
		let newPage  = Ach.getOnlyElement(dom, "page");

		title.replaceWith(newTitle);
		body.setAttribute("data-page", page_name);
		page.replaceWith(newPage);

		initImages();
		initLinks();

		if (anchor) {
			console.log(`going to anchor \`${anchor}\``);

			anchor = `anchor.${anchor}`;

			console.log(`note: anchor has id "${anchor}"`);

			let anchor_element = document.getElementById(anchor);

			if (!anchor_element) {
				throw new Error(`unable to find anchor "${anchor}"`);
			}

			anchor_element.scrollIntoView({
				behavior: "smooth",
			});
		}
	}

	export function toggleSideMenu() {
		window.scrollTo({
			top:      0.0,
			left:     undefined,
			behavior: "smooth",
		});

		let sideMenu = Ach.getOnlyElement(document, "sideMenu");
		let navBar   = Ach.getOnlyElement(document, "navBar");
		let glyph    = Ach.getOnlyElement(document, "glyph");

		sideMenu.classList.toggle("visible");
		glyph.classList.toggle("hidden");

		for (let link of navBar.getElementsByTagName("a")) {
			link.classList.toggle("hidden");
		}
	}

	export function parseInternalLink(address: string): [string, string | undefined] | undefined {
		let regex = /\/html\/([A-Za-z0-9]+)\.html(?:#([A-Za-z0-9]+)){0,1}/;
		let regex_result = regex.exec(address);

		if (!regex_result) {
			return;
		}

		let page   = regex_result[0x1];
		let anchor = regex_result[0x2];

		if (!page) {
			return;
		}

		return [page, anchor];
	}
}