1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
19 } from "@material-ui/core";
20 import classnames from "classnames";
21 import { DataColumn, SortDirection } from "./data-column";
22 import { DataTableDefaultView } from "../data-table-default-view/data-table-default-view";
23 import { DataTableFilters } from "../data-table-filters/data-table-filters-tree";
24 import { DataTableMultiselectPopover } from "../data-table-multiselect-popover/data-table-multiselect-popover";
25 import { DataTableFiltersPopover } from "../data-table-filters/data-table-filters-popover";
26 import { countNodes, getTreeDirty } from "models/tree";
27 import { IconType } from "components/icon/icon";
28 import { SvgIconProps } from "@material-ui/core/SvgIcon";
29 import ArrowDownwardIcon from "@material-ui/icons/ArrowDownward";
30 import { createTree } from "models/tree";
31 import { DataTableMultiselectOption } from "../data-table-multiselect-popover/data-table-multiselect-popover";
32 import { PendingIcon } from "components/icon/icon";
34 export type DataColumns<I, R> = Array<DataColumn<I, R>>;
36 export enum DataTableFetchMode {
41 export interface DataTableDataProps<I> {
43 columns: DataColumns<I, any>;
44 onRowClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
45 onContextMenu: (event: React.MouseEvent<HTMLElement>, item: I) => void;
46 onRowDoubleClick: (event: React.MouseEvent<HTMLTableRowElement>, item: I) => void;
47 onSortToggle: (column: DataColumn<I, any>) => void;
48 onFiltersChange: (filters: DataTableFilters, column: DataColumn<I, any>) => void;
49 extractKey?: (item: I) => React.Key;
51 defaultViewIcon?: IconType;
52 defaultViewMessages?: string[];
53 currentItemUuid?: string;
54 currentRoute?: string;
55 toggleMSToolbar: (isVisible: boolean) => void;
56 setCheckedListOnStore: (checkedList: TCheckedList) => void;
57 checkedList: TCheckedList;
74 | "tableCellWorkflows";
76 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
81 display: "inline-block",
85 background: theme.palette.background.paper,
89 padding: theme.spacing.unit,
96 paddingLeft: '0.9rem',
106 paddingLeft: '0.35rem',
107 paddingTop: '0.1rem',
108 marginLeft: '0.5rem',
115 wordWrap: "break-word",
116 paddingRight: "24px",
122 tableCellWorkflows: {
123 "&:nth-last-child(2)": {
129 paddingRight: "24px",
137 color: theme.palette.text.primary,
141 export type TCheckedList = Record<string, boolean>;
143 type DataTableState = {
148 type DataTableProps<T> = DataTableDataProps<T> & WithStyles<CssRules>;
150 export const DataTable = withStyles(styles)(
151 class Component<T> extends React.Component<DataTableProps<T>> {
152 state: DataTableState = {
157 componentDidMount(): void {
158 this.initializeCheckedList([]);
161 componentDidUpdate(prevProps: Readonly<DataTableProps<T>>, prevState: DataTableState) {
162 const { items, setCheckedListOnStore } = this.props;
163 const { isSelected } = this.state;
164 if (prevProps.items !== items) {
165 if (isSelected === true) this.setState({ isSelected: false });
166 if (items.length) this.initializeCheckedList(items);
167 else setCheckedListOnStore({});
169 if (prevProps.currentRoute !== this.props.currentRoute) {
170 this.initializeCheckedList([])
172 if(prevProps.working === true && this.props.working === false) {
173 this.setState({ isLoaded: true });
175 if((this.props.items.length > 0) && !this.state.isLoaded) {
176 this.setState({ isLoaded: true });
180 componentWillUnmount(): void {
181 this.initializeCheckedList([])
184 checkBoxColumn: DataColumn<any, any> = {
185 name: "checkBoxColumn",
188 filters: createTree(),
190 const { classes, checkedList } = this.props;
193 className={classes.clickBox}
196 this.handleSelectOne(uuid)
198 onDoubleClick={(ev) => ev.stopPropagation()}
201 data-cy={`multiselect-checkbox-${uuid}`}
204 className={classes.checkBox}
205 checked={checkedList && checkedList[uuid] ? checkedList[uuid] : false}
206 onChange={() => this.handleSelectOne(uuid)}
207 onDoubleClick={(ev) => ev.stopPropagation()}
214 multiselectOptions: DataTableMultiselectOption[] = [
215 { name: "All", fn: list => this.handleSelectAll(list) },
216 { name: "None", fn: list => this.handleSelectNone(list) },
217 { name: "Invert", fn: list => this.handleInvertSelect(list) },
220 initializeCheckedList = (uuids: any[]): void => {
221 const newCheckedList = { ...this.props.checkedList };
223 uuids.forEach(uuid => {
224 if (!newCheckedList.hasOwnProperty(uuid)) {
225 newCheckedList[uuid] = false;
228 for (const key in newCheckedList) {
229 if (!uuids.includes(key)) {
230 delete newCheckedList[key];
233 this.props.setCheckedListOnStore(newCheckedList);
236 isAllSelected = (list: TCheckedList): boolean => {
237 for (const key in list) {
238 if (list[key] === false) return false;
243 isAnySelected = (): boolean => {
244 const { checkedList } = this.props;
245 if (!Object.keys(checkedList).length) return false;
246 for (const key in checkedList) {
247 if (checkedList[key] === true) return true;
252 handleSelectOne = (uuid: string): void => {
253 const { checkedList } = this.props;
254 const newCheckedList = { ...checkedList };
255 newCheckedList[uuid] = !checkedList[uuid];
256 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
257 this.props.setCheckedListOnStore(newCheckedList);
260 handleSelectorSelect = (): void => {
261 const { checkedList } = this.props;
262 const { isSelected } = this.state;
263 isSelected ? this.handleSelectNone(checkedList) : this.handleSelectAll(checkedList);
266 handleSelectAll = (list: TCheckedList): void => {
267 if (Object.keys(list).length) {
268 const newCheckedList = { ...list };
269 for (const key in newCheckedList) {
270 newCheckedList[key] = true;
272 this.setState({ isSelected: true });
273 this.props.setCheckedListOnStore(newCheckedList);
277 handleSelectNone = (list: TCheckedList): void => {
278 const newCheckedList = { ...list };
279 for (const key in newCheckedList) {
280 newCheckedList[key] = false;
282 this.setState({ isSelected: false });
283 this.props.setCheckedListOnStore(newCheckedList);
286 handleInvertSelect = (list: TCheckedList): void => {
287 if (Object.keys(list).length) {
288 const newCheckedList = { ...list };
289 for (const key in newCheckedList) {
290 newCheckedList[key] = !list[key];
292 this.setState({ isSelected: this.isAllSelected(newCheckedList) });
293 this.props.setCheckedListOnStore(newCheckedList);
298 const { items, classes, columns, isNotFound } = this.props;
299 const { isLoaded } = this.state;
300 if (columns[0].name === this.checkBoxColumn.name) columns.shift();
301 columns.unshift(this.checkBoxColumn);
303 <div className={classes.root}>
304 <div className={classes.content}>
307 <TableRow>{this.mapVisibleColumns(this.renderHeadCell)}</TableRow>
309 <TableBody className={classes.tableBody}>{(isLoaded && !isNotFound) && items.map(this.renderBodyRow)}</TableBody>
311 {(!isLoaded || isNotFound || items.length === 0) && this.renderNoItemsPlaceholder(this.props.columns)}
317 renderNoItemsPlaceholder = (columns: DataColumns<T, any>) => {
318 const { isLoaded } = this.state;
319 const { working, isNotFound } = this.props;
320 const dirty = columns.some(column => getTreeDirty("")(column.filters));
321 if (isNotFound && isLoaded) {
323 <DataTableDefaultView
324 icon={this.props.defaultViewIcon}
325 messages={["No items found"]}
329 if (isLoaded === false || working === true) {
331 <DataTableDefaultView
333 messages={["Loading data, please wait"]}
337 // isLoaded && !working && !isNotFound
339 <DataTableDefaultView
340 icon={this.props.defaultViewIcon}
341 messages={this.props.defaultViewMessages}
342 filtersApplied={dirty}
348 renderHeadCell = (column: DataColumn<T, any>, index: number) => {
349 const { name, key, renderHeader, filters, sort } = column;
350 const { onSortToggle, onFiltersChange, classes, checkedList } = this.props;
351 const { isSelected } = this.state;
352 return column.name === "checkBoxColumn" ? (
355 className={classes.checkBoxCell}>
356 <div className={classes.checkBoxHead}>
357 <Tooltip title={this.state.isSelected ? "Deselect all" : "Select all"}>
360 className={classes.checkBox}
362 disabled={!this.props.items.length}
363 onChange={this.handleSelectorSelect}></input>
365 <DataTableMultiselectPopover
367 disabled={!this.props.items.length}
368 options={this.multiselectOptions}
369 checkedList={checkedList}></DataTableMultiselectPopover>
374 className={index === 1 ? classes.firstTableCell : classes.tableCell}
378 ) : countNodes(filters) > 0 ? (
379 <DataTableFiltersPopover
380 name={`${name} filters`}
381 mutuallyExclusive={column.mutuallyExclusiveFilters}
382 onChange={filters => onFiltersChange && onFiltersChange(filters, column)}
385 </DataTableFiltersPopover>
388 active={sort.direction !== SortDirection.NONE}
389 direction={sort.direction !== SortDirection.NONE ? sort.direction : undefined}
390 IconComponent={this.ArrowIcon}
392 onClick={() => onSortToggle && onSortToggle(column)}>
402 ArrowIcon = ({ className, ...props }: SvgIconProps) => (
405 className={this.props.classes.arrowButton}
409 className={classnames(className, this.props.classes.arrow)}
414 renderBodyRow = (item: any, index: number) => {
415 const { onRowClick, onRowDoubleClick, extractKey, classes, currentItemUuid, currentRoute } = this.props;
418 data-cy={'data-table-row'}
420 key={extractKey ? extractKey(item) : index}
421 onClick={event => onRowClick && onRowClick(event, item)}
422 onContextMenu={this.handleRowContextMenu(item)}
423 onDoubleClick={event => onRowDoubleClick && onRowDoubleClick(event, item)}
424 selected={item === currentItemUuid}>
425 {this.mapVisibleColumns((column, index) => (
427 key={column.key || index}
429 currentRoute === "/workflows"
430 ? classes.tableCellWorkflows
432 ? classes.checkBoxCell
433 : `${classes.tableCell} ${index === 1 ? classes.firstTableCell : ""}`
435 {column.render(item)}
442 mapVisibleColumns = (fn: (column: DataColumn<T, any>, index: number) => React.ReactElement<any>) => {
443 return this.props.columns.filter(column => column.selected).map(fn);
446 handleRowContextMenu = (item: T) => (event: React.MouseEvent<HTMLElement>) => this.props.onContextMenu(event, item);