2023-01-05 22:47:17 +01:00
|
|
|
import {Table, TableBody, TableCell, TableHead, TableRow} from "@material-ui/core";
|
|
|
|
import ArrowUpwardIcon from "@material-ui/icons/ArrowUpward";
|
|
|
|
import ArrowDownwardIcon from "@material-ui/icons/ArrowDownward";
|
|
|
|
import React, {useCallback, useContext, useEffect, useState} from "react";
|
|
|
|
import "./data-table.css";
|
|
|
|
import {LocaleContext} from "../locale";
|
|
|
|
import clsx from "clsx";
|
2023-02-09 23:55:30 +01:00
|
|
|
import {Box, IconButton, Select, TextField} from "@mui/material";
|
2023-01-20 12:16:18 +01:00
|
|
|
import {formatDate, formatDateTime} from "../util";
|
2023-01-16 21:47:23 +01:00
|
|
|
import CachedIcon from "@material-ui/icons/Cached";
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
export function DataTable(props) {
|
|
|
|
|
|
|
|
const { className, placeholder,
|
2023-01-16 21:47:23 +01:00
|
|
|
columns, data, pagination,
|
2023-01-05 22:47:17 +01:00
|
|
|
fetchData, onClick, onFilter,
|
|
|
|
defaultSortColumn, defaultSortOrder,
|
2023-01-16 21:47:23 +01:00
|
|
|
title, ...other } = props;
|
2023-01-05 22:47:17 +01:00
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
const {translate: L} = useContext(LocaleContext);
|
2023-01-05 22:47:17 +01:00
|
|
|
|
2023-01-07 15:34:05 +01:00
|
|
|
const [doFetchData, setFetchData] = useState(false);
|
2023-01-05 22:47:17 +01:00
|
|
|
const [sortAscending, setSortAscending] = useState(["asc","ascending"].includes(defaultSortOrder?.toLowerCase));
|
|
|
|
const [sortColumn, setSortColumn] = useState(defaultSortColumn || null);
|
2023-01-20 12:16:18 +01:00
|
|
|
const sortable = !!fetchData && (props.hasOwnProperty("sortable") ? !!props.sortable : true);
|
2023-01-16 21:47:23 +01:00
|
|
|
const onRowClick = onClick || (() => {});
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
const onFetchData = useCallback((force = false) => {
|
2023-01-20 12:16:18 +01:00
|
|
|
if (fetchData) {
|
|
|
|
if (doFetchData || force) {
|
|
|
|
setFetchData(false);
|
|
|
|
const orderBy = columns[sortColumn]?.field || null;
|
|
|
|
const sortOrder = sortAscending ? "asc" : "desc";
|
|
|
|
fetchData(pagination.getPage(), pagination.getPageSize(), orderBy, sortOrder);
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
2023-01-20 12:16:18 +01:00
|
|
|
}, [fetchData, doFetchData, columns, sortColumn, sortAscending, pagination]);
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
// pagination changed?
|
|
|
|
useEffect(() => {
|
2023-01-20 12:16:18 +01:00
|
|
|
if (pagination) {
|
|
|
|
let forceFetch = false;
|
|
|
|
if (pagination.getPageSize() < pagination.getTotal()) {
|
|
|
|
// page size is smaller than the total count
|
|
|
|
forceFetch = true;
|
|
|
|
} else if (data?.length && pagination.getPageSize() >= data.length && data.length < pagination.getTotal()) {
|
|
|
|
// page size is greater than the current visible count but there were hidden rows before
|
|
|
|
forceFetch = true;
|
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
|
2023-01-20 12:16:18 +01:00
|
|
|
onFetchData(forceFetch);
|
|
|
|
}
|
|
|
|
}, [pagination?.data?.pageSize, pagination?.data?.current]);
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
// sorting changed
|
|
|
|
useEffect(() => {
|
|
|
|
onFetchData(true);
|
|
|
|
}, [sortAscending, sortColumn]);
|
|
|
|
|
|
|
|
let headerRow = [];
|
|
|
|
const onChangeSort = useCallback((index, column) => {
|
|
|
|
if (sortable && column.sortable) {
|
|
|
|
if (sortColumn === index) {
|
|
|
|
setSortAscending(!sortAscending);
|
|
|
|
} else {
|
|
|
|
setSortColumn(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [onFetchData, sortColumn, sortAscending]);
|
|
|
|
|
|
|
|
for (const [index, column] of columns.entries()) {
|
|
|
|
if (!(column instanceof DataColumn)) {
|
|
|
|
throw new Error("DataTable can only have DataColumn-objects as column definition, got: " + typeof column);
|
2023-01-22 12:32:18 +01:00
|
|
|
} else if (column.hidden) {
|
|
|
|
continue;
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sortable && column.sortable) {
|
2023-01-16 21:47:23 +01:00
|
|
|
headerRow.push(<TableCell key={"col-" + index} className={"data-table-clickable"}
|
2023-01-05 22:47:17 +01:00
|
|
|
title={L("general.sort_by") + ": " + column.label}
|
2023-01-16 21:47:23 +01:00
|
|
|
onClick={() => onChangeSort(index, column)}
|
|
|
|
align={column.align}>
|
2023-01-05 22:47:17 +01:00
|
|
|
{sortColumn === index ? (sortAscending ? <ArrowUpwardIcon /> : <ArrowDownwardIcon />): <></>}{column.renderHead(index)}
|
|
|
|
</TableCell>);
|
|
|
|
} else {
|
2023-01-16 21:47:23 +01:00
|
|
|
headerRow.push(<TableCell key={"col-" + index} align={column.align}>
|
2023-01-05 22:47:17 +01:00
|
|
|
{column.renderHead(index)}
|
|
|
|
</TableCell>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const numColumns = columns.length;
|
2023-01-19 18:12:16 +01:00
|
|
|
let numRows = 0;
|
2023-01-05 22:47:17 +01:00
|
|
|
let rows = [];
|
2023-01-16 21:47:23 +01:00
|
|
|
if (data && data?.length) {
|
2023-01-19 18:12:16 +01:00
|
|
|
numRows = data.length;
|
2023-01-16 21:47:23 +01:00
|
|
|
for (const [rowIndex, entry] of data.entries()) {
|
2023-01-05 22:47:17 +01:00
|
|
|
let row = [];
|
|
|
|
for (const [index, column] of columns.entries()) {
|
2023-01-16 21:47:23 +01:00
|
|
|
row.push(<TableCell key={"col-" + index} align={column.align}>
|
|
|
|
{column.renderData(L, entry, index)}
|
|
|
|
</TableCell>);
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
rows.push(<TableRow className={clsx({["data-table-clickable"]: typeof onClick === 'function'})}
|
2023-01-19 18:12:16 +01:00
|
|
|
onClick={(e) => ["tr","td"].includes(e.target.tagName.toLowerCase()) && onRowClick(rowIndex, entry)}
|
2023-01-16 21:47:23 +01:00
|
|
|
key={"row-" + rowIndex}>
|
|
|
|
{ row }
|
|
|
|
</TableRow>);
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
} else if (placeholder) {
|
|
|
|
rows.push(<TableRow key={"row-placeholder"}>
|
2023-01-19 18:12:16 +01:00
|
|
|
<TableCell colSpan={numColumns} align={"center"}>
|
2023-01-05 22:47:17 +01:00
|
|
|
{ placeholder }
|
|
|
|
</TableCell>
|
|
|
|
</TableRow>);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Box position={"relative"}>
|
2023-01-20 12:16:18 +01:00
|
|
|
{title ?
|
|
|
|
<h3>
|
|
|
|
{fetchData ?
|
|
|
|
<IconButton onClick={() => onFetchData(true)}>
|
|
|
|
<CachedIcon/>
|
|
|
|
</IconButton>
|
|
|
|
: <></>
|
|
|
|
}
|
|
|
|
{title}
|
|
|
|
</h3> : <></>
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
<Table className={clsx("data-table", className)} size="small" {...other}>
|
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
|
|
|
{ headerRow }
|
|
|
|
</TableRow>
|
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
{ rows }
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
2023-01-20 12:16:18 +01:00
|
|
|
{pagination && pagination.renderPagination(L, numRows)}
|
2023-01-05 22:47:17 +01:00
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DataColumn {
|
2023-01-16 21:47:23 +01:00
|
|
|
constructor(label, field = null, params = {}) {
|
2023-01-05 22:47:17 +01:00
|
|
|
this.label = label;
|
|
|
|
this.field = field;
|
2023-01-16 21:47:23 +01:00
|
|
|
this.sortable = !params.hasOwnProperty("sortable") || !!params.sortable;
|
|
|
|
this.align = params.align || "left";
|
2023-01-22 12:32:18 +01:00
|
|
|
this.hidden = !!params.hidden;
|
2023-01-18 14:37:34 +01:00
|
|
|
this.params = params;
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
renderData(L, entry, index) {
|
2023-01-19 18:12:16 +01:00
|
|
|
return typeof this.field === 'function' ? this.field(entry) : entry[this.field];
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
renderHead() {
|
|
|
|
return this.label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class StringColumn extends DataColumn {
|
2023-01-16 21:47:23 +01:00
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
2023-01-18 14:37:34 +01:00
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
|
|
|
let data = super.renderData(L, entry, index);
|
|
|
|
|
2023-01-22 12:32:18 +01:00
|
|
|
if (this.params.maxLength && data?.length && data.length > this.params.maxLength) {
|
|
|
|
data = data.substring(0, this.params.maxLength) + "...";
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:37:34 +01:00
|
|
|
if (this.params.style) {
|
2023-01-22 12:32:18 +01:00
|
|
|
let style = (typeof this.params.style === 'function'
|
|
|
|
? this.params.style(entry) : this.params.style);
|
|
|
|
data = <span style={style}>{data}</span>
|
2023-01-18 14:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-03-05 15:30:06 +01:00
|
|
|
export class ArrayColumn extends DataColumn {
|
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
this.seperator = params.seperator || ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
|
|
|
let data = super.renderData(L, entry, index);
|
|
|
|
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
data = Object.values(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data.join(this.seperator);
|
|
|
|
|
|
|
|
if (this.params.style) {
|
|
|
|
let style = (typeof this.params.style === 'function'
|
|
|
|
? this.params.style(entry) : this.params.style);
|
|
|
|
data = <span style={style}>{data}</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SecretsColumn extends DataColumn {
|
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
this.asteriskCount = params.asteriskCount || 8;
|
|
|
|
this.character = params.character || "*";
|
|
|
|
this.canCopy = params.hasOwnProperty("canCopy") ? params.canCopy : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
|
|
|
let originalData = super.renderData(L, entry, index);
|
|
|
|
if (!originalData) {
|
|
|
|
return "(None)";
|
|
|
|
}
|
|
|
|
|
|
|
|
let properties = this.params.properties || {};
|
|
|
|
properties.className = clsx(properties.className, "font-monospace");
|
|
|
|
|
|
|
|
if (this.canCopy) {
|
|
|
|
properties.title = L("Click to copy");
|
|
|
|
properties.className = clsx(properties.className, "data-table-clickable");
|
|
|
|
properties.onClick = () => {
|
|
|
|
navigator.clipboard.writeText(originalData);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return <span {...properties}>{this.character.repeat(this.asteriskCount)}</span>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
export class NumericColumn extends DataColumn {
|
2023-01-16 21:47:23 +01:00
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
this.decimalDigits = params.decimalDigits || null;
|
|
|
|
this.integerDigits = params.integerDigits || null;
|
|
|
|
this.prefix = params.prefix || "";
|
|
|
|
this.suffix = params.suffix || "";
|
|
|
|
this.decimalChar = params.decimalChar || ".";
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
renderData(L, entry, index) {
|
2023-02-09 23:55:30 +01:00
|
|
|
let number = super.renderData(L, entry, index).toString();
|
2023-01-16 21:47:23 +01:00
|
|
|
|
|
|
|
if (this.decimalDigits !== null) {
|
|
|
|
number = number.toFixed(this.decimalDigits);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.integerDigits !== null) {
|
|
|
|
let currentLength = number.split(".")[0].length;
|
|
|
|
if (currentLength < this.integerDigits) {
|
|
|
|
number = number.padStart(this.integerDigits - currentLength, "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.decimalChar !== ".") {
|
|
|
|
number = number.replace(".", this.decimalChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prefix + number + this.suffix;
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DateTimeColumn extends DataColumn {
|
2023-01-16 21:47:23 +01:00
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
this.precise = !!params.precise;
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
renderData(L, entry, index) {
|
2023-02-09 23:55:30 +01:00
|
|
|
let date = super.renderData(L, entry, index);
|
|
|
|
return date ? formatDateTime(L, date, this.precise) : "";
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
|
2023-01-20 12:16:18 +01:00
|
|
|
export class DateColumn extends DataColumn {
|
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
2023-02-09 23:55:30 +01:00
|
|
|
let date = super.renderData(L, entry, index);
|
|
|
|
return date ? formatDate(L, date) : "";
|
2023-01-20 12:16:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:37:34 +01:00
|
|
|
export class BoolColumn extends DataColumn {
|
2023-01-16 21:47:23 +01:00
|
|
|
constructor(label, field = null, params = {}) {
|
|
|
|
super(label, field, params);
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
renderData(L, entry, index) {
|
2023-02-09 23:55:30 +01:00
|
|
|
let data = super.renderData(L, entry, index);
|
2023-01-18 16:36:29 +01:00
|
|
|
return L(data ? "general.yes" : "general.no");
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 12:32:18 +01:00
|
|
|
export class InputColumn extends DataColumn {
|
|
|
|
constructor(label, field, type, onChange, params = {}) {
|
|
|
|
super(label, field, { ...params, sortable: false });
|
|
|
|
this.type = type;
|
|
|
|
this.onChange = onChange;
|
|
|
|
this.props = params.props || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
|
|
|
let value = super.renderData(L, entry, index);
|
2023-02-09 23:55:30 +01:00
|
|
|
let inputProps = typeof this.props === 'function' ? this.props(entry, index) : this.props;
|
2023-01-22 12:32:18 +01:00
|
|
|
if (this.type === 'text') {
|
2023-02-09 23:55:30 +01:00
|
|
|
return <TextField {...inputProps} size={"small"} fullWidth={true}
|
2023-01-22 12:32:18 +01:00
|
|
|
value={value} onChange={(e) => this.onChange(entry, index, e.target.value)} />
|
2023-02-09 23:55:30 +01:00
|
|
|
} else if (this.type === "select") {
|
|
|
|
let options = Object.entries(this.params.options || {}).map(([value, label]) =>
|
|
|
|
<option key={"option-" + value} value={value}>{label}</option>);
|
|
|
|
return <Select native {...inputProps} size={"small"} fullWidth={true} value={value}
|
|
|
|
onChange={(e) => this.onChange(entry, index, e.target.value)}>
|
|
|
|
{options}
|
|
|
|
</Select>
|
2023-01-22 12:32:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return <>[Invalid type: {this.type}]</>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
export class ControlsColumn extends DataColumn {
|
2023-01-18 16:36:29 +01:00
|
|
|
constructor(label, buttons = [], params = {}) {
|
|
|
|
super(label, null, { align: "center", ...params, sortable: false });
|
2023-01-16 21:47:23 +01:00
|
|
|
this.buttons = buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderData(L, entry, index) {
|
|
|
|
let buttonElements = [];
|
|
|
|
for (const [index, button] of this.buttons.entries()) {
|
2023-01-19 18:12:16 +01:00
|
|
|
let element = typeof button.element === 'function'
|
|
|
|
? button.element(entry, index)
|
|
|
|
: button.element;
|
|
|
|
|
|
|
|
let buttonProps = {};
|
|
|
|
if (typeof button.props === 'function') {
|
|
|
|
buttonProps = button.props(entry, index);
|
|
|
|
} else {
|
|
|
|
buttonProps = button.props;
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
let props = {
|
2023-01-19 18:12:16 +01:00
|
|
|
...buttonProps,
|
2023-01-16 21:47:23 +01:00
|
|
|
key: "button-" + index,
|
2023-01-19 18:12:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:55:30 +01:00
|
|
|
// TODO: icon button!
|
2023-01-19 18:12:16 +01:00
|
|
|
if (button.hasOwnProperty("disabled")) {
|
|
|
|
props.disabled = typeof button.disabled === 'function'
|
|
|
|
? button.disabled(entry, index)
|
|
|
|
: button.disabled;
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:55:30 +01:00
|
|
|
if (!props.disabled) {
|
|
|
|
props.onClick = (e) => { e.stopPropagation(); button.onClick(entry, index); }
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:12:16 +01:00
|
|
|
if ((!button.hasOwnProperty("hidden")) ||
|
|
|
|
(typeof button.hidden === 'function' && !button.hidden(entry, index)) ||
|
|
|
|
(!button.hidden)) {
|
2023-01-16 21:47:23 +01:00
|
|
|
buttonElements.push(React.createElement(element, props))
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 09:51:46 +01:00
|
|
|
|
2023-01-25 14:15:34 +01:00
|
|
|
return <Box className={"data-table-buttons"}>
|
2023-01-16 21:47:23 +01:00
|
|
|
{buttonElements}
|
2023-01-25 14:15:34 +01:00
|
|
|
</Box>
|
2023-01-14 09:51:46 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|