-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
181 lines (165 loc) · 5.89 KB
/
index.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React from "react";
import { NavLink } from "react-router-dom";
import { slugify } from "../utils";
class NavBar extends React.Component {
constructor(props) {
super(props);
this.state = { globalSidebar: false, pageSidebar: false };
}
componentDidMount() {
const { pages } = this.props;
pages.forEach((page) => {
if (page.tags && page.tags.includes("sidebar")) {
// Global sidebar
this.setState({ globalSidebar: true });
}
});
}
onNavlinkClick = (event) => {
// Collapse when changing pages
// eslint-disable-next-line no-undef
$("#navPages").collapse("hide");
// See if the new page has a sidebar
this.setState({ pageSidebar: false });
const pageID = event.target.getAttribute("data-page-id");
this.props.pages[pageID].sections.forEach((section) => {
if (section.tags && section.tags.includes("sidebar")) {
// Global sidebar
this.setState({ pageSidebar: true });
}
});
};
render() {
const {
home,
logo,
title,
subtitle,
sourceCodeLink,
pages,
} = this.props;
const { globalSidebar, pageSidebar } = this.state;
let homeEl = "";
if (home) {
homeEl = (
<a href={home} className="home">
<i className="material-icons">home</i>
</a>
);
}
let logoEl = "";
if (logo) {
logoEl = <img alt="logo" className="logo" src={logo}></img>;
}
let collapseButtonSidebar = (
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#sidebar"
aria-controls="sidebar"
aria-expanded="true"
aria-label="Toggle sidebar"
>
<span className="navbar-toggler-icon"></span>
</button>
);
let collapseButton = (
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navPages"
aria-controls="navPages"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
);
let pagesEl = [];
if (pages && pages.length > 1) {
pages.forEach((page, i) => {
if (page.title && !page.tags.includes("sidebar")) {
const pageSlug = slugify(page.title);
const pagePath = pagesEl.length == 0 ? "/" : `${pageSlug}`;
const newPage = (
<li key={page.title} className="nav-item">
<NavLink
to={pagePath}
exact={true}
className="nav-link"
activeClassName="active"
onClick={this.onNavlinkClick}
data-page-id={i}
>
{page.title}
</NavLink>
</li>
);
pagesEl.push(newPage);
}
});
}
let subtitleEl;
if (subtitle) {
subtitleEl = (
<span className="subtitle navbar-text">{subtitle}</span>
);
}
let sourceEl;
if (sourceCodeLink) {
sourceEl = (
<ul className="navbar-nav">
<li key="source-code" className="nav-item">
<a
className="source-code nav-link"
href={sourceCodeLink}
target="_blank"
rel="noreferrer"
aria-label="Source Code"
>
Source Code
<i className="material-icons">launch</i>
</a>
</li>
</ul>
);
}
return (
<header>
<nav className="navbar navbar-expand-md">
<div className="container-fluid">
<div className="nav-content">
{globalSidebar || pageSidebar
? collapseButtonSidebar
: null}
{homeEl}
<span className="navbar-brand">
{logoEl}
{title}
</span>
{pagesEl.length > 1 || sourceCodeLink || subtitle
? collapseButton
: null}
</div>
<div id="navPages" className="collapse navbar-collapse">
<ul className="nav-pages navbar-nav mr-auto">
{pagesEl.length > 1 ? pagesEl : null}
</ul>
{subtitleEl}
{sourceEl}
<span
className="d-inline-block"
data-toggle="tooltip"
>
<div id="kernel-activity"></div>
</span>
</div>
</div>
</nav>
</header>
);
}
}
export default NavBar;