Skip to content
Snippets Groups Projects
Grid.jsx 10.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { withStyles } from '@material-ui/core/styles'
    
    import Loading from '@src/js/components/common/loading/Loading.jsx'
    
    import TableHead from '@material-ui/core/TableHead'
    
    import TableBody from '@material-ui/core/TableBody'
    
    import Header from '@src/js/components/common/form/Header.jsx'
    
    import GridController from '@src/js/components/common/grid/GridController.js'
    
    import GridFilters from '@src/js/components/common/grid/GridFilters.jsx'
    import GridHeaders from '@src/js/components/common/grid/GridHeaders.jsx'
    
    import GridSelectionInfo from '@src/js/components/common/grid/GridSelectionInfo.jsx'
    
    import GridRow from '@src/js/components/common/grid/GridRow.jsx'
    
    import GridRowFullWidth from '@src/js/components/common/grid/GridRowFullWidth.jsx'
    
    import GridExports from '@src/js/components/common/grid/GridExports.jsx'
    
    import GridExportLoading from '@src/js/components/common/grid/GridExportLoading.jsx'
    
    import GridExportWarnings from '@src/js/components/common/grid/GridExportWarnings.jsx'
    
    import GridExportError from '@src/js/components/common/grid/GridExportError.jsx'
    
    import GridPaging from '@src/js/components/common/grid/GridPaging.jsx'
    
    import GridColumnsConfig from '@src/js/components/common/grid/GridColumnsConfig.jsx'
    
    import GridFiltersConfig from '@src/js/components/common/grid/GridFiltersConfig.jsx'
    
    import ComponentContext from '@src/js/components/common/ComponentContext.js'
    
        zIndex: '200',
        backgroundColor: theme.palette.background.paper
      },
    
      title: {
        paddingTop: theme.spacing(1),
        paddingBottom: 0
      },
    
    class Grid extends React.PureComponent {
    
        if (this.props.controller) {
          this.controller = this.props.controller
        } else {
          this.controller = new GridController()
    
        this.controller.init(new ComponentContext(this))
    
    
        if (this.props.controllerRef) {
          this.props.controllerRef(this.controller)
        }
    
          <div
            id={id}
            onClick={this.handleClickContainer}
            className={classes.container}
          >
    
              <Loading loading={loading} styles={{ root: classes.loading }}>
    
                    <TableHead classes={{ root: classes.tableHead }}>
                      {this.renderTitle()}
                      {this.renderPagingAndConfigsAndExports()}
                      {this.renderHeaders()}
                      {this.renderFilters()}
    
                    </TableHead>
                    <TableBody>
                      {rows.map(row => {
                        return this.renderRow(row)
                      })}
                    </TableBody>
                  </Table>
    
    
      renderTitle() {
        const { header, multiselectable, classes } = this.props
    
        if (header === null || header === undefined) {
          return null
        }
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
    
          <GridRowFullWidth
            multiselectable={multiselectable}
            columns={visibleColumns}
            styles={{ cell: classes.titleCell, content: classes.titleContent }}
          >
            <div onClick={this.handleClickContainer}>
              <Header styles={{ root: classes.title }}>{header}</Header>
            </div>
          </GridRowFullWidth>
    
        )
      }
    
      renderPagingAndConfigsAndExports() {
        const { multiselectable, classes } = this.props
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
    
          <GridRowFullWidth
            multiselectable={multiselectable}
            columns={visibleColumns}
            styles={{
              content: classes.pagingAndConfigsAndExportsContent
            }}
          >
            {this.renderPaging()}
            {this.renderConfigs()}
            {this.renderExports()}
          </GridRowFullWidth>
    
        const { page, pageSize, totalCount } = this.state
    
        return (
          <GridPaging
    
            count={totalCount}
            page={page}
            pageSize={pageSize}
            onPageChange={this.controller.handlePageChange}
            onPageSizeChange={this.controller.handlePageSizeChange}
          />
        )
      }
    
      renderConfigs() {
    
        const { loading, filterMode, columnsVisibility } = this.state
    
        const allColumns = this.controller.getAllColumns()
    
        return (
          <React.Fragment>
            <GridColumnsConfig
    
              columns={allColumns}
              columnsVisibility={columnsVisibility}
              loading={loading}
              onVisibleChange={this.controller.handleColumnVisibleChange}
              onOrderChange={this.controller.handleColumnOrderChange}
            />
            <GridFiltersConfig
    
              filterModes={filterModes}
              filterMode={filterMode}
              loading={loading}
              onFilterModeChange={this.controller.handleFilterModeChange}
            />
          </React.Fragment>
        )
      }
    
      renderExports() {
    
        const { rows, multiselectedRows, exportOptions } = this.state
    
        const visibleColumns = this.controller.getVisibleColumns()
    
    
            exportable={exportable}
    
            exportOptions={exportOptions}
            multiselectable={multiselectable}
    
            onExport={this.controller.handleExport}
            onExportOptionsChange={this.controller.handleExportOptionsChange}
          />
        )
      }
    
    
      renderExportState() {
        const { exportState } = this.state
    
        if (!exportState) {
          return null
        }
    
        return (
          <React.Fragment>
            <GridExportLoading loading={!!exportState.loading} />
            <GridExportError
    
              error={exportState.error}
              onClose={this.controller.handleExportCancel}
            />
    
              onDownload={() =>
                this.controller.handleExportDownload(
                  exportState.fileName,
                  exportState.fileUrl
                )
              }
              onCancel={this.controller.handleExportCancel}
            />
          </React.Fragment>
        )
      }
    
    
      renderHeaders() {
        const { multiselectable } = this.props
        const { sortings, rows, multiselectedRows } = this.state
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
          <GridHeaders
            columns={visibleColumns}
            rows={rows}
            sortings={sortings}
            onSortChange={this.controller.handleSortChange}
            onMultiselectAllRowsChange={
              this.controller.handleMultiselectAllRowsChange
            }
            multiselectable={multiselectable}
            multiselectedRows={multiselectedRows}
          />
        )
      }
    
      renderFilters() {
    
    mpukhliak's avatar
    mpukhliak committed
        const { id, filterModes, multiselectable } = this.props
    
        const { filterMode, filters, globalFilter } = this.state
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
          <GridFilters
    
    mpukhliak's avatar
    mpukhliak committed
            id={id}
    
            columns={visibleColumns}
            filterModes={filterModes}
            filterMode={filterMode}
            filters={filters}
            onFilterChange={this.controller.handleFilterChange}
            onFilterModeChange={this.controller.handleFilterModeChange}
            globalFilter={globalFilter}
            onGlobalFilterChange={this.controller.handleGlobalFilterChange}
            multiselectable={multiselectable}
          />
        )
      }
    
    
        const { multiselectable, actions } = this.props
        const { rows, multiselectedRows } = this.state
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
    
            columns={visibleColumns}
            rows={rows}
            actions={actions}
            onExecuteAction={this.controller.handleExecuteAction}
            onMultiselectionClear={this.controller.handleMultiselectionClear}
            multiselectable={multiselectable}
            multiselectedRows={multiselectedRows}
          />
        )
      }
    
      renderRow(row) {
        const { selectable, multiselectable, onRowClick } = this.props
    
        const { selectedRow, multiselectedRows, heights } = this.state
    
    
        const visibleColumns = this.controller.getVisibleColumns()
    
        return (
          <GridRow
            key={row.id}
            columns={visibleColumns}
            row={row}
    
            clickable={onRowClick ? true : false}
            selectable={selectable}
            selected={selectedRow ? selectedRow.id === row.id : false}
            multiselectable={multiselectable}
            multiselected={multiselectedRows && multiselectedRows[row.id]}
            onClick={this.controller.handleRowClick}
            onSelect={this.controller.handleRowSelect}
            onMultiselect={this.controller.handleRowMultiselect}