Newer
Older
import React from 'react'
import PropTypes from 'prop-types'
esikkala
committed
import intl from 'react-intl-universal'
import { withStyles } from '@material-ui/core/styles'
import IconButton from '@material-ui/core/IconButton'
import Tooltip from '@material-ui/core/Tooltip'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import MoreVertIcon from '@material-ui/icons/MoreVert'
import Typography from '@material-ui/core/Typography'
import InfoIcon from '@material-ui/icons/InfoOutlined'
esikkala
committed
import ListSubheader from '@material-ui/core/ListSubheader'
import history from '../../History'
import ChartDialog from './ChartDialog'
import { createApexPieChartData } from '../../configs/sampo/ApexCharts/PieChartConfig'
import { createApexLineChartData } from '../../configs/sampo/ApexCharts/LineChartConfig'
import PieChartIcon from '@material-ui/icons/PieChart'
import LineChartIcon from '@material-ui/icons/ShowChart'
const styles = theme => ({
root: {
width: '100%',
height: '100%'
},
headingContainer: {
display: 'flex',
alignItems: 'center',
// justifyContent: 'space-between',
},
facetValuesContainerTen: {
height: 345,
padding: theme.spacing(1)
},
facetValuesContainerThree: {
height: 108,
padding: theme.spacing(1)
},
facetHeaderButtons: {
marginLeft: 'auto'
}
/**
* A component for rendering a header and optional settings dropdown for a facet component.
*/
class FacetHeader extends React.Component {
state = {
this.setState({ anchorEl: event.currentTarget })
handleSortOnClick = buttonID => () => {
this.setState({ anchorEl: null })
let sortDirection
if (buttonID === 'prefLabel') {
if (this.props.facet.sortBy === 'instanceCount') {
sortDirection = 'asc' // default sort direction when sorting by prefLabel
} else {
sortDirection = this.props.facet.sortDirection === 'asc'
? 'desc' : 'asc'
}
if (buttonID === 'instanceCount') {
if (this.props.facet.sortBy === 'prefLabel') {
sortDirection = 'desc' // default sort direction when sorting by instanceCount
} else {
sortDirection = this.props.facet.sortDirection === 'asc'
? 'desc' : 'asc'
}
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'sortDirection',
value: sortDirection
})
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'sortBy',
value: buttonID
})
handleFilterTypeOnClick = buttonID => () => {
this.setState({ anchorEl: null })
if (buttonID === 'uriFilter' && this.props.facet.filterType === 'spatialFilter') {
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'spatialFilter',
value: null
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'filterType',
value: 'uriFilter'
}
if (buttonID === 'spatialFilter' && this.props.facet.filterType === 'uriFilter') {
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'filterType',
value: 'spatialFilter'
history.push({ pathname: `${this.props.rootUrl}/${this.props.resultClass}/faceted-search/${this.props.facet.spatialFilterTab}` })
esikkala
committed
handleSubconceptsOnClick = buttonID => () => {
this.setState({ anchorEl: null })
let selectAlsoSubconcepts
if (buttonID === 'selectAlsoSubconcepts') {
selectAlsoSubconcepts = true
}
if (buttonID === 'doNotSelectSubconcepts') {
selectAlsoSubconcepts = false
}
this.props.clearFacet({
facetClass: this.props.facetClass,
facetID: this.props.facetID
})
this.props.updateFacetOption({
facetClass: this.props.facetClass,
facetID: this.props.facetID,
option: 'selectAlsoSubconcepts',
value: selectAlsoSubconcepts
})
};
this.setState({ anchorEl: null })
const { anchorEl } = this.state
esikkala
committed
const {
sortButton,
spatialFilterButton,
sortBy,
filterType,
type,
esikkala
committed
selectAlsoSubconceptsButton = false,
selectAlsoSubconcepts
} = this.props.facet
const open = Boolean(anchorEl)
const menuButtons = []
esikkala
committed
esikkala
committed
menuButtons.push(
<ListSubheader component='div' key='sortingOptionsSubheader'>
{intl.get('facetBar.sortingOptions')}
</ListSubheader>
)
menuButtons.push(
<MenuItem
key='prefLabel'
selected={sortBy === 'prefLabel'}
onClick={this.handleSortOnClick('prefLabel')}
>
{intl.get('facetBar.sortAlphabetically')}
</MenuItem>
)
menuButtons.push(
<MenuItem
key='instanceCount'
selected={sortBy === 'instanceCount'}
onClick={this.handleSortOnClick('instanceCount')}
>
{intl.get('facetBar.sortByNumberOfSearchResults')}
</MenuItem>
)
esikkala
committed
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
menuButtons.push(
<ListSubheader component='div' key='filterOptionsSubheader'>
{intl.get('facetBar.filterOptions')}
</ListSubheader>
)
menuButtons.push(
<MenuItem
key='uriFilter'
selected={filterType === 'uriFilter'}
onClick={this.handleFilterTypeOnClick('uriFilter')}
>
{intl.get('facetBar.filterByName')}
</MenuItem>
)
menuButtons.push(
<MenuItem
key='spatialFilter'
selected={filterType === 'spatialFilter'}
onClick={this.handleFilterTypeOnClick('spatialFilter')}
>
{intl.get('facetBar.filterByBoundingBox')}
</MenuItem>
)
}
if (type === 'hierarchical' && selectAlsoSubconceptsButton) {
menuButtons.push(
<ListSubheader component='div' key='selectionOptionsSubheader'>
{intl.get('facetBar.selectionOptions')}
</ListSubheader>
)
menuButtons.push(
<MenuItem
key='selectAlsoSubconcepts'
selected={selectAlsoSubconcepts}
onClick={this.handleSubconceptsOnClick('selectAlsoSubconcepts')}
>
{intl.get('facetBar.selectAlsoSubconcepts')}
</MenuItem>
)
menuButtons.push(
<MenuItem
key='doNotSelectSubconcepts'
selected={!selectAlsoSubconcepts}
onClick={this.handleSubconceptsOnClick('doNotSelectSubconcepts')}
>
{intl.get('facetBar.doNotSelectSubconcepts')}
</MenuItem>
)
rawData={this.props.facetConstrainSelf.values}
rawDataUpdateID={this.props.facetConstrainSelfUpdateID}
fetching={this.props.facetConstrainSelf.isFetching}
fetchData={this.props.fetchFacetConstrainSelf}
facetID={this.props.facetID}
facetClass={this.props.facetClass}
rawData={this.props.facetResults.results}
rawDataUpdateID={this.props.facetResults.resultUpdateID}
fetching={this.props.facetResults.fetching}
fetchData={this.props.fetchResults}
resultClass={`${this.props.facetID}LineChart`}
/>}
{menuButtons.length > 0 &&
<>
<Tooltip disableFocusListener title='Filter options'>
<IconButton
aria-label='Filter options'
aria-owns={open ? 'facet-option-menu' : undefined}
aria-haspopup='true'
onClick={this.handleMenuButtonClick}
>
<MoreVertIcon />
</IconButton>
</Tooltip>
<Menu
id='facet-option-menu'
anchorEl={anchorEl}
open={open}
onClose={this.handleMenuClose}
>
{menuButtons}
</Menu>
</>}
render () {
const { classes, isActive, facetDescription, facetLabel } = this.props
const { sortButton, spatialFilterButton, pieChartButton, lineChartButton, selectAlsoSubconceptsButton } = this.props.facet
const showButtons = isActive &&
(sortButton || spatialFilterButton || pieChartButton || lineChartButton || selectAlsoSubconceptsButton)
<Typography variant='body1'>{facetLabel} </Typography>
enterDelay={300}
>
<IconButton>
<InfoIcon />
</IconButton>
</Tooltip>
<div className={classes.facetHeaderButtons}>
{this.renderFacetMenu()}
}
}
FacetHeader.propTypes = {
classes: PropTypes.object.isRequired,
facetClass: PropTypes.string,
resultClass: PropTypes.string,
fetchFacet: PropTypes.func,
fetchResults: PropTypes.func,
facetResults: PropTypes.object,
esikkala
committed
clearFacet: PropTypes.func,
updateFacetOption: PropTypes.func,
facetDescription: PropTypes.string.isRequired,
rootUrl: PropTypes.string.isRequired
export default withStyles(styles)(FacetHeader)