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';
14 } from "@material-ui/core";
15 import { GridProps } from '@material-ui/core/Grid';
16 import { isArray } from 'lodash';
17 import { DefaultView } from 'components/default-view/default-view';
18 import { InfoIcon } from 'components/icon/icon';
19 import { ReactNodeArray } from 'prop-types';
20 import classNames from 'classnames';
22 type CssRules = 'button' | 'buttonIcon' | 'content';
24 const styles: StyleRulesCallback<CssRules> = theme => ({
31 padding: '2px 0px 2px 5px',
39 interface MPVHideablePanelDataProps {
45 panelRef?: MutableRefObject<any>;
48 interface MPVHideablePanelActionProps {
49 doHidePanel: () => void;
50 doMaximizePanel: () => void;
51 doUnMaximizePanel: () => void;
54 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
56 const MPVHideablePanel = ({doHidePanel, doMaximizePanel, doUnMaximizePanel, name, visible, maximized, illuminated, ...props}: MPVHideablePanelProps) =>
59 {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })}
63 interface MPVPanelDataProps {
65 panelMaximized?: boolean;
66 panelIlluminated?: boolean;
67 panelRef?: MutableRefObject<any>;
68 forwardProps?: boolean;
73 interface MPVPanelActionProps {
74 doHidePanel?: () => void;
75 doMaximizePanel?: () => void;
76 doUnMaximizePanel?: () => void;
79 // Props received by panel implementors
80 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
82 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
84 // Grid item compatible component for layout and MPV props passing
85 export const MPVPanelContent = ({doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName,
86 panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight, minHeight,
87 ...props}: MPVPanelContentProps) => {
89 if (panelRef && panelRef.current) {
90 panelRef.current.scrollIntoView({alignToTop: true});
94 const maxH = panelMaximized
98 return <Grid item style={{maxHeight: maxH, minHeight}} {...props}>
99 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
100 <Paper style={{height: '100%'}} elevation={panelIlluminated ? 8 : 0}>
102 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized })
108 export interface MPVPanelState {
112 interface MPVContainerDataProps {
113 panelStates?: MPVPanelState[];
115 type MPVContainerProps = MPVContainerDataProps & GridProps;
117 // Grid container compatible component that also handles panel toggling.
118 const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVContainerProps & WithStyles<CssRules>) => {
119 if (children === undefined || children === null || children === {}) {
121 } else if (!isArray(children)) {
122 children = [children];
124 const initialVisibility = (children as ReactNodeArray).map((_, idx) =>
125 !panelStates || // if panelStates wasn't passed, default to all visible panels
127 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
128 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(initialVisibility);
129 const [previousPanelVisibility, setPreviousPanelVisibility] = useState<boolean[]>(initialVisibility);
130 const [highlightedPanel, setHighlightedPanel] = useState<number>(-1);
131 const [selectedPanel, setSelectedPanel] = useState<number>(-1);
132 const panelRef = useRef<any>(null);
134 let panels: JSX.Element[] = [];
135 let buttons: JSX.Element[] = [];
137 if (isArray(children)) {
138 for (let idx = 0; idx < children.length; idx++) {
139 const showFn = (idx: number) => () => {
140 setPreviousPanelVisibility(initialVisibility);
142 ...panelVisibility.slice(0, idx),
144 ...panelVisibility.slice(idx+1)
146 setSelectedPanel(idx);
148 const hideFn = (idx: number) => () => {
149 setPreviousPanelVisibility(initialVisibility);
151 ...panelVisibility.slice(0, idx),
153 ...panelVisibility.slice(idx+1)
156 const maximizeFn = (idx: number) => () => {
157 setPreviousPanelVisibility(panelVisibility);
158 // Maximize X == hide all but X
160 ...panelVisibility.slice(0, idx).map(() => false),
162 ...panelVisibility.slice(idx+1).map(() => false),
165 const unMaximizeFn = (idx: number) => () => {
166 setPanelVisibility(previousPanelVisibility);
167 setSelectedPanel(idx);
169 const panelName = panelStates === undefined
171 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
172 const btnVariant = panelVisibility[idx]
175 const btnTooltip = panelVisibility[idx]
177 :`Open ${panelName} panel`;
178 const panelIsMaximized = panelVisibility[idx] &&
179 panelVisibility.filter(e => e).length === 1;
183 <Tooltip title={btnTooltip} disableFocusListener>
184 <Button variant={btnVariant} size="small" color="primary"
185 className={classNames(classes.button)}
186 onMouseEnter={() => {
187 setHighlightedPanel(idx);
189 onMouseLeave={() => {
190 setHighlightedPanel(-1);
192 onClick={showFn(idx)}>
199 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
200 panelRef={(idx === selectedPanel) ? panelRef : undefined}
201 maximized={panelIsMaximized} illuminated={idx === highlightedPanel}
202 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)} doUnMaximizePanel={panelIsMaximized ? unMaximizeFn(idx) : () => null}>
205 panels = [...panels, aPanel];
209 return <Grid container {...props}>
210 <Grid container item direction="row">
211 { buttons.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>) }
213 <Grid container item {...props} xs className={classes.content}
214 onScroll={() => setSelectedPanel(-1)}>
215 { panelVisibility.includes(true)
217 : <Grid container item alignItems='center' justify='center'>
218 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
224 export const MPVContainer = withStyles(styles)(MPVContainerComponent);