Newer
Older
piotr.kupczyk@id.ethz.ch
committed
import _ from 'lodash'
import React from 'react'
import autoBind from 'auto-bind'
import { withStyles } from '@material-ui/core/styles'
import TableRow from '@material-ui/core/TableRow'
import TableCell from '@material-ui/core/TableCell'
import GridHeader from '@src/js/components/common/grid/GridHeader.jsx'
import CheckboxField from '@src/js/components/common/form/CheckboxField.jsx'
import logger from '@src/js/common/logger.js'
const styles = theme => ({
multiselectable: {},
piotr.kupczyk@id.ethz.ch
committed
multiselectContainer: {
width: '32px'
},
piotr.kupczyk@id.ethz.ch
committed
multiselect: {
backgroundColor: theme.palette.background.primary,
piotr.kupczyk@id.ethz.ch
committed
paddingTop: theme.spacing(1),
paddingBottom: theme.spacing(1),
paddingLeft: theme.spacing(2),
piotr.kupczyk@id.ethz.ch
committed
paddingRight: 0,
position: 'sticky',
left: 0,
zIndex: 100
},
header: {
'&$firstHeader': {
paddingLeft: theme.spacing(2),
position: 'sticky',
left: 0,
zIndex: 100
},
'$multiselectable &$firstHeader': {
piotr.kupczyk@id.ethz.ch
committed
left: '48px'
}
},
firstHeader: {}
piotr.kupczyk@id.ethz.ch
committed
})
class GridHeaders extends React.PureComponent {
constructor(props) {
super(props)
autoBind(this)
}
piotr.kupczyk@id.ethz.ch
committed
handleMultiselectAllRowsChange() {
const { onMultiselectAllRowsChange } = this.props
if (onMultiselectAllRowsChange) {
onMultiselectAllRowsChange()
piotr.kupczyk@id.ethz.ch
committed
}
}
render() {
logger.log(logger.DEBUG, 'GridHeaders.render')
const { multiselectable, columns, classes } = this.props
piotr.kupczyk@id.ethz.ch
committed
if (columns.length === 0) {
return null
}
let rowClass = null
if (multiselectable) {
rowClass = classes.multiselectable
}
piotr.kupczyk@id.ethz.ch
committed
return (
<TableRow classes={{ root: rowClass }}>
piotr.kupczyk@id.ethz.ch
committed
{this.renderMultiselectCell()}
{columns.map((column, columnIndex) =>
this.renderHeaderCell(column, columnIndex)
)}
piotr.kupczyk@id.ethz.ch
committed
</TableRow>
)
}
renderHeaderCell(column, columnIndex) {
const { sortings, onSortChange, classes } = this.props
piotr.kupczyk@id.ethz.ch
committed
const index = _.findIndex(
sortings,
sorting => sorting.columnName === column.name
)
piotr.kupczyk@id.ethz.ch
committed
const headerClasses = [classes.header]
if (columnIndex === 0) {
headerClasses.push(classes.firstHeader)
}
piotr.kupczyk@id.ethz.ch
committed
return (
<GridHeader
key={column.name}
column={column}
piotr.kupczyk@id.ethz.ch
committed
sortCount={sortings.length}
sortIndex={index !== -1 ? index : null}
sortDirection={index !== -1 ? sortings[index].sortDirection : null}
piotr.kupczyk@id.ethz.ch
committed
onSortChange={onSortChange}
piotr.kupczyk@id.ethz.ch
committed
className={headerClasses.join(' ')}
piotr.kupczyk@id.ethz.ch
committed
/>
)
}
renderMultiselectCell() {
piotr.kupczyk@id.ethz.ch
committed
const { multiselectable, multiselectedRows, rows, classes } = this.props
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
if (multiselectable) {
piotr.kupczyk@id.ethz.ch
committed
const multiselectedRowIds = Object.keys(multiselectedRows)
const rowIds = rows.map(row => String(row.id))
const rowIdsSelected = _.intersection(rowIds, multiselectedRowIds)
const value = rowIds.length > 0 && rowIds.length === rowIdsSelected.length
const indeterminate =
rowIdsSelected.length > 0 && rowIdsSelected.length < rowIds.length
return (
<TableCell classes={{ root: classes.multiselect }}>
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.multiselectContainer}>
<CheckboxField
value={value}
indeterminate={indeterminate}
onChange={this.handleMultiselectAllRowsChange}
/>
</div>