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';
16 } from "@material-ui/core";
17 import { GridProps } from '@material-ui/core/Grid';
18 import { isArray } from 'lodash';
19 import { DefaultView } from 'components/default-view/default-view';
20 import { InfoIcon } from 'components/icon/icon';
21 import { ReactNodeArray } from 'prop-types';
22 import classNames from 'classnames';
26 | 'exclusiveGridContainerRoot'
32 | 'exclusiveContentPaper'
35 const styles: StyleRulesCallback<CssRules> = theme => ({
39 exclusiveGridContainerRoot: {
43 paddingTop: '0 !important',
48 flexDirection: 'column',
56 padding: '2px 0px 2px 5px',
63 exclusiveContentPaper: {
70 borderBottom: `1px solid ${theme.palette.grey[300]}`,
74 interface MPVHideablePanelDataProps {
80 panelRef?: MutableRefObject<any>;
81 paperClassName?: string;
84 interface MPVHideablePanelActionProps {
85 doHidePanel: () => void;
86 doMaximizePanel: () => void;
87 doUnMaximizePanel: () => void;
90 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
92 const MPVHideablePanel = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, name, visible, maximized, illuminated, paperClassName, ...props }: MPVHideablePanelProps) =>
95 {React.cloneElement((props.children as ReactElement), {
100 panelMaximized: maximized,
101 panelIlluminated: illuminated,
102 panelRef: props.panelRef,
108 interface MPVPanelDataProps {
110 panelMaximized?: boolean;
111 panelIlluminated?: boolean;
112 panelRef?: MutableRefObject<any>;
113 forwardProps?: boolean;
116 paperClassName?: string;
119 interface MPVPanelActionProps {
120 doHidePanel?: () => void;
121 doMaximizePanel?: () => void;
122 doUnMaximizePanel?: () => void;
125 // Props received by panel implementors
126 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
128 type MPVPanelContentProps = { children: ReactElement } & MPVPanelProps & GridProps;
130 // Grid item compatible component for layout and MPV props passing
131 export const MPVPanelContent = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName,
132 panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight, minHeight, paperClassName,
133 ...props }: MPVPanelContentProps) => {
135 if (panelRef && panelRef.current) {
136 panelRef.current.scrollIntoView({ alignToTop: true });
140 const maxH = panelMaximized
144 return <Grid item style={{ maxHeight: maxH, minHeight }} {...props}>
145 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
146 <Paper style={{ height: '100%' }} elevation={panelIlluminated ? 8 : 0}>
148 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized, paperClassName })
149 : React.cloneElement(props.children, { paperClassName })}
154 export interface MPVPanelState {
158 interface MPVContainerDataProps {
159 panelStates?: MPVPanelState[];
160 mutuallyExclusive?: boolean;
162 type MPVContainerProps = MPVContainerDataProps & GridProps;
164 // Grid container compatible component that also handles panel toggling.
165 const MPVContainerComponent = ({ children, panelStates, classes, ...props }: MPVContainerProps & WithStyles<CssRules>) => {
166 if (children === undefined || children === null || Object.keys(children).length === 0) {
168 } else if (!isArray(children)) {
169 children = [children];
171 const initialVisibility = (children as ReactNodeArray).map((_, idx) =>
172 !panelStates || // if panelStates wasn't passed, default to all visible panels
174 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
175 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(initialVisibility);
176 const [previousPanelVisibility, setPreviousPanelVisibility] = useState<boolean[]>(initialVisibility);
177 const [highlightedPanel, setHighlightedPanel] = useState<number>(-1);
178 const currentSelectedPanel = panelVisibility.findIndex(Boolean);
179 const [selectedPanel, setSelectedPanel] = useState<number>(-1);
180 const panelRef = useRef<any>(null);
182 let panels: JSX.Element[] = [];
183 let buttons: JSX.Element[] = [];
184 let tabs: JSX.Element[] = [];
185 let buttonBar: JSX.Element = <></>;
187 if (isArray(children)) {
188 const showFn = (idx: number) => () => {
189 setPreviousPanelVisibility(initialVisibility);
190 if (props.mutuallyExclusive) {
191 // Hide all other panels
193 ...(new Array(idx).fill(false)),
195 ...(new Array(panelVisibility.length-(idx+1)).fill(false)),
199 ...panelVisibility.slice(0, idx),
201 ...panelVisibility.slice(idx + 1)
204 setSelectedPanel(idx);
206 const hideFn = (idx: number) => () => {
207 setPreviousPanelVisibility(initialVisibility);
209 ...panelVisibility.slice(0, idx),
211 ...panelVisibility.slice(idx+1)
214 const maximizeFn = (idx: number) => () => {
215 setPreviousPanelVisibility(panelVisibility);
216 // Maximize X == hide all but X
218 ...panelVisibility.slice(0, idx).map(() => false),
220 ...panelVisibility.slice(idx+1).map(() => false),
223 const unMaximizeFn = (idx: number) => () => {
224 setPanelVisibility(previousPanelVisibility);
225 setSelectedPanel(idx);
227 for (let idx = 0; idx < children.length; idx++) {
228 const panelName = panelStates === undefined
230 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx + 1}`;
231 const btnVariant = panelVisibility[idx]
234 const btnTooltip = panelVisibility[idx]
236 : `Open ${panelName} panel`;
237 const panelIsMaximized = panelVisibility[idx] &&
238 panelVisibility.filter(e => e).length === 1;
242 <Tooltip title={btnTooltip} disableFocusListener>
243 <Button variant={btnVariant} size="small" color="primary"
244 className={classNames(classes.button)}
245 onMouseEnter={() => {
246 setHighlightedPanel(idx);
248 onMouseLeave={() => {
249 setHighlightedPanel(-1);
251 onClick={showFn(idx)}>
265 visible={panelVisibility[idx]}
267 paperClassName={props.mutuallyExclusive ? classes.exclusiveContentPaper : undefined}
268 panelRef={(idx === selectedPanel) ? panelRef : undefined}
269 maximized={panelIsMaximized} illuminated={idx === highlightedPanel}
270 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)} doUnMaximizePanel={panelIsMaximized ? unMaximizeFn(idx) : () => null}>
273 panels = [...panels, aPanel];
276 buttonBar = props.mutuallyExclusive ?
277 <Tabs value={currentSelectedPanel} onChange={(e, val) => showFn(val)()} data-cy={"mpv-tabs"}>
278 {tabs.map((tgl, idx) => <Tab className={classes.tabs} key={idx} label={tgl} />)}
280 <Grid container item direction="row">
281 {buttons.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>)}
285 const content = <Grid container item {...props} xs className={classes.content}
286 onScroll={() => setSelectedPanel(-1)}>
287 {panelVisibility.includes(true)
289 : <Grid container item alignItems='center' justify='center'>
290 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
294 if (props.mutuallyExclusive) {
295 return <Grid container {...props} className={classNames(classes.exclusiveGridContainerRoot, props.className)}>
296 <Grid item {...props} className={classes.gridItemRoot}>
297 <Paper className={classes.paperRoot}>
304 return <Grid container {...props} className={classNames(classes.gridContainerRoot, props.className)}>
311 export const MPVContainer = withStyles(styles)(MPVContainerComponent);