1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { MutableRefObject, ReactElement, ReactNode, useEffect, useRef, useState } from 'react';
6 import { CustomStyleRulesCallback } from 'common/custom-theme';
7 import { Button, Grid, Paper, Tooltip, Tabs, Tab } from "@mui/material";
8 import { WithStyles } from '@mui/styles';
9 import withStyles from '@mui/styles/withStyles';
10 import { GridProps } from '@mui/material/Grid';
11 import { isArray } from 'lodash';
12 import { DefaultView } from 'components/default-view/default-view';
13 import { InfoIcon } from 'components/icon/icon';
14 import classNames from 'classnames';
19 | 'exclusiveGridContainerRoot'
25 | 'exclusiveContentPaper'
27 | 'buttonBarGridContainer'
30 const styles: CustomStyleRulesCallback<CssRules> = theme => ({
35 margin: '10px -4px -4px',
36 width: 'calc(100% + 8px) !important',
38 exclusiveGridContainerRoot: {
42 paddingTop: '0 !important',
47 flexDirection: 'column',
55 padding: '2px 0px 2px 5px',
61 padding: '4px !important',
67 exclusiveContentPaper: {
70 buttonBarGridContainer: {
71 padding: '4px !important',
77 borderBottom: `1px solid ${theme.palette.grey[300]}`,
81 interface MPVHideablePanelDataProps {
87 panelRef?: MutableRefObject<any>;
88 paperClassName?: string;
91 interface MPVHideablePanelActionProps {
92 doHidePanel: () => void;
93 doMaximizePanel: () => void;
94 doUnMaximizePanel: () => void;
97 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
99 const MPVHideablePanel = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, name, visible, maximized, illuminated, paperClassName, ...props }: MPVHideablePanelProps) =>
102 {React.cloneElement((props.children as ReactElement), {
107 panelMaximized: maximized,
108 panelIlluminated: illuminated,
109 panelRef: props.panelRef,
115 interface MPVPanelDataProps {
117 panelMaximized?: boolean;
118 panelIlluminated?: boolean;
119 panelRef?: MutableRefObject<any>;
120 forwardProps?: boolean;
123 paperClassName?: string;
126 interface MPVPanelActionProps {
127 doHidePanel?: () => void;
128 doMaximizePanel?: () => void;
129 doUnMaximizePanel?: () => void;
132 // Props received by panel implementors
133 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
135 type MPVPanelContentProps = { children: ReactElement } & MPVPanelProps & GridProps;
137 // Grid item compatible component for layout and MPV props passing
138 export const MPVPanelContent = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName,
139 panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight, minHeight, paperClassName,
140 ...props }: MPVPanelContentProps) => {
142 if (panelRef && panelRef.current) {
143 panelRef.current.scrollIntoView({ alignToTop: true });
147 const maxH = panelMaximized
151 return <Grid item style={{ maxHeight: maxH, minHeight, padding: '4px' }} {...props}>
152 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
153 <Paper style={{ height: '100%' }} elevation={panelIlluminated ? 8 : 0}>
155 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized, paperClassName })
156 : React.cloneElement(props.children, { paperClassName })}
161 export interface MPVPanelState {
165 interface MPVContainerDataProps {
166 panelStates?: MPVPanelState[];
167 mutuallyExclusive?: boolean;
169 type MPVContainerProps = MPVContainerDataProps & GridProps;
171 // Grid container compatible component that also handles panel toggling.
172 const MPVContainerComponent = ({ children, panelStates, classes, ...props }: MPVContainerProps & WithStyles<CssRules>) => {
173 if (children === undefined || children === null || Object.keys(children).length === 0) {
175 } else if (!isArray(children)) {
176 children = [children];
178 const initialVisibility = (children as ReactNode[]).map((_, idx) =>
179 !panelStates || // if panelStates wasn't passed, default to all visible panels
181 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
182 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(initialVisibility);
183 const [previousPanelVisibility, setPreviousPanelVisibility] = useState<boolean[]>(initialVisibility);
184 const [highlightedPanel, setHighlightedPanel] = useState<number>(-1);
185 const currentSelectedPanel = panelVisibility.findIndex(Boolean);
186 const [selectedPanel, setSelectedPanel] = useState<number>(-1);
187 const panelRef = useRef<any>(null);
189 let panels: JSX.Element[] = [];
190 let buttons: JSX.Element[] = [];
191 let tabs: JSX.Element[] = [];
192 let buttonBar: JSX.Element = <></>;
194 if (isArray(children)) {
195 const showFn = (idx: number) => () => {
196 setPreviousPanelVisibility(initialVisibility);
197 if (props.mutuallyExclusive) {
198 // Hide all other panels
200 ...(new Array(idx).fill(false)),
202 ...(new Array(panelVisibility.length-(idx+1)).fill(false)),
206 ...panelVisibility.slice(0, idx),
208 ...panelVisibility.slice(idx + 1)
211 setSelectedPanel(idx);
213 const hideFn = (idx: number) => () => {
214 setPreviousPanelVisibility(initialVisibility);
216 ...panelVisibility.slice(0, idx),
218 ...panelVisibility.slice(idx+1)
221 const maximizeFn = (idx: number) => () => {
222 setPreviousPanelVisibility(panelVisibility);
223 // Maximize X == hide all but X
225 ...panelVisibility.slice(0, idx).map(() => false),
227 ...panelVisibility.slice(idx+1).map(() => false),
230 const unMaximizeFn = (idx: number) => () => {
231 setPanelVisibility(previousPanelVisibility);
232 setSelectedPanel(idx);
234 for (let idx = 0; idx < children.length; idx++) {
235 const panelName = panelStates === undefined
237 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx + 1}`;
238 const btnVariant = panelVisibility[idx]
241 const btnTooltip = panelVisibility[idx]
243 : `Open ${panelName} panel`;
244 const panelIsMaximized = panelVisibility[idx] &&
245 panelVisibility.filter(e => e).length === 1;
249 <Tooltip title={btnTooltip} disableFocusListener>
250 <Button variant={btnVariant} size="small" color="primary"
251 className={classNames(classes.button)}
252 onMouseEnter={() => {
253 setHighlightedPanel(idx);
255 onMouseLeave={() => {
256 setHighlightedPanel(-1);
258 onClick={showFn(idx)}>
272 visible={panelVisibility[idx]}
274 paperClassName={props.mutuallyExclusive ? classes.exclusiveContentPaper : undefined}
275 panelRef={(idx === selectedPanel) ? panelRef : undefined}
276 maximized={panelIsMaximized} illuminated={idx === highlightedPanel}
277 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)} doUnMaximizePanel={panelIsMaximized ? unMaximizeFn(idx) : () => null}>
280 panels = [...panels, aPanel];
283 buttonBar = props.mutuallyExclusive ?
284 <Tabs value={currentSelectedPanel} onChange={(e, val) => showFn(val)()} data-cy={"mpv-tabs"}>
285 {tabs.map((tgl, idx) => <Tab className={classes.tabs} key={idx} label={tgl} />)}
287 <Grid container item direction="row" className={classes.buttonBarGridContainer}>
288 {buttons.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>)}
292 const content = <Grid container item {...props} xs className={props.mutuallyExclusive ? classes.exclusiveContent : classes.content}
293 onScroll={() => setSelectedPanel(-1)}>
294 {panelVisibility.includes(true)
296 : <Grid container item alignItems='center' justifyContent='center'>
297 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
301 if (props.mutuallyExclusive) {
302 return <Grid container {...props} className={classNames(classes.exclusiveGridContainerRoot, props.className)}>
303 <Grid item {...props} className={classes.gridItemRoot}>
304 <Paper className={classes.paperRoot}>
311 return <Grid container {...props} className={classNames(classes.gridContainerRoot, props.className)}>
318 export const MPVContainer = withStyles(styles)(MPVContainerComponent);