summaryrefslogtreecommitdiff
path: root/js/init.ts
blob: f24990243b6a7884618fc792a5964b8393129707 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
namespace Ach {
	export async function init() {
		Ach.loadTheme();

		Ach.initImages();
		Ach.initLinks();

		window.addEventListener("popstate", (_e) => {
			location.reload();
		});
	}

	export async function initImages() {
		let page = Ach.getOnlyElement(document, "page");

		let imageList = Array.from(page.getElementsByTagName("x-image"));

		for (let image of imageList) {
			let alt  = image.getAttribute("alt");
			let file = image.getAttribute("data-file");

			if (!alt) {
				alert("stupid superuser forgot to add image caption, please notify");
				alt = "";
			}

			if (!file) {
				throw new Error("file not set for image element");
			}

			console.log("initialising image that links to \"" + file + "\"");

			let sourceUrl    = "/image/source/" + file + ".webp";
			let thumbnailUrl = "/image/thumbnail/" + file + ".avif";

			let blurElement = document.createElement("img");
			blurElement.setAttribute("class", "blur");
			blurElement.setAttribute("src",   thumbnailUrl);

			let hyperlinkElement = document.createElement("a");
			hyperlinkElement.setAttribute("href",   sourceUrl);
			hyperlinkElement.setAttribute("rel",    "noopener noreferrer");
			hyperlinkElement.setAttribute("target", "_blank");

			let image_element = document.createElement("img");
			image_element.setAttribute("alt", alt);
			image_element.setAttribute("src", thumbnailUrl);

			hyperlinkElement.appendChild(image_element);

			image.appendChild(blurElement);
			image.appendChild(hyperlinkElement);
		}
	}

	export async function initLinks() {
		console.log("initialising links");

		let stats = {
			total:         0x0,
			overrideCount: 0x0,
			currentCount:  0x0,
		};

		for (let link of document.getElementsByTagName("a")) {
			++stats.total;

			let address = link.getAttribute("href");

			if (!address) {
				console.log("note: skipping override of empty link");
				continue;
			}

			let pageAnchor = Ach.parseInternalLink(address);

			if (!pageAnchor) {
				console.log(`note: skipping override of link to "${address}"`);
				continue;
			}

			console.log(`note: overriding link to "${address}"`);
			++stats.overrideCount;

			let command = `Ach.loadPage(${JSON.stringify(pageAnchor[0x0])}, ${JSON.stringify(pageAnchor[0x1])})`

			link.removeAttribute("href");
			link.setAttribute("data-page", pageAnchor[0x0]);
			link.setAttribute("onclick", command);

			if (pageAnchor[0x1]) {
				link.setAttribute("data-anchor", pageAnchor[0x1]);
			}
		}

		let currentPage = Ach.currentPage();

		for (let link of document.getElementsByTagName("a")) {
			let page   = link.getAttribute("data-page");
			let anchor = link.getAttribute("data-anchor");

			if (page == currentPage && !anchor) {
				++stats.currentCount;
				link.setAttribute("aria-current", "page");
			} else {
				link.setAttribute("aria-current", "false");
			}
		}

		console.log(`note: initialised (${stats.total}) links`);
		console.log(`note: of which (${stats.overrideCount}) overrides were done`);
		console.log(`note: and of which (${stats.currentCount}) were marked as current`);
	}
}