@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
195 lines (193 loc) • 7.66 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { styled } from '@mui/material/styles';
import MuiList from '@mui/material/List';
import MuiListItemAvatar from '@mui/material/ListItemAvatar';
import Checkbox from '@mui/material/Checkbox';
import MuiCheckbox from '@mui/material/Checkbox';
import ListItemText from '@mui/material/ListItemText';
import Skeleton from '@mui/material/Skeleton';
import Typography from '@mui/material/Typography';
import React from 'react';
import MuiListItem from '@mui/material/ListItem';
import MuiListItemIcon from '@mui/material/ListItemIcon';
import MuiListSubheader from '@mui/material/ListSubheader';
import CheckRounded from '@mui/icons-material/CheckRounded';
import Box from '@mui/material/Box';
import { UNDEFINED } from '../../utils/constants';
import { getInitials, toColor } from '../../utils/string';
import Avatar from '@mui/material/Avatar';
import { getPersonFullName } from '../SiteDashboard/utils';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import { FormattedMessage } from 'react-intl';
import { Pagination } from '../Pagination';
import { useDispatch } from 'react-redux';
import { getOffsetLeft, getOffsetTop } from '@mui/material/Popover';
import { showItemMegaMenu } from '../../state/actions/dialogs';
import IconButton from '@mui/material/IconButton';
import MoreVertRoundedIcon from '@mui/icons-material/MoreVertRounded';
import Tooltip from '@mui/material/Tooltip';
export const actionsToBeShown = [
'edit',
'delete',
'publish',
'approvePublish',
'requestPublish',
'rejectPublish',
'dependencies',
'history'
];
export const List = styled(MuiList)({ paddingTop: 0 });
export const ListSubheader = styled(MuiListSubheader)({ paddingLeft: 0, paddingRight: 0, lineHeight: 2 });
export const ListItem = styled(MuiListItem)({ padding: 0 });
export const ListItemIcon = styled(MuiListItemIcon)({ marginRight: 0 });
export const ListItemAvatar = styled(MuiListItemAvatar)({ minWidth: 50 });
export const DenseCheckbox = styled(MuiCheckbox)({ padding: '5px' });
export const DashletAvatar = styled(Avatar)({ width: 40, height: 40 });
export function PersonAvatar(props) {
const { person, sx } = props;
const backgroundColor = toColor(person.username);
return React.createElement(DashletAvatar, {
src: person.avatar ?? UNDEFINED,
children: person.avatar ? UNDEFINED : getInitials(person),
sx: { backgroundColor, color: (theme) => theme.palette.getContrastText(backgroundColor), ...sx }
});
}
export const getItemSkeleton = ({ numOfItems = 1, showCheckbox = false, showAvatar = false }) =>
React.createElement(
List,
null,
new Array(numOfItems)
.fill(null)
.map((nothing, index) =>
React.createElement(
ListItem,
{ key: index, sx: { pl: 2, pr: 2 } },
showCheckbox &&
React.createElement(ListItemIcon, null, React.createElement(Checkbox, { edge: 'start', disabled: true })),
showAvatar && React.createElement(ListItemAvatar, null, React.createElement(DashletAvatar, null)),
React.createElement(ListItemText, {
primary: React.createElement(Skeleton, { variant: 'text' }),
secondary: React.createElement(
Typography,
{ color: 'text.secondary', variant: 'body2' },
React.createElement(Skeleton, { variant: 'text' })
)
})
)
)
);
export const DashletEmptyMessage = ({ children, sx }) =>
React.createElement(
Box,
{ display: 'flex', flexDirection: 'column', alignItems: 'center', sx: { mt: 2, ...sx } },
React.createElement(CheckRounded, { sx: { color: 'success.main', mb: 1 } }),
React.createElement(Typography, { color: 'text.secondary', variant: 'body2' }, children)
);
export function PersonFullName({ person }) {
return React.createElement(Typography, { variant: 'h6' }, getPersonFullName(person));
}
export function Pager(props) {
const { totalPages, totalItems, currentPage, rowsPerPage, onPagePickerChange, onPageChange, onRowsPerPageChange } =
props;
return React.createElement(
React.Fragment,
null,
React.createElement(
Select,
{
variant: 'standard',
disableUnderline: true,
value: `${currentPage}`,
onChange: (e) => onPagePickerChange(parseInt(e.target.value)),
sx: { fontSize: (theme) => theme.typography.fontSize }
},
new Array(totalPages)
.fill(null)
.map((nothing, index) =>
React.createElement(
MenuItem,
{ value: index, sx: { fontSize: (theme) => theme.typography.fontSize }, key: index },
currentPage === index
? React.createElement(FormattedMessage, {
defaultMessage: 'Page {pageNumber}',
values: { pageNumber: index + 1 }
})
: React.createElement(FormattedMessage, {
defaultMessage: 'Go to page {pageNumber}',
values: { pageNumber: index + 1 }
})
)
)
),
React.createElement(Pagination, {
count: totalItems,
onPageChange: (e, page) => onPageChange(page),
page: currentPage,
rowsPerPage: rowsPerPage,
onRowsPerPageChange: (e) => onRowsPerPageChange(parseInt(e.target.value)),
mode: 'table'
})
);
}
export function DashletItemOptions(props) {
const { path, iconButtonProps } = props;
const dispatch = useDispatch();
const onOpenItemMegaMenu = (element) => {
const anchorRect = element.getBoundingClientRect();
const top = anchorRect.top + getOffsetTop(anchorRect, 'top');
const left = anchorRect.left + getOffsetLeft(anchorRect, 'left');
dispatch(
showItemMegaMenu({
path,
anchorReference: 'anchorPosition',
anchorPosition: { top, left }
})
);
};
return React.createElement(
Tooltip,
{ title: React.createElement(FormattedMessage, { defaultMessage: 'Options' }) },
React.createElement(
IconButton,
{
size: 'small',
onClick: (e) => {
e.stopPropagation();
onOpenItemMegaMenu(e.currentTarget);
},
...iconButtonProps
},
React.createElement(MoreVertRoundedIcon, null)
)
);
}