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" | "toolbarUnderTitle" | "footer" | "root" | 'moreOptionsButton' | 'title';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 paddingBottom: theme.spacing.unit * 2
24 paddingTop: theme.spacing.unit * 2
39 paddingLeft: theme.spacing.unit * 3,
40 paddingTop: theme.spacing.unit * 3,
45 interface DataExplorerDataProps<T> {
46 fetchMode: DataTableFetchMode;
48 itemsAvailable: number;
49 columns: DataColumns<T>;
52 rowsPerPageOptions: number[];
54 contextMenuColumn: boolean;
55 dataTableDefaultView?: React.ReactNode;
57 hideColumnSelector?: boolean;
58 paperProps?: PaperProps;
59 actions?: React.ReactNode;
60 hideSearchInput?: boolean;
61 title?: React.ReactNode;
63 currentItemUuid: string;
66 interface DataExplorerActionProps<T> {
67 onSetColumns: (columns: DataColumns<T>) => void;
68 onSearch: (value: string) => void;
69 onRowClick: (item: T) => void;
70 onRowDoubleClick: (item: T) => void;
71 onColumnToggle: (column: DataColumn<T>) => void;
72 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: T) => void;
73 onSortToggle: (column: DataColumn<T>) => void;
74 onFiltersChange: (filters: DataTableFilters, column: DataColumn<T>) => void;
75 onChangePage: (page: number) => void;
76 onChangeRowsPerPage: (rowsPerPage: number) => void;
77 onLoadMore: (page: number) => void;
78 extractKey?: (item: T) => React.Key;
81 type DataExplorerProps<T> = DataExplorerDataProps<T> & DataExplorerActionProps<T> & WithStyles<CssRules>;
83 export const DataExplorer = withStyles(styles)(
84 class DataExplorerGeneric<T> extends React.Component<DataExplorerProps<T>> {
86 if (this.props.onSetColumns) {
87 this.props.onSetColumns(this.props.columns);
92 columns, onContextMenu, onFiltersChange, onSortToggle, working, extractKey,
93 rowsPerPage, rowsPerPageOptions, onColumnToggle, searchValue, onSearch,
94 items, itemsAvailable, onRowClick, onRowDoubleClick, classes,
95 dataTableDefaultView, hideColumnSelector, actions, paperProps, hideSearchInput,
96 paperKey, fetchMode, currentItemUuid, title
98 return <Paper className={classes.root} {...paperProps} key={paperKey}>
99 {title && <div className={classes.title}>{title}</div>}
100 {(!hideColumnSelector || !hideSearchInput) && <Toolbar className={title ? classes.toolbarUnderTitle : classes.toolbar}>
101 <Grid container justify="space-between" wrap="nowrap" alignItems="center">
102 <div className={classes.searchBox}>
103 {!hideSearchInput && <SearchInput
105 onSearch={onSearch} />}
108 {!hideColumnSelector && <ColumnSelector
110 onColumnToggle={onColumnToggle} />}
114 columns={this.props.contextMenuColumn ? [...columns, this.contextMenuColumn] : columns}
116 onRowClick={(_, item: T) => onRowClick(item)}
117 onContextMenu={onContextMenu}
118 onRowDoubleClick={(_, item: T) => onRowDoubleClick(item)}
119 onFiltersChange={onFiltersChange}
120 onSortToggle={onSortToggle}
121 extractKey={extractKey}
123 defaultView={dataTableDefaultView}
124 currentItemUuid={currentItemUuid}
125 currentRoute={paperKey} />
126 <Toolbar className={classes.footer}>
127 <Grid container justify="flex-end">
128 {fetchMode === DataTableFetchMode.PAGINATED ? <TablePagination
129 count={itemsAvailable}
130 rowsPerPage={rowsPerPage}
131 rowsPerPageOptions={rowsPerPageOptions}
132 page={this.props.page}
133 onChangePage={this.changePage}
134 onChangeRowsPerPage={this.changeRowsPerPage}
135 component="div" /> : <Button
138 onClick={this.loadMore}
145 changePage = (event: React.MouseEvent<HTMLButtonElement>, page: number) => {
146 this.props.onChangePage(page);
149 changeRowsPerPage: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (event) => {
150 this.props.onChangeRowsPerPage(parseInt(event.target.value, 10));
154 this.props.onLoadMore(this.props.page + 1);
157 renderContextMenuTrigger = (item: T) =>
158 <Grid container justify="center">
159 <Tooltip title="More options" disableFocusListener>
160 <IconButton className={this.props.classes.moreOptionsButton} onClick={event => this.props.onContextMenu(event, item)}>
166 contextMenuColumn: DataColumn<any> = {
170 filters: createTree(),
171 key: "context-actions",
172 render: this.renderContextMenuTrigger