Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLAN6-259 Widget Expanded Content Improvements #193

Merged
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
12 changes: 11 additions & 1 deletion cogboard-webapp/src/components/Widget/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export const mapStatusToColor = (status, theme) => theme.palette.status[status];
export const getWidgetOverflow = type =>
type !== 'TextWidget' ? 'visible' : 'hidden';

export const dispatchEvent = (customEvent, data) => {
if (customEvent) {
const Event = new CustomEvent(customEvent, { detail: data });
document.dispatchEvent(Event);
}
};

export const renderCardContent = (
content,
updateTimestamp,
Expand All @@ -19,7 +26,8 @@ export const renderCardContent = (
status,
expandContent,
expanded,
handleToggle
handleToggle,
closeWidgets
) => {
return (
<StyledCardContent>
Expand All @@ -42,6 +50,8 @@ export const renderCardContent = (
handleToggle={handleToggle}
content={content}
expandContent={expandContent}
closeWidgets={closeWidgets}
id={id}
/>
</StyledCardContent>
);
Expand Down
38 changes: 36 additions & 2 deletions cogboard-webapp/src/components/Widget/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import widgetTypes from '../widgets';
import { ItemTypes } from '../../constants';
import { getIsAuthenticated } from '../../selectors';
import { renderCardContent } from './helpers';
import { renderCardContent, dispatchEvent } from './helpers';

import { MenuItem } from '@material-ui/core';
import { StyledCard, StyledCardHeader, StyledCollapse } from './styled';
Expand All @@ -25,6 +25,10 @@ import ConfirmationDialog from '../ConfirmationDialog';
import StatusIcon from '../StatusIcon';
import { getWidgetStatus, getWidgetUpdateTime } from '../../utils/components';

const selectors = {
collapse: '[class*="-StyledCollapse"]'
};

const Widget = ({ id, index }) => {
const widgetData = useSelector(
({ widgets }) => widgets.widgetsById[id],
Expand Down Expand Up @@ -99,6 +103,20 @@ const Widget = ({ id, index }) => {

drag(drop(ref));

document.addEventListener('CloseAllWidgets', event => {
if (event.detail === id) {
return;
}
if (!expanded) {
return;
}
handleToggle();
});

const closeWidgets = widgetId => {
dispatchEvent('CloseAllWidgets', widgetId);
};

const handleEditClick = closeMenu => () => {
dispatch(loadSettings());
openDialog();
Expand All @@ -115,6 +133,20 @@ const Widget = ({ id, index }) => {
closeConfirmationDialog();
};

const handleCollapseScrollIntoView = () => {
const { top, height } = ref.current
.querySelectorAll(selectors.collapse)[0]
.getBoundingClientRect();
const collapseVerticalOffset = top + height;

if (collapseVerticalOffset > window.innerHeight) {
window.scrollTo({
top: collapseVerticalOffset,
behavior: 'smooth'
});
}
};

return (
<>
<StyledCard
Expand Down Expand Up @@ -178,7 +210,8 @@ const Widget = ({ id, index }) => {
widgetStatus,
expandContent,
expanded,
handleToggle
handleToggle,
closeWidgets
)}
{expandContent && (
<StyledCollapse
Expand All @@ -188,6 +221,7 @@ const Widget = ({ id, index }) => {
isDragging={isDragging}
in={expanded}
timeout="auto"
onEntered={() => handleCollapseScrollIntoView()}
unmountOnExit
>
<WidgetContent id={id} type={type} content={content} />
Expand Down
6 changes: 3 additions & 3 deletions cogboard-webapp/src/components/Widget/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const StyledCardContent = styled(CardContent)`
justify-content: space-between;

&:last-child {
padding-bottom: 16px;
padding-bottom: 8px;
}

.cardFootWrapper {
Expand All @@ -113,13 +113,13 @@ export const StyledCollapse = styled(
<Collapse {...props} />
)
)`
bottom: 1px;
bottom: 2px;
background-color: ${({ isDragging, status, theme }) =>
!isDragging
? mapStatusToColor(status, theme)
: theme.palette.background.paper};
box-shadow: ${({ isExpanded }) =>
isExpanded ? '2px 4px 4px 2px rgba(0,0,0,0.3)' : 'none'};
isExpanded ? '4px 4px 4px rgba(0,0,0,0.3)' : 'none'};
height: auto;
opacity: ${({ isExpanded }) => (isExpanded ? 1 : 0)};
padding: 0 16px 16px;
Expand Down
9 changes: 7 additions & 2 deletions cogboard-webapp/src/components/WidgetFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const WidgetFooter = ({
content,
expanded,
handleToggle,
expandContent
expandContent,
closeWidgets,
id
}) => {
return (
<div className="cardFootWrapper">
Expand All @@ -20,7 +22,10 @@ const WidgetFooter = ({
{expandContent && !content.errorMessage && (
<StyledIconButton
isExpanded={expanded}
onClick={handleToggle}
onClick={() => {
handleToggle();
closeWidgets(id);
}}
aria-expanded={expandContent}
aria-label="show more"
>
Expand Down