-
Notifications
You must be signed in to change notification settings - Fork 12
/
Header.jsx
353 lines (336 loc) · 12.2 KB
/
Header.jsx
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import * as React from 'react';
import { styled, useTheme } from '@mui/material/styles';
import Box from '@mui/material/Box';
import MuiDrawer from '@mui/material/Drawer';
import MuiAppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import List from '@mui/material/List';
import CssBaseline from '@mui/material/CssBaseline';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';
import IconButton from '@mui/material/IconButton';
import MenuIcon from '@mui/icons-material/Menu';
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import { Paper, Popper, Grow, ClickAwayListener, Collapse, Button } from '@mui/material';
import {
ExpandLess as ExpandLessIcon,
ExpandMore as ExpandMoreIcon,
} from '@mui/icons-material';
import { map, isEmpty, get } from 'lodash';
import {
isLoggedIn, isServerSwitched, canSwitchServer, getAppliedServerConfig, getEnv,
getSiteTitle, getLoginURL, isAdminUser
} from '../../common/utils';
import { WHITE, BLACK } from '../../common/constants';
import SearchInput from '../search/SearchInput';
import SearchByAttributeInput from '../search/SearchByAttributeInput';
import UserOptions from '../users/UserOptions';
import Favorites from './Favorites';
import RecentHistory from './RecentHistory';
import { OPTIONS, SITE_URL } from './MenuOptions.jsx';
import AppsMenu from '../common/AppsMenu';
import ServerConfigsChip from '../common/ServerConfigsChip';
import Languages from './Languages';
const drawerWidth = 250;
const openedMixin = (theme) => ({
width: drawerWidth,
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
overflowX: 'hidden',
});
const closedMixin = (theme) => ({
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: 'hidden',
width: `calc(${theme.spacing(7)} + 1px)`,
[theme.breakpoints.up('sm')]: {
width: `calc(${theme.spacing(8)} + 1px)`,
},
});
const DrawerHeader = styled('div')(({ theme }) => ({
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
padding: theme.spacing(0, 1),
// necessary for content to be below app bar
...theme.mixins.toolbar,
}));
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== 'open',
})(() => ({
backgroundColor: WHITE,
color: BLACK,
borderLeft: 'none',
borderBottom: '1px solid rgba(0, 0, 0, 0.12)',
boxShadow: 'none',
zIndex: 1300,
}));
const Drawer = styled(MuiDrawer, { shouldForwardProp: (prop) => prop !== 'open' })(
({ theme, open }) => ({
width: drawerWidth,
flexShrink: 0,
whiteSpace: 'nowrap',
boxSizing: 'border-box',
...(open && {
...openedMixin(theme),
'& .MuiDrawer-paper': openedMixin(theme),
}),
...(!open && {
...closedMixin(theme),
'& .MuiDrawer-paper': closedMixin(theme),
}),
}),
);
const NestedMenuContainer = ({ open, toggleState, anchorRef, handleClose, toggleFunc, children}) => {
return open ?
<Collapse in={toggleState} timeout="auto" unmountOnExit>
{children}
</Collapse> :
<Popper
open={toggleState}
anchorEl={anchorRef.current}
transition
className='menu-popper-right'
placement='right'
>
{({ TransitionProps }) => (
<Grow {...TransitionProps}>
<Paper>
<ClickAwayListener onClickAway={event => handleClose(event, anchorRef, toggleFunc)}>
{children}
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
}
const Header = props => {
const theme = useTheme();
const [open, setOpen] = React.useState(false);
const communityAnchorRef = React.useRef(null);
const toolsAnchorRef = React.useRef(null);
const authenticated = isLoggedIn()
const [nestedCommunity, setNestedCommunity] = React.useState(false);
const [nestedTools, setNestedTools] = React.useState(false);
const handleDrawerClose = () => setOpen(false)
const toggleNestedCommunity = () => {
const value = !nestedCommunity
if(value)
setNestedTools(false)
setNestedCommunity(value)
};
const toggleNestedTools = () => {
const value = !nestedTools
if(value)
setNestedCommunity(false)
setNestedTools(value)
};
const handleCloseNested = (event, anchorRef, toggleFunc) => {
if (anchorRef.current && anchorRef.current.contains(event.target))
return;
toggleFunc();
};
const toggleOpen = () => setOpen(prevOpen => {
const newOpen = !prevOpen
props.onOpen(newOpen)
setTimeout(() => window.dispatchEvent(new CustomEvent("resize")), 300)
if(!newOpen) {
setNestedTools(false)
setNestedCommunity(false)
}
return newOpen
})
const serverConfig = getAppliedServerConfig()
const siteConfiguration = get(serverConfig, 'info.site')
const isFHIRServer = get(serverConfig, 'type') === 'fhir';
const env = getEnv()
const isProduction = env === 'production';
const hideLeftNav = get(siteConfiguration, 'noLeftMenu', false)
const hideTermBrowserApp = get(siteConfiguration, 'hideTermBrowserApp', false)
const hideImportApp = get(siteConfiguration, 'hideImportApp', false)
const hideAppsMenu = hideImportApp && hideTermBrowserApp;
const getLogo = () => {
let logo = getSiteTitle()
if(get(siteConfiguration, 'logoText'))
logo = siteConfiguration.logoText
if(get(siteConfiguration, 'logoURL'))
logo = (<img src={siteConfiguration.logoURL} style={{width: '50px', height: '50px', marginTop: '-10px'}} />);
return logo
}
return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<AppBar position="fixed" open={open}>
<Toolbar>
{
!hideLeftNav &&
<IconButton
color="primary"
aria-label="open drawer"
onClick={toggleOpen}
edge="start"
sx={{
marginRight: '16px',
}}
>
<MenuIcon />
</IconButton>
}
<Typography variant="h6" className="brand col-sm-1" style={{padding: '0 5px'}}>
<a className="no-anchor-styles" href={isProduction ? SITE_URL : '/'} rel="noopener noreferrer">
{getLogo()}
</a>
</Typography>
<div className="col-sm-8 col-xs-6">
{
props.fhir ?
<SearchByAttributeInput {...props} /> :
<SearchInput {...props} />
}
</div>
<div className='col-sm-4 col-xs-6 pull-right no-side-padding'style={{textAlign: 'right'}}>
{
canSwitchServer() && isServerSwitched() &&
<ServerConfigsChip />
}
{
authenticated ?
<span style={{marginLeft: '20px'}}>
{isAdminUser() && <Languages />}
<RecentHistory />
<Favorites />
{
!hideAppsMenu &&
<AppsMenu
hideTermBrowserApp={hideTermBrowserApp}
hideImportApp={hideImportApp}
/>
}
<UserOptions />
</span> :
(
!isFHIRServer &&
<span style={{marginLeft: '20px'}}>
<Button className='primary-btn' href={getLoginURL()} color='primary' variant='contained'>
Sign In
</Button>
</span>
)
}
</div>
</Toolbar>
</AppBar>
<Drawer variant="permanent" open={open}>
<DrawerHeader>
<IconButton onClick={handleDrawerClose}>
{theme.direction === 'rtl' ? <ChevronRightIcon /> : <ChevronLeftIcon />}
</IconButton>
</DrawerHeader>
<Divider />
<List>
{
map(OPTIONS, option => {
const { href, label, nested, selected, icon } = option;
const hasNested = !isEmpty(nested);
const isCommunity = label === 'Community';
const isTools = label === 'Tools';
const toggleFunc = isCommunity ? toggleNestedCommunity : toggleNestedTools;
const toggleState = isCommunity ? nestedCommunity : nestedTools;
let anchorRef;
if(isCommunity)
anchorRef = communityAnchorRef
if(isTools)
anchorRef = toolsAnchorRef
return (
<React.Fragment key={label}>
<ListItem disablePadding sx={{ display: 'block' }}>
<ListItemButton
className='btn'
component="a"
target='_blank'
selected={selected}
href={!hasNested ? href : undefined}
onClick={hasNested ? toggleFunc : undefined}
sx={{
minHeight: 48,
justifyContent: open ? 'initial' : 'center',
px: 2.5,
}}
ref={anchorRef}
>
<ListItemIcon
sx={{
minWidth: 0,
mr: open ? 3 : 'auto',
justifyContent: 'center',
}}
>
{icon}
</ListItemIcon>
<ListItemText primary={label} sx={{ opacity: open ? 1 : 0 }} />
{
hasNested && open && (
toggleState ?
<ExpandLessIcon /> :
<ExpandMoreIcon />
)
}
</ListItemButton>
</ListItem>
{
(open ? hasNested : (anchorRef && toggleState)) &&
<NestedMenuContainer open={open} toggleState={toggleState} toggleFunc={toggleFunc} anchorRef={anchorRef} handleClose={event => handleCloseNested(event, anchorRef, toggleFunc)}>
<List component="div" disablePadding>
{
nested.map(nestedOption => (
<ListItemButton
className='btn'
component="a"
target='_blank'
key={nestedOption.label}
href={nestedOption.href}
sx={{
minHeight: 48,
pl: 4,
whiteSpace: 'pre-wrap'
}}
>
<ListItemIcon
sx={{
minWidth: 0,
mr: 3,
justifyContent: 'center',
}}
>
{nestedOption.icon}
</ListItemIcon>
<ListItemText primary={nestedOption.label} secondary={nestedOption.deprecated ? 'deprecated' : undefined} style={nestedOption.deprecated ? {fontStyle: 'italic'} : {}} />
</ListItemButton>
))
}
</List>
</NestedMenuContainer>
}
</React.Fragment>
)
}
)}
</List>
</Drawer>
<Box component="main" sx={{ flexGrow: 1, p: 0 }}>
<DrawerHeader />
{ props.children }
</Box>
</Box>
);
}
export default Header;