summaryrefslogtreecommitdiffstats
path: root/static/script.js
blob: 36e2b189b8bbab2a5117c987bd93fd1b7a3eb956 (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
(function() {
	'use strict';

	var init = function() {
		var toggle = document.createElement('button');

		var clickToggle = function() {
			toggle.open = !toggle.open;
			var open = toggle.open ? 'open' : '';
			var secs = document.querySelectorAll('section>details');
			for (var i=0; i<secs.length; i++) {
				secs[i].open = open;
			}
			toggle.textContent = (toggle.open ? 'Collapse' : 'Expand') + ' all sections';
		};

		toggle.addEventListener('click', clickToggle);

		var hashchange = function() {
			var selected = [];
			if (location.hash != '' && location.hash != '#') {
				var h = location.hash.slice(1);
				var el = document.getElementById(h);
				while (el) {
					if (el.tagName == 'DETAILS') {
						selected.push(el);
					}
					el = el.parentNode;
				}
			}

			if (selected.length > 0) {
				toggle.open = true;
				clickToggle();
				for (var i=0; i<selected.length; i++) {
					selected[i].open = 'open';
				}
				selected[0].scrollIntoView();
			} else {
				toggle.open = false;
				clickToggle();
			}
		};
		window.addEventListener('hashchange', hashchange);

		hashchange();
		if (document.querySelectorAll('main section').length > 0) {
			var main = document.querySelector('main');
			main.insertBefore(toggle, main.firstChild);
		}

	};

	if (document.readyState !== 'loading') {
		init();
	} else {
		document.addEventListener('DOMContentLoaded', init);
	}
})();