Newer
Older
import React from 'react'
import PropTypes from 'prop-types'
import intl from 'react-intl-universal'
import AppBar from '@material-ui/core/AppBar'
import Toolbar from '@material-ui/core/Toolbar'
import IconButton from '@material-ui/core/IconButton'
import Typography from '@material-ui/core/Typography'
import MenuItem from '@material-ui/core/MenuItem'
import Menu from '@material-ui/core/Menu'
import { withStyles } from '@material-ui/core/styles'
import MoreIcon from '@material-ui/icons/MoreVert'
import Button from '@material-ui/core/Button'
import { Link, NavLink } from 'react-router-dom'
import TopBarSearchField from './TopBarSearchField'
import TopBarInfoButton from './TopBarInfoButton'
import Divider from '@material-ui/core/Divider'
import { has } from 'lodash'
import secoLogo from '../../img/logos/seco-logo-48x50.png'
import { showLanguageButton } from '../../configs/sampo/GeneralConfig'
paddingLeft: theme.spacing(1.5),
paddingRight: theme.spacing(1.5)
sectionDesktop: {
display: 'none',
sectionMobile: {
display: 'flex',
appBarButton: {
color: 'white !important',
border: `1px solid ${theme.palette.primary.main}`
appBarButtonActive: {
border: '1px solid white'
},
appBarDivider: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
borderLeft: '2px solid white'
},
secoLogo: {
marginLeft: theme.spacing(1),
[theme.breakpoints.down('md')]: {
display: 'none'
}
class TopBar extends React.Component {
state = {
this.setState({ infoAnchorEl: event.currentTarget })
this.setState({ infoAnchorEl: null })
handleMobileMenuOpen = event => {
this.setState({ mobileMoreAnchorEl: event.currentTarget })
handleMobileMenuClose = () => {
this.setState({ mobileMoreAnchorEl: null })
// https://material-ui.com/components/buttons/#third-party-routing-library
AdapterLink = React.forwardRef((props, ref) => <Link innerRef={ref} {...props} />);
AdapterNavLink = React.forwardRef((props, ref) => <NavLink innerRef={ref} {...props} />);
const searchMode = perspective.id.startsWith('clientFS') ? 'federated-search' : 'faceted-search'
return (
<a
className={this.props.classes.link}
key={perspective.id}
href={perspective.externalUrl}
target='_blank'
rel='noopener noreferrer'
>
<MenuItem>
{intl.get(`perspectives.${perspective.id}.label`).toUpperCase()}
<MenuItem
key={perspective.id}
component={this.AdapterLink}
to={`/${perspective.id}/${searchMode}`}
{intl.get(`perspectives.${perspective.id}.label`).toUpperCase()}
const searchMode = perspective.id.startsWith('clientFS') ? 'federated-search' : 'faceted-search'
return (
<a
className={this.props.classes.link}
key={perspective.id}
href={perspective.externalUrl}
target='_blank'
rel='noopener noreferrer'
>
<Button
className={this.props.classes.appBarButton}
{intl.get(`perspectives.${perspective.id}.label`).toUpperCase()}
<Button
key={perspective.id}
className={this.props.classes.appBarButton}
component={this.AdapterNavLink}
to={`/${perspective.id}/${searchMode}`}
isActive={(match, location) => location.pathname.startsWith(`/${perspective.id}`)}
activeClassName={this.props.classes.appBarButtonActive}
>
{intl.get(`perspectives.${perspective.id}.label`).toUpperCase()}
renderMobileMenu = perspectives =>
<Menu
anchorEl={this.state.mobileMoreAnchorEl}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
open={Boolean(this.state.mobileMoreAnchorEl)}
onClose={this.handleMobileMenuClose}
>
{perspectives.map(perspective => this.renderMobileMenuItem(perspective))}
<Divider />
<MenuItem
key='feedback'
component={this.AdapterLink}
{intl.get('topBar.info.aboutThePortal').toUpperCase()}
<a
className={this.props.classes.link}
target='_blank'
rel='noopener noreferrer'
>
<MenuItem>
<MenuItem
key='info'
component={this.AdapterLink}
const { classes, perspectives, currentLocale, availableLocales } = this.props
return (
<div className={classes.root}>
{/* Add an empty Typography element to ensure that that the MuiTypography class is loaded for
any lower level components that use MuiTypography class only in translation files */}
<Typography />
<Button component={this.AdapterLink} to='/'>
<Typography variant='h6'>{intl.get('appTitle.short')}</Typography>
<TopBarSearchField
fetchResultsClientSide={this.props.fetchResultsClientSide}
clearResults={this.props.clearResults}
<div className={classes.grow} />
<div className={classes.sectionDesktop}>
{perspectives.map((perspective, index) => this.renderDesktopTopMenuItem(perspective, index))}
<div className={classes.appBarDivider} />
<Button
className={classes.appBarButton}
component={this.AdapterNavLink}
to='/feedback'
isActive={(match, location) => location.pathname.startsWith('/feedback')}
activeClassName={this.props.classes.appBarButtonActive}
>
<Button
className={classes.appBarButton}
component={this.AdapterNavLink}
to='/instructions'
isActive={(match, location) => location.pathname.startsWith('/instructions')}
activeClassName={this.props.classes.appBarButtonActive}
>
{showLanguageButton &&
<TopBarLanguageButton
currentLocale={currentLocale}
availableLocales={availableLocales}
loadLocales={this.props.loadLocales}
/>}
<a
className={classes.secoLogo}
href='https://seco.cs.aalto.fi/projects/mmm'
target='_blank'
rel='noopener noreferrer'
>
<Button><img src={secoLogo} /></Button>
</a>
<div className={classes.sectionMobile}>
<IconButton aria-haspopup='true' onClick={this.handleMobileMenuOpen} color='inherit'>
<MoreIcon />
</IconButton>
</div>
</Toolbar>
</AppBar>
}
}
TopBar.propTypes = {
classes: PropTypes.object.isRequired,
fetchResultsClientSide: PropTypes.func.isRequired,
clearResults: PropTypes.func.isRequired,
loadLocales: PropTypes.func.isRequired,
perspectives: PropTypes.array.isRequired,
currentLocale: PropTypes.string.isRequired,
availableLocales: PropTypes.array.isRequired,
xsScreen: PropTypes.bool.isRequired
export default withStyles(styles)(TopBar)