1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Grid, Paper, Toolbar, StyleRulesCallback, withStyles, WithStyles, TablePagination, IconButton, Tooltip, Button } from '@material-ui/core';
7 import { ColumnSelector } from "~/components/column-selector/column-selector";
8 import { DataTable, DataColumns, DataTableFetchMode } from "~/components/data-table/data-table";
9 import { DataColumn, SortDirection } from "~/components/data-table/data-column";
10 import { SearchInput } from '~/components/search-input/search-input';
11 import { ArvadosTheme } from "~/common/custom-theme";
12 import { createTree } from '~/models/tree';
13 import { DataTableFilters } from '~/components/data-table-filters/data-table-filters-tree';
14 import { MoreOptionsIcon } from '~/components/icon/icon';
15 import { PaperProps } from '@material-ui/core/Paper';
17 type CssRules = 'searchBox' | "toolbar" | "footer" | "root" | 'moreOptionsButton';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 paddingBottom: theme.spacing.unit * 2
24 paddingTop: theme.spacing.unit * 2
37 interface DataExplorerDataProps<T> {
38 fetchMode: DataTableFetchMode;
40 itemsAvailable: number;
41 columns: DataColumns<T>;
44 rowsPerPageOptions: number[];
46 contextMenuColumn: boolean;
47 dataTableDefaultView?: React.ReactNode;
49 hideColumnSelector?: boolean;
50 paperProps?: PaperProps;
51 actions?: React.ReactNode;
52 hideSearchInput?: boolean;
56 interface DataExplorerActionProps<T> {
57 onSetColumns: (columns: DataColumns<T>) => void;
58 onSearch: (value: string) => void;
59 onRowClick: (item: T) => void;
60 onRowDoubleClick: (item: T) => void;
61 onColumnToggle: (column: DataColumn<T>) => void;
62 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
63 onSortToggle: (column: DataColumn<T>) => void;
64 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
65 onChangePage: (page: number) => void;
66 onChangeRowsPerPage: (rowsPerPage: number) => void;
67 onLoadMore: (page: number) => void;
68 extractKey?: (item: T) => React.Key;
71 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
73 export const DataExplorer = withStyles(styles)(
74 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
76 if (this.props.onSetColumns) {
77 this.props.onSetColumns(this.props.columns);
82 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
83 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
84 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
85 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
88 return <Paper className={classes.root} {...paperProps} key={paperKey}>
89 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={classes.toolbar}>
90 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
91 {!hideSearchInput && <div className={classes.searchBox}>
94 onSearch={onSearch} />
97 {!hideColumnSelector && <ColumnSelector
99 onColumnToggle={onColumnToggle} />}
103 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
105 onRowClick={(_, item: T) => onRowClick(item)}
106 onContextMenu={onContextMenu}
107 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
108 onFiltersChange={onFiltersChange}
109 onSortToggle={onSortToggle}
110 extractKey={extractKey}
112 defaultView={dataTableDefaultView} />
113 <Toolbar className={classes.footer}>
114 <Grid container justify="flex-end">
115 {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
116 count={itemsAvailable}
117 rowsPerPage={rowsPerPage}
118 rowsPerPageOptions={rowsPerPageOptions}
119 page={this.props.page}
120 onChangePage={this.changePage}
121 onChangeRowsPerPage={this.changeRowsPerPage}
122 component="div" /> : <Button
125 onClick={this.loadMore}
132 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
133 this.props.onChangePage(page);
136 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
137 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
141 this.props.onLoadMore(this.props.page + 1);
144 renderContextMenuTrigger = (item: T) =>
145 <Grid container justify="center">
146 <Tooltip title="More options" disableFocusListener>
147 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
153 contextMenuColumn: DataColumn<any> = {
157 filters: createTree(),
158 key: "context-actions",
159 render: this.renderContextMenuTrigger