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 } 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;
54 currentItemUuid: string;
57 interface DataExplorerActionProps<T> {
58 onSetColumns: (columns: DataColumns<T>) => void;
59 onSearch: (value: string) => void;
60 onRowClick: (item: T) => void;
61 onRowDoubleClick: (item: T) => void;
62 onColumnToggle: (column: DataColumn<T>) => void;
63 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
64 onSortToggle: (column: DataColumn<T>) => void;
65 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
66 onChangePage: (page: number) => void;
67 onChangeRowsPerPage: (rowsPerPage: number) => void;
68 onLoadMore: (page: number) => void;
69 extractKey?: (item: T) => React.Key;
72 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
74 export const DataExplorer = withStyles(styles)(
75 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
77 if (this.props.onSetColumns) {
78 this.props.onSetColumns(this.props.columns);
83 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
84 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
85 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
86 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
87 paperKey, fetchMode, currentItemUuid
89 return <Paper className={classes.root} {...paperProps} key={paperKey}>
90 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={classes.toolbar}>
91 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
92 {!hideSearchInput && <div className={classes.searchBox}>
95 onSearch={onSearch} />
98 {!hideColumnSelector && <ColumnSelector
100 onColumnToggle={onColumnToggle} />}
104 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
106 onRowClick={(_, item: T) => onRowClick(item)}
107 onContextMenu={onContextMenu}
108 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
109 onFiltersChange={onFiltersChange}
110 onSortToggle={onSortToggle}
111 extractKey={extractKey}
113 defaultView={dataTableDefaultView}
114 currentItemUuid={currentItemUuid}
115 currentRoute={paperKey} />
116 <Toolbar className={classes.footer}>
117 <Grid container justify="flex-end">
118 {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
119 count={itemsAvailable}
120 rowsPerPage={rowsPerPage}
121 rowsPerPageOptions={rowsPerPageOptions}
122 page={this.props.page}
123 onChangePage={this.changePage}
124 onChangeRowsPerPage={this.changeRowsPerPage}
125 component="div" /> : <Button
128 onClick={this.loadMore}
135 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
136 this.props.onChangePage(page);
139 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
140 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
144 this.props.onLoadMore(this.props.page + 1);
147 renderContextMenuTrigger = (item: T) =>
148 <Grid container justify="center">
149 <Tooltip title="More options" disableFocusListener>
150 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
156 contextMenuColumn: DataColumn<any> = {
160 filters: createTree(),
161 key: "context-actions",
162 render: this.renderContextMenuTrigger