aboutsummaryrefslogtreecommitdiff
path: root/content/sparse_layout_generator/sparse_layout_generator.js
blob: bf82fdd79ce8d0a7fe722c7a59ec85a258f93090 (about) (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
/*
	Generate simple yet clean single-page site(oids)
	
	Copyright © 2021 jahoti (jahoti@tilde.team)
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	   http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

function Sparse(navItems, opts) {
	opts = opts || {};
	var width = opts.width || 20,
	    text = opts.text || 'black',
	    bg = opts.bgcolor || opts.background || 'yellow',
	    pagebg = opts.tabcolor || 'white';
	    gap = opts.gap || '10px';
	
	var body = document.createElement('body'),
	    nav = document.createElement('nav'),
	    style = document.createElement('style');
	
	var link, activeLink, title;
	
	style.innerText = `
	body {
		display: grid;
		grid-column-gap: ${gap};
		column-gap: ${gap};
		grid-template-columns: ${width}%;
	}
	
	nav {
		grid-column: 1;
		position: sticky;
		top: 0;
	}
	
	nav > a:not([href]) {
		color: ${text};
		font-weight: bold;
	}
	
	body, nav { background-color: ${bg}; }
	body > * { grid-row: 1; }
	body > *:not(nav) { background-color: ${pagebg} }
	`
	
	body.append(style);
	body.append(nav);
	
	function onLinkClick(e) {
		var link, section, title;
		if (activeLink) {
			link = activeLink.inactive
			nav.replaceChild(link, activeLink);
			for (pane of link.panes) body.removeChild(pane.parentNode);
			
			delete activeLink.inactive;
		}
		
		link = e.target;
		activeLink = document.createElement('span');
		activeLink.innerText = link.textContent;
		activeLink.inactive = link;
		nav.replaceChild(activeLink, link);
		
		link.panes.forEach((pane, i) => {
			section = document.createElement('article');
			title = document.createElement('h1');
			
			title.innerText = pane.docTitle;
			section.style.gridColumn = (i + 2).toString();
			
			if (pane.docTitleLink) {
				let titleLink = document.createElement('a');
				
				titleLink.href = pane.docTitleLink;
				titleLink.append(title);
				title = titleLink;
			}
			
			section.append(title);
			section.append(pane);
			body.append(section);
		});
	}
	
	document.title = navItems[0];
	
	for (var item of navItems) {
		link = document.createElement('a');
		if (typeof item === 'string') link.innerText = item;
		else {
			link.innerText = item.shift();
			if (link.textContent.startsWith('* ')) {
				link.innerText = link.textContent.substr(2);
				link.style.fontSize = '80%';
			}
			
			if (typeof item[0] === 'string' && item.length === 1) link.href = item[0];
			else {
				link.onclick = onLinkClick;
				link.href = '#';
				if (item.length === 2 && typeof item[1] === 'string') {
					// Add a title link (if applicable)
					item[0].docTitleLink = item.pop();
				}
				
				link.panes = item;
				
				// Add the title info
				if (item.length === 1) item[0].docTitle = link.textContent;
			}
		}
		
		nav.append(link);
		nav.append(document.createElement('br'));
	}
	
	activeLink = null;
	document.body.parentNode.replaceChild(body, document.body);
	nav.querySelector('a[href]').click();
}