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, InvisibleIcon, VisibleIcon } 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;
53 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
55 const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, illuminated, ...props}: MPVHideablePanelProps) =>
58 {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })}
62 interface MPVPanelDataProps {
64 panelMaximized?: boolean;
65 panelIlluminated?: boolean;
66 panelRef?: MutableRefObject<any>;
67 forwardProps?: boolean;
70 interface MPVPanelActionProps {
71 doHidePanel?: () => void;
72 doMaximizePanel?: () => void;
75 // Props received by panel implementors
76 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
78 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
80 // Grid item compatible component for layout and MPV props passing
81 export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName, panelMaximized, panelIlluminated, panelRef, forwardProps, ...props}: MPVPanelContentProps) => {
83 if (panelRef && panelRef.current) {
84 panelRef.current.scrollIntoView({behavior: 'smooth'});
88 return <Grid item {...props}>
89 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
90 <Paper style={{height: '100%'}} elevation={panelIlluminated ? 8 : 0}>
92 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })
98 export interface MPVPanelState {
102 interface MPVContainerDataProps {
103 panelStates?: MPVPanelState[];
105 type MPVContainerProps = MPVContainerDataProps & GridProps;
107 // Grid container compatible component that also handles panel toggling.
108 const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVContainerProps & WithStyles<CssRules>) => {
109 if (children === undefined || children === null || children === {}) {
111 } else if (!isArray(children)) {
112 children = [children];
114 const visibility = (children as ReactNodeArray).map((_, idx) =>
115 !!!panelStates || // if panelStates wasn't passed, default to all visible panels
117 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
118 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
119 const [brightenedPanel, setBrightenedPanel] = useState<number>(-1);
120 const panelRef = useRef<any>(null);
122 let panels: JSX.Element[] = [];
123 let toggles: JSX.Element[] = [];
125 if (isArray(children)) {
126 for (let idx = 0; idx < children.length; idx++) {
127 const showFn = (idx: number) => () => {
129 ...panelVisibility.slice(0, idx),
131 ...panelVisibility.slice(idx+1)
134 const hideFn = (idx: number) => () => {
136 ...panelVisibility.slice(0, idx),
138 ...panelVisibility.slice(idx+1)
141 const maximizeFn = (idx: number) => () => {
142 // Maximize X == hide all but X
144 ...panelVisibility.slice(0, idx).map(() => false),
146 ...panelVisibility.slice(idx+1).map(() => false),
149 const toggleIcon = panelVisibility[idx]
150 ? <VisibleIcon className={classNames(classes.buttonIcon)} />
151 : <InvisibleIcon className={classNames(classes.buttonIcon)}/>
152 const panelName = panelStates === undefined
154 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
155 const toggleVariant = "outlined";
156 const toggleTooltip = panelVisibility[idx]
158 :`Show ${panelName} panel`;
159 const panelIsMaximized = panelVisibility[idx] &&
160 panelVisibility.filter(e => e).length === 1;
164 <Tooltip title={toggleTooltip} disableFocusListener>
165 <Button variant={toggleVariant} size="small" color="primary"
166 className={classNames(classes.button)}
167 onMouseEnter={() => setBrightenedPanel(idx)}
168 onMouseLeave={() => setBrightenedPanel(-1)}
169 onClick={showFn(idx)}>
177 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
178 panelRef={(idx === brightenedPanel) ? panelRef : undefined}
179 maximized={panelIsMaximized} illuminated={idx === brightenedPanel}
180 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)}>
183 panels = [...panels, aPanel];
187 return <Grid container {...props}>
188 <Grid container item direction="row">
189 { toggles.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>) }
191 <Grid container item {...props} xs className={classes.content}>
192 { panelVisibility.includes(true)
194 : <Grid container item alignItems='center' justify='center'>
195 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
201 export const MPVContainer = withStyles(styles)(MPVContainerComponent);