Newer
Older
piotr.kupczyk@id.ethz.ch
committed
import _ from 'lodash'
import React from 'react'
import { withStyles } from '@material-ui/core/styles'
import Popover from '@material-ui/core/Popover'
import SelectField from '@src/js/components/common/form/SelectField.jsx'
import Button from '@src/js/components/common/form/Button.jsx'
import Container from '@src/js/components/common/form/Container.jsx'
piotr.kupczyk@id.ethz.ch
committed
import Message from '@src/js/components/common/form/Message.jsx'
piotr.kupczyk@id.ethz.ch
committed
import GridExportOptions from '@src/js/components/common/grid/GridExportOptions.js'
piotr.kupczyk@id.ethz.ch
committed
import messages from '@src/js/common/messages.js'
import logger from '@src/js/common/logger.js'
const styles = theme => ({
container: {
piotr.kupczyk@id.ethz.ch
committed
display: 'flex',
alignItems: 'center'
piotr.kupczyk@id.ethz.ch
committed
},
piotr.kupczyk@id.ethz.ch
committed
popup: {
piotr.kupczyk@id.ethz.ch
committed
maxWidth: '300px'
},
piotr.kupczyk@id.ethz.ch
committed
field: {
piotr.kupczyk@id.ethz.ch
committed
paddingBottom: theme.spacing(1),
'&:first-child': {
paddingTop: 0
},
'&:last-child': {
paddingBottom: 0
}
},
button: {
paddingTop: theme.spacing(1)
piotr.kupczyk@id.ethz.ch
committed
}
})
class GridExports extends React.PureComponent {
constructor(props) {
super(props)
this.state = {
piotr.kupczyk@id.ethz.ch
committed
el: null,
piotr.kupczyk@id.ethz.ch
committed
validate: false,
piotr.kupczyk@id.ethz.ch
committed
importCompatibleError: null
piotr.kupczyk@id.ethz.ch
committed
}
this.handleOpen = this.handleOpen.bind(this)
this.handleClose = this.handleClose.bind(this)
piotr.kupczyk@id.ethz.ch
committed
this.handleChange = this.handleChange.bind(this)
piotr.kupczyk@id.ethz.ch
committed
this.handleExport = this.handleExport.bind(this)
}
handleOpen(event) {
this.setState({
el: event.currentTarget
})
}
handleClose() {
piotr.kupczyk@id.ethz.ch
committed
const { exportOptions, onExportOptionsChange } = this.props
piotr.kupczyk@id.ethz.ch
committed
this.setState({
piotr.kupczyk@id.ethz.ch
committed
el: null,
piotr.kupczyk@id.ethz.ch
committed
validate: false,
piotr.kupczyk@id.ethz.ch
committed
importCompatibleError: null
piotr.kupczyk@id.ethz.ch
committed
})
piotr.kupczyk@id.ethz.ch
committed
if (onExportOptionsChange) {
const newExportOptions = {
...exportOptions,
importCompatible: null
}
onExportOptionsChange(newExportOptions)
}
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
async handleChange(event) {
piotr.kupczyk@id.ethz.ch
committed
const { exportOptions, onExportOptionsChange } = this.props
if (onExportOptionsChange) {
const newExportOptions = {
...exportOptions,
[event.target.name]: event.target.value
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
if (newExportOptions.importCompatible === true) {
piotr.kupczyk@id.ethz.ch
committed
newExportOptions.values = GridExportOptions.VALUES.RICH_TEXT
piotr.kupczyk@id.ethz.ch
committed
}
await onExportOptionsChange(newExportOptions)
this.validate()
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
}
handleExport() {
piotr.kupczyk@id.ethz.ch
committed
const { onExport } = this.props
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
this.setState({ validate: true }, () => {
if (this.validate()) {
this.handleClose()
if (onExport) {
onExport()
}
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
})
piotr.kupczyk@id.ethz.ch
committed
}
validate() {
const { exportable } = this.props
piotr.kupczyk@id.ethz.ch
committed
const { validate } = this.state
if (!validate) {
return true
}
piotr.kupczyk@id.ethz.ch
committed
const isXLSExport =
piotr.kupczyk@id.ethz.ch
committed
exportable.fileFormat === GridExportOptions.FILE_FORMAT.XLS
piotr.kupczyk@id.ethz.ch
committed
if (isXLSExport) {
const { importCompatible } = this.props.exportOptions
let importCompatibleError = null
if (_.isNil(importCompatible) || importCompatible === '') {
importCompatibleError = messages.get(
messages.VALIDATION_CANNOT_BE_EMPTY,
messages.get(messages.IMPORT_COMPATIBLE)
)
} else {
importCompatibleError = null
}
this.setState({
importCompatibleError
})
return _.isNil(importCompatibleError)
} else {
return true
piotr.kupczyk@id.ethz.ch
committed
}
}
render() {
logger.log(logger.DEBUG, 'GridExports.render')
piotr.kupczyk@id.ethz.ch
committed
const {
id,
piotr.kupczyk@id.ethz.ch
committed
exportOptions,
disabled,
multiselectable,
multiselectedRows,
piotr.kupczyk@id.ethz.ch
committed
visibleColumns,
piotr.kupczyk@id.ethz.ch
committed
classes
} = this.props
piotr.kupczyk@id.ethz.ch
committed
const { el, importCompatibleError } = this.state
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
const rowsOptions = [
{
label: messages.get(messages.ALL_PAGES),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.ROWS.ALL_PAGES
piotr.kupczyk@id.ethz.ch
committed
},
{
label: messages.get(messages.CURRENT_PAGE),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.ROWS.CURRENT_PAGE
piotr.kupczyk@id.ethz.ch
committed
}
]
if (multiselectable) {
rowsOptions.push({
label: messages.get(messages.SELECTED_ROWS),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.ROWS.SELECTED_ROWS
piotr.kupczyk@id.ethz.ch
committed
})
}
piotr.kupczyk@id.ethz.ch
committed
const isXLSExport =
piotr.kupczyk@id.ethz.ch
committed
exportable.fileFormat === GridExportOptions.FILE_FORMAT.XLS
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
exportable.fileFormat === GridExportOptions.FILE_FORMAT.TSV
const isXLSEntityExport =
piotr.kupczyk@id.ethz.ch
committed
isXLSExport &&
piotr.kupczyk@id.ethz.ch
committed
exportable.fileContent === GridExportOptions.FILE_CONTENT.ENTITIES
const isXLSTypesExport =
piotr.kupczyk@id.ethz.ch
committed
isXLSExport &&
exportable.fileContent === GridExportOptions.FILE_CONTENT.TYPES
piotr.kupczyk@id.ethz.ch
committed
return (
<div className={classes.container}>
<Button
piotr.kupczyk@id.ethz.ch
committed
id={id + '.exports-button-id'}
piotr.kupczyk@id.ethz.ch
committed
label={messages.get(messages.EXPORTS)}
color='default'
disabled={disabled}
piotr.kupczyk@id.ethz.ch
committed
variant='outlined'
piotr.kupczyk@id.ethz.ch
committed
onClick={this.handleOpen}
/>
<Popover
piotr.kupczyk@id.ethz.ch
committed
id={id + '.exports-popup-id'}
piotr.kupczyk@id.ethz.ch
committed
open={Boolean(el)}
anchorEl={el}
onClose={this.handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left'
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left'
}}
>
piotr.kupczyk@id.ethz.ch
committed
<Container square={true} className={classes.popup}>
<div>
piotr.kupczyk@id.ethz.ch
committed
{isXLSExport && (
<div className={classes.field}>
<SelectField
piotr.kupczyk@id.ethz.ch
committed
label={messages.get(messages.IMPORT_COMPATIBLE)}
name='importCompatible'
piotr.kupczyk@id.ethz.ch
committed
error={importCompatibleError}
piotr.kupczyk@id.ethz.ch
committed
mandatory={true}
piotr.kupczyk@id.ethz.ch
committed
sort={false}
piotr.kupczyk@id.ethz.ch
committed
emptyOption={{}}
piotr.kupczyk@id.ethz.ch
committed
label: messages.get(messages.YES),
piotr.kupczyk@id.ethz.ch
committed
value: true
piotr.kupczyk@id.ethz.ch
committed
label: messages.get(messages.NO),
piotr.kupczyk@id.ethz.ch
committed
value: false
piotr.kupczyk@id.ethz.ch
committed
value={exportOptions.importCompatible}
variant='standard'
onChange={this.handleChange}
/>
</div>
)}
piotr.kupczyk@id.ethz.ch
committed
{isXLSExport && exportOptions.importCompatible === true && (
<div className={classes.field}>
<Message type='info'>
{messages.get(messages.EXPORT_IMPORT_COMPATIBLE_INFO)}
</Message>
</div>
)}
{isXLSExport && exportOptions.importCompatible === false && (
<div className={classes.field}>
<Message type='info'>
{messages.get(messages.EXPORT_IMPORT_INCOMPATIBLE_INFO)}
</Message>
</div>
)}
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.field}>
<SelectField
label={messages.get(messages.COLUMNS)}
name='columns'
options={[
{
label: messages.get(messages.ALL_COLUMNS),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.COLUMNS.ALL
piotr.kupczyk@id.ethz.ch
committed
},
{
label: messages.get(messages.VISIBLE_COLUMNS),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.COLUMNS.VISIBLE
piotr.kupczyk@id.ethz.ch
committed
}
]}
value={exportOptions.columns}
variant='standard'
onChange={this.handleChange}
/>
</div>
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.field}>
piotr.kupczyk@id.ethz.ch
committed
<SelectField
label={messages.get(messages.ROWS)}
name='rows'
options={rowsOptions}
value={exportOptions.rows}
variant='standard'
onChange={this.handleChange}
/>
piotr.kupczyk@id.ethz.ch
committed
</div>
{(isTSVExport || isXLSEntityExport) && (
<div className={classes.field}>
<SelectField
label={messages.get(messages.VALUES)}
name='values'
options={[
{
label: messages.get(messages.PLAIN_TEXT),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.VALUES.PLAIN_TEXT
},
{
label: messages.get(messages.RICH_TEXT),
piotr.kupczyk@id.ethz.ch
committed
value: GridExportOptions.VALUES.RICH_TEXT
piotr.kupczyk@id.ethz.ch
committed
disabled={exportOptions.importCompatible}
value={exportOptions.values}
variant='standard'
onChange={this.handleChange}
/>
</div>
)}
piotr.kupczyk@id.ethz.ch
committed
{(isTSVExport || isXLSEntityExport) &&
piotr.kupczyk@id.ethz.ch
committed
exportOptions.values ===
GridExportOptions.VALUES.PLAIN_TEXT && (
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.field}>
<Message type='warning'>
{messages.get(messages.EXPORT_PLAIN_TEXT_WARNING)}
</Message>
</div>
)}
piotr.kupczyk@id.ethz.ch
committed
{isXLSTypesExport && (
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.field}>
<SelectField
label={messages.get(messages.INCLUDE_DEPENDENCIES)}
name='includeDependencies'
piotr.kupczyk@id.ethz.ch
committed
options={[
{
label: messages.get(messages.YES),
value: true
piotr.kupczyk@id.ethz.ch
committed
},
{
label: messages.get(messages.NO),
value: false
piotr.kupczyk@id.ethz.ch
committed
}
]}
piotr.kupczyk@id.ethz.ch
committed
sort={false}
value={exportOptions.includeDependencies}
piotr.kupczyk@id.ethz.ch
committed
variant='standard'
onChange={this.handleChange}
/>
</div>
piotr.kupczyk@id.ethz.ch
committed
<div className={classes.button}>
piotr.kupczyk@id.ethz.ch
committed
<Button
piotr.kupczyk@id.ethz.ch
committed
label={messages.get(messages.EXPORT)}
type='neutral'
piotr.kupczyk@id.ethz.ch
committed
disabled={
piotr.kupczyk@id.ethz.ch
committed
(exportOptions.columns ===
GridExportOptions.COLUMNS.VISIBLE &&
_.isEmpty(visibleColumns)) ||
(exportOptions.rows ===
GridExportOptions.ROWS.SELECTED_ROWS &&
_.isEmpty(multiselectedRows))
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
onClick={this.handleExport}
/>