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';
24 type CssRules = 'root' | 'button' | 'buttonIcon' | 'content' | 'tabsWrapper' | 'tabsRoot' | 'tabs';
26 const styles: StyleRulesCallback<CssRules> = theme => ({
36 padding: '2px 0px 2px 5px',
56 interface MPVHideablePanelDataProps {
62 panelRef?: MutableRefObject<any>;
65 interface MPVHideablePanelActionProps {
66 doHidePanel: () => void;
67 doMaximizePanel: () => void;
68 doUnMaximizePanel: () => void;
71 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
73 const MPVHideablePanel = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, name, visible, maximized, illuminated, ...props }: MPVHideablePanelProps) =>
76 {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })}
80 interface MPVPanelDataProps {
82 panelMaximized?: boolean;
83 panelIlluminated?: boolean;
84 panelRef?: MutableRefObject<any>;
85 forwardProps?: boolean;
90 interface MPVPanelActionProps {
91 doHidePanel?: () => void;
92 doMaximizePanel?: () => void;
93 doUnMaximizePanel?: () => void;
96 // Props received by panel implementors
97 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
99 type MPVPanelContentProps = { children: ReactElement } & MPVPanelProps & GridProps;
101 // Grid item compatible component for layout and MPV props passing
102 export const MPVPanelContent = ({ doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName,
103 panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight, minHeight,
104 ...props }: MPVPanelContentProps) => {
106 if (panelRef && panelRef.current) {
107 panelRef.current.scrollIntoView({ alignToTop: true });
111 const maxH = panelMaximized
115 return <Grid item style={{ maxHeight: maxH, minHeight }} {...props}>
116 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
117 <Paper style={{ height: '100%' }} elevation={panelIlluminated ? 8 : 0}>
119 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized })
125 export interface MPVPanelState {
129 interface MPVContainerDataProps {
130 panelStates?: MPVPanelState[];
131 mutuallyExclusive?: boolean;
133 type MPVContainerProps = MPVContainerDataProps & GridProps;
135 // Grid container compatible component that also handles panel toggling.
136 const MPVContainerComponent = ({ children, panelStates, classes, ...props }: MPVContainerProps & WithStyles<CssRules>) => {
137 if (children === undefined || children === null || children === {}) {
139 } else if (!isArray(children)) {
140 children = [children];
142 const initialVisibility = (children as ReactNodeArray).map((_, idx) =>
143 !panelStates || // if panelStates wasn't passed, default to all visible panels
145 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
146 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(initialVisibility);
147 const [previousPanelVisibility, setPreviousPanelVisibility] = useState<boolean[]>(initialVisibility);
148 const [highlightedPanel, setHighlightedPanel] = useState<number>(-1);
149 const currentSelectedPanel = panelVisibility.findIndex(Boolean);
150 const [selectedPanel, setSelectedPanel] = useState<number>(-1);
151 const panelRef = useRef<any>(null);
153 let panels: JSX.Element[] = [];
154 let buttons: JSX.Element[] = [];
155 let tabs: JSX.Element[] = [];
156 let buttonBar: JSX.Element = <></>;
158 if (isArray(children)) {
159 const showFn = (idx: number) => () => {
160 setPreviousPanelVisibility(initialVisibility);
161 if (props.mutuallyExclusive) {
162 // Hide all other panels
164 ...(new Array(idx).fill(false)),
166 ...(new Array(panelVisibility.length-(idx+1)).fill(false)),
170 ...panelVisibility.slice(0, idx),
172 ...panelVisibility.slice(idx + 1)
175 setSelectedPanel(idx);
177 const hideFn = (idx: number) => () => {
178 setPreviousPanelVisibility(initialVisibility);
180 ...panelVisibility.slice(0, idx),
182 ...panelVisibility.slice(idx+1)
185 const maximizeFn = (idx: number) => () => {
186 setPreviousPanelVisibility(panelVisibility);
187 // Maximize X == hide all but X
189 ...panelVisibility.slice(0, idx).map(() => false),
191 ...panelVisibility.slice(idx+1).map(() => false),
194 const unMaximizeFn = (idx: number) => () => {
195 setPanelVisibility(previousPanelVisibility);
196 setSelectedPanel(idx);
198 for (let idx = 0; idx < children.length; idx++) {
199 const panelName = panelStates === undefined
201 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx + 1}`;
202 const btnVariant = panelVisibility[idx]
205 const btnTooltip = panelVisibility[idx]
207 : `Open ${panelName} panel`;
208 const panelIsMaximized = panelVisibility[idx] &&
209 panelVisibility.filter(e => e).length === 1;
213 <Tooltip title={btnTooltip} disableFocusListener>
214 <Button variant={btnVariant} size="small" color="primary"
215 className={classNames(classes.button)}
216 onMouseEnter={() => {
217 setHighlightedPanel(idx);
219 onMouseLeave={() => {
220 setHighlightedPanel(-1);
222 onClick={showFn(idx)}>
234 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
235 panelRef={(idx === selectedPanel) ? panelRef : undefined}
236 maximized={panelIsMaximized} illuminated={idx === highlightedPanel}
237 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)} doUnMaximizePanel={panelIsMaximized ? unMaximizeFn(idx) : () => null}>
240 panels = [...panels, aPanel];
243 buttonBar = props.mutuallyExclusive ?
244 <Paper className={classes.tabsWrapper}>
245 <Tabs className={classes.tabsRoot} value={currentSelectedPanel} onChange={(e, val) => showFn(val)()}>
246 {tabs.map((tgl, idx) => <Tab className={classes.tabs} key={idx} label={tgl} />)}
250 {buttons.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>)}
254 return <Grid container {...props} className={classNames(classes.root, props.className)}>
255 <Grid container item direction="row">
258 <Grid container item {...props} xs className={classes.content}
259 onScroll={() => setSelectedPanel(-1)}>
260 {panelVisibility.includes(true)
262 : <Grid container item alignItems='center' justify='center'>
263 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
269 export const MPVContainer = withStyles(styles)(MPVContainerComponent);