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;
71 interface MPVPanelActionProps {
72 doHidePanel?: () => void;
73 doMaximizePanel?: () => void;
76 // Props received by panel implementors
77 export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
79 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
81 // Grid item compatible component for layout and MPV props passing
82 export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName,
83 panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight,
84 ...props}: MPVPanelContentProps) => {
86 if (panelRef && panelRef.current) {
87 panelRef.current.scrollIntoView({behavior: 'smooth'});
91 const mh = panelMaximized
95 return <Grid item style={{maxHeight: mh}} {...props}>
96 <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
97 <Paper style={{height: '100%'}} elevation={panelIlluminated ? 8 : 0}>
99 ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })
105 export interface MPVPanelState {
109 interface MPVContainerDataProps {
110 panelStates?: MPVPanelState[];
112 type MPVContainerProps = MPVContainerDataProps & GridProps;
114 // Grid container compatible component that also handles panel toggling.
115 const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVContainerProps & WithStyles<CssRules>) => {
116 if (children === undefined || children === null || children === {}) {
118 } else if (!isArray(children)) {
119 children = [children];
121 const visibility = (children as ReactNodeArray).map((_, idx) =>
122 !panelStates || // if panelStates wasn't passed, default to all visible panels
124 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
125 const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
126 const [brightenedPanel, setBrightenedPanel] = useState<number>(-1);
127 const panelRef = useRef<any>(null);
129 let panels: JSX.Element[] = [];
130 let toggles: JSX.Element[] = [];
132 if (isArray(children)) {
133 for (let idx = 0; idx < children.length; idx++) {
134 const showFn = (idx: number) => () => {
136 ...panelVisibility.slice(0, idx),
138 ...panelVisibility.slice(idx+1)
141 const hideFn = (idx: number) => () => {
143 ...panelVisibility.slice(0, idx),
145 ...panelVisibility.slice(idx+1)
148 const maximizeFn = (idx: number) => () => {
149 // Maximize X == hide all but X
151 ...panelVisibility.slice(0, idx).map(() => false),
153 ...panelVisibility.slice(idx+1).map(() => false),
156 const toggleIcon = panelVisibility[idx]
157 ? <VisibleIcon className={classNames(classes.buttonIcon)} />
158 : <InvisibleIcon className={classNames(classes.buttonIcon)}/>
159 const panelName = panelStates === undefined
161 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
162 const toggleVariant = "outlined";
163 const toggleTooltip = panelVisibility[idx]
165 :`Show ${panelName} panel`;
166 const panelIsMaximized = panelVisibility[idx] &&
167 panelVisibility.filter(e => e).length === 1;
169 let brightenerTimer: NodeJS.Timer;
172 <Tooltip title={toggleTooltip} disableFocusListener>
173 <Button variant={toggleVariant} size="small" color="primary"
174 className={classNames(classes.button)}
175 onMouseEnter={() => {
176 brightenerTimer = setTimeout(
177 () => setBrightenedPanel(idx), 100);
179 onMouseLeave={() => {
180 brightenerTimer && clearTimeout(brightenerTimer);
181 setBrightenedPanel(-1);
183 onClick={showFn(idx)}>
191 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
192 panelRef={(idx === brightenedPanel) ? panelRef : undefined}
193 maximized={panelIsMaximized} illuminated={idx === brightenedPanel}
194 doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)}>
197 panels = [...panels, aPanel];
201 return <Grid container {...props}>
202 <Grid container item direction="row">
203 { toggles.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>) }
205 <Grid container item {...props} xs className={classes.content}>
206 { panelVisibility.includes(true)
208 : <Grid container item alignItems='center' justify='center'>
209 <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
215 export const MPVContainer = withStyles(styles)(MPVContainerComponent);