Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions frontend/src/components/dialogs/EditEnumDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
DialogContent as MuiDialogContent,
DialogTitle as MuiDialogTitle,
IconButton,
Menu,
MenuItem,
styled,
Tooltip,
useTheme
Expand All @@ -13,24 +15,29 @@ import {
ArrowDropDown,
ArrowDropUp,
Close as CloseIcon,
MoreHoriz
MoreHoriz,
SortByAlpha
} from '@mui/icons-material';
import {
getDialogTableHeight,
getPageSizeOptionsOnDialog,
getTableHeaderBackgroundColor,
getTableRowBackgroundColor,
moveDown,
movedRow,
moveUp,
removeRow,
replaceRow
replaceRow,
sortedRows,
withoutHeaderSorting
} from "../../utils/MaterialTableHelper";
import MaterialTable from '@material-table/core';
import PropTypes from 'prop-types';
import EditIconDialog from "./SelectIconDialog";
import {MessageBox} from "../utils/MessageBox";
import {getLastPageSizeOnDialog, setLastPageSizeOnDialog} from "../../utils/ConfigurationStorage";
import StyledIcon from "../utils/StyledIcon";
import DraggableBodyRow from "../utils/DraggableBodyRow";

const PREFIX = 'EditEnumDialog';

Expand Down Expand Up @@ -91,6 +98,19 @@ export default function EditEnumDialog(props) {
const [data, setData] = useState(props.items != null ? props.items : []);
// the rows per page of the dialogs, remembered
const [pageSize, setPageSize] = useState(getLastPageSizeOnDialog);
// the Sort menu: sorts the items for good (the order of the list in the data); opened at the position of the
// button, because material-table renders its actions again and the button element itself doesn't stay
const [sortMenuPosition, setSortMenuPosition] = useState(null);

const openSortMenu = event => {
const rect = event.currentTarget.getBoundingClientRect();
setSortMenuPosition({top: rect.bottom, left: rect.left});
}

const sortItems = (field, descending) => {
setData(sortedRows(data, field, descending));
setSortMenuPosition(null);
}

const getColumns = () => {
let columnsArray = [];
Expand All @@ -112,7 +132,7 @@ export default function EditEnumDialog(props) {
editComponent: EditComponent
});
}
return columnsArray;
return withoutHeaderSorting(columnsArray);
}

const handleClickOpen = () => {
Expand Down Expand Up @@ -186,7 +206,6 @@ export default function EditEnumDialog(props) {
paginationType: 'stepped',
pageSizeOptions: getPageSizeOptionsOnDialog(),
actionsColumnIndex: -1,
maxColumnSort: 1,
selection: false,
filtering: false,
padding: 'dense',
Expand All @@ -202,7 +221,9 @@ export default function EditEnumDialog(props) {
})
}}
components={{
Container: props => <div {...props} />
Container: props => <div {...props} />,
Row: props => <DraggableBodyRow {...props}
onMoveRow={(from, to) => setData(movedRow(data, from, to))}/>
}}
editable={{
onRowAdd: newData =>
Expand Down Expand Up @@ -230,6 +251,13 @@ export default function EditEnumDialog(props) {
}),
}}
actions={[
{
icon: () => <SortByAlpha/>,
tooltip: 'Sort items',
isFreeAction: true,
disabled: data.length < 2,
onClick: openSortMenu
},
rowData => ({
icon: () => <ArrowDropDown/>,
tooltip: 'Move down',
Expand All @@ -247,6 +275,17 @@ export default function EditEnumDialog(props) {
})
]}
/>
<Menu
anchorReference='anchorPosition'
anchorPosition={sortMenuPosition ?? undefined}
open={sortMenuPosition != null}
onClose={() => setSortMenuPosition(null)}
>
<MenuItem onClick={() => sortItems('value', false)}>Value: A → Z</MenuItem>
<MenuItem onClick={() => sortItems('value', true)}>Value: Z → A</MenuItem>
<MenuItem onClick={() => sortItems('text', false)}>Text: A → Z</MenuItem>
<MenuItem onClick={() => sortItems('text', true)}>Text: Z → A</MenuItem>
</Menu>
</DialogContent>
</Dialog>
<MessageBox
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/components/utils/ActionPropertiesTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
getTableHeaderBackgroundColor,
getTableRowBackgroundColor,
moveDown,
movedRow,
moveUp,
withoutHeaderSorting,
removeRow,
replaceRow
} from "../../utils/MaterialTableHelper";
Expand All @@ -17,6 +19,7 @@ import {
import ArrowDropDown from "@mui/icons-material/ArrowDropDown";
import ArrowDropUp from "@mui/icons-material/ArrowDropUp";
import MaterialTable, {MTableEditField} from '@material-table/core';
import DraggableBodyRow from "./DraggableBodyRow";
import React, {useContext, useEffect, useState} from "react";
import EnumsContext from "../../context/enums/EnumsContext";
import {useTheme} from "@mui/material";
Expand Down Expand Up @@ -212,7 +215,7 @@ export default function ActionPropertiesTable(props) {
<MaterialTable
title={'Modify properties:'}
tableRef={tableRef}
columns={[
columns={withoutHeaderSorting([
{
title: '#',
cellStyle: {width: '1%'},
Expand Down Expand Up @@ -262,12 +265,11 @@ export default function ActionPropertiesTable(props) {
return (<div/>);
}
}
]}
])}
data={data}
options={{
paging: false,
actionsColumnIndex: -1,
maxColumnSort: 1,
selection: false,
search: true,
filtering: false,
Expand All @@ -278,7 +280,9 @@ export default function ActionPropertiesTable(props) {
rowStyle: rowData => ({backgroundColor: getTableRowBackgroundColor(rowData, theme)})
}}
components={{
Container: props => <div {...props} />
Container: props => <div {...props} />,
Row: props => <DraggableBodyRow {...props}
onMoveRow={(from, to) => setData(movedRow(data, from, to))}/>
}}
editable={getEditable()}
actions={[
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/components/utils/ColumnsTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
getTableHeaderBackgroundColor,
getTableRowBackgroundColor,
moveDown,
movedRow,
moveUp,
withoutHeaderSorting,
removeRow,
replaceRow
} from "../../utils/MaterialTableHelper";
Expand All @@ -15,6 +17,7 @@ import {
} from "../../utils/JsonHelper";
import {ArrowDropDown, ArrowDropUp} from "@mui/icons-material";
import MaterialTable from '@material-table/core';
import DraggableBodyRow from "./DraggableBodyRow";
import React, {useEffect, useState} from "react";
import {
getLastPageSizeOnDialog,
Expand Down Expand Up @@ -65,7 +68,7 @@ export default function ColumnsTable(props) {
<MaterialTable
title={'Class origin or defined properties:'}
tableRef={tableRef}
columns={[
columns={withoutHeaderSorting([
{
title: '#',
cellStyle: {width: '1%'},
Expand Down Expand Up @@ -143,7 +146,7 @@ export default function ColumnsTable(props) {
initialEditValue: true,
cellStyle: {width: '1%'}
}
]}
])}
data={columns}
onRowsPerPageChange={onChangeRowsPerPage}
options={{
Expand All @@ -152,7 +155,6 @@ export default function ColumnsTable(props) {
paginationType: 'stepped',
pageSizeOptions: getPageSizeOptionsOnDialog(),
actionsColumnIndex: -1,
maxColumnSort: 1,
selection: false,
filtering: false,
padding: 'dense',
Expand All @@ -170,7 +172,9 @@ export default function ColumnsTable(props) {
})
}}
components={{
Container: props => <div {...props} />
Container: props => <div {...props} />,
Row: props => <DraggableBodyRow {...props}
onMoveRow={(from, to) => setData(movedRow(columns, from, to))}/>
}}
editable={{
onRowAdd: newData =>
Expand Down
57 changes: 57 additions & 0 deletions frontend/src/components/utils/DraggableBodyRow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, {useState} from 'react';
import {MTableBodyRow} from '@material-table/core';
import {useTheme} from '@mui/material';

// The position (in the data) of the row being dragged; one drag at a time, so shared by all the rows
let draggedIndex = null;

// A row of a material-table that can be dragged onto another row to move it there (besides the Move up/down arrows).
// Use: components={{Row: props => <DraggableBodyRow {...props} onMoveRow={(from, to) => setData(movedRow(data, from, to))}/>}}
// Native HTML drag and drop: material-table passes the other props on to the <tr>. The row isn't draggable while a row
// is edited (the inputs need the mouse).
export default function DraggableBodyRow({onMoveRow, ...props}) {
const theme = useTheme();
const [dropMarker, setDropMarker] = useState(null); // 'top' | 'bottom' | null
const index = props.data.tableData.index;
const draggable = !props.hasAnyEditingRow;

const line = `inset 0 ${dropMarker === 'top' ? 2 : -2}px 0 ${theme.palette.primary.main}`;

return (
<MTableBodyRow
{...props}
draggable={draggable}
sx={{
cursor: draggable ? 'grab' : undefined,
boxShadow: dropMarker != null ? line : undefined
}}
onDragStart={event => {
draggedIndex = index;
event.dataTransfer.effectAllowed = 'move';
// Firefox starts a drag only with some data set
event.dataTransfer.setData('text/plain', '');
}}
onDragOver={event => {
if (draggedIndex == null || draggedIndex === index) {
return;
}
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
setDropMarker(draggedIndex > index ? 'top' : 'bottom');
}}
onDragLeave={() => setDropMarker(null)}
onDrop={event => {
event.preventDefault();
setDropMarker(null);
if (draggedIndex != null && draggedIndex !== index) {
onMoveRow(draggedIndex, index);
}
draggedIndex = null;
}}
onDragEnd={() => {
draggedIndex = null;
setDropMarker(null);
}}
/>
);
}
15 changes: 8 additions & 7 deletions frontend/src/components/utils/PropertiesTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import {
getTableHeaderBackgroundColor,
getTableRowBackgroundColor,
moveDown,
movedRow,
moveUp,
withoutHeaderSorting,
removeRow,
replaceRow
} from "../../utils/MaterialTableHelper";
import SelectEnumDialog from "../dialogs/SelectEnumDialog";
import {isItemChanged, uuidV4, validateItem} from "../../utils/JsonHelper";
import {ArrowDropDown, ArrowDropUp} from "@mui/icons-material";
import MaterialTable from '@material-table/core';
import DraggableBodyRow from "./DraggableBodyRow";
import React, {useEffect, useState} from "react";
import {
getLastPageSizeOnDialog,
Expand Down Expand Up @@ -79,15 +82,12 @@ export default function PropertiesTable(props) {
return '';
}

console.log("data:");
console.log(data);

return !delay && (
<div>
<MaterialTable
title={props.title}
tableRef={tableRef}
columns={[
columns={withoutHeaderSorting([
{
title: '#',
cellStyle: {width: '1%'},
Expand Down Expand Up @@ -135,7 +135,7 @@ export default function PropertiesTable(props) {
) : <div/>
},
{title: 'UUID', field: 'uuid', hidden: true, type: 'string'}
]}
])}
data={data}
onRowsPerPageChange={onChangeRowsPerPage}
options={{
Expand All @@ -145,7 +145,6 @@ export default function PropertiesTable(props) {
paginationType: 'stepped',
pageSizeOptions: getPageSizeOptionsOnDialog(),
actionsColumnIndex: -1,
maxColumnSort: 1,
selection: false,
filtering: false,
padding: 'dense',
Expand All @@ -163,7 +162,9 @@ export default function PropertiesTable(props) {
})
}}
components={{
Container: props => <div {...props} />
Container: props => <div {...props} />,
Row: props => <DraggableBodyRow {...props}
onMoveRow={(from, to) => setData(movedRow(data, from, to))}/>
}}
editable={{
onRowAdd: newData =>
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/utils/MaterialTableHelper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ export const moveDown = (dataCollection, itemId) => {
return swappedArray(dataCollection, itemId, itemId + 1);
}

// Rows sorted for good by a text field: natural order ("item2" before "item10"), letters with diacritics in place, a
// stable sort (rows with the same value keep their order)
const naturalCollator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});

export const sortedRows = (rows, field, descending = false) =>
rows.map(withoutTableData).sort((a, b) => {
const result = naturalCollator.compare(String(a[field] ?? ''), String(b[field] ?? ''));
return descending ? -result : result;
});

// Rows with the row at position "from" moved to position "to" (dragging a row onto another one)
export const movedRow = (rows, from, to) => {
const newRows = [...rows];
const [row] = newRows.splice(from, 1);
newRows.splice(to, 0, row);
return newRows;
}

// Columns of a table whose rows have an order of their own (Move up/down, dragging): the headers don't sort, because
// that would only sort the view, while the arrows and dragging move the rows in the data (options.sorting is not
// enough in material-table 6)
export const withoutHeaderSorting = columns => columns.map(column => ({...column, sorting: false}));

function swappedArray(arr, index1, index2) {
const newArr = [...arr];
[newArr[index1], newArr[index2]] = [newArr[index2], newArr[index1]];
Expand Down
Loading