X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a482d45b5a46c897ff2da9808e01317831559a00..034bf8ad6df6c47d3c392ae8233c8d08dc6b4609:/src/components/multi-panel-view/multi-panel-view.tsx diff --git a/src/components/multi-panel-view/multi-panel-view.tsx b/src/components/multi-panel-view/multi-panel-view.tsx index 5c16096ba2..877061de37 100644 --- a/src/components/multi-panel-view/multi-panel-view.tsx +++ b/src/components/multi-panel-view/multi-panel-view.tsx @@ -2,16 +2,24 @@ // // SPDX-License-Identifier: AGPL-3.0 -import React, { ReactElement, ReactNode, useState } from 'react'; -import { Button, Grid, StyleRulesCallback, Tooltip, withStyles, WithStyles } from "@material-ui/core"; +import React, { MutableRefObject, ReactElement, ReactNode, useEffect, useRef, useState } from 'react'; +import { + Button, + Grid, + Paper, + StyleRulesCallback, + Tooltip, + withStyles, + WithStyles +} from "@material-ui/core"; import { GridProps } from '@material-ui/core/Grid'; import { isArray } from 'lodash'; import { DefaultView } from 'components/default-view/default-view'; -import { InfoIcon, InvisibleIcon, VisibleIcon } from 'components/icon/icon'; +import { InfoIcon } from 'components/icon/icon'; import { ReactNodeArray } from 'prop-types'; import classNames from 'classnames'; -type CssRules = 'button' | 'buttonIcon'; +type CssRules = 'button' | 'buttonIcon' | 'content'; const styles: StyleRulesCallback = theme => ({ button: { @@ -23,33 +31,49 @@ const styles: StyleRulesCallback = theme => ({ padding: '2px 0px 2px 5px', fontSize: '1rem' }, + content: { + overflow: 'auto', + }, }); interface MPVHideablePanelDataProps { name: string; visible: boolean; + maximized: boolean; + illuminated: boolean; children: ReactNode; + panelRef?: MutableRefObject; } interface MPVHideablePanelActionProps { doHidePanel: () => void; + doMaximizePanel: () => void; + doUnMaximizePanel: () => void; } type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps; -const MPVHideablePanel = ({doHidePanel, name, visible, ...props}: MPVHideablePanelProps) => +const MPVHideablePanel = ({doHidePanel, doMaximizePanel, doUnMaximizePanel, name, visible, maximized, illuminated, ...props}: MPVHideablePanelProps) => visible ? <> - {React.cloneElement((props.children as ReactElement), { doHidePanel, panelName: name })} + {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })} : null; interface MPVPanelDataProps { panelName?: string; + panelMaximized?: boolean; + panelIlluminated?: boolean; + panelRef?: MutableRefObject; + forwardProps?: boolean; + maxHeight?: string; + minHeight?: string; } interface MPVPanelActionProps { doHidePanel?: () => void; + doMaximizePanel?: () => void; + doUnMaximizePanel?: () => void; } // Props received by panel implementors @@ -58,66 +82,124 @@ export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps; type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps; // Grid item compatible component for layout and MPV props passing -export const MPVPanelContent = ({doHidePanel, panelName, ...props}: MPVPanelContentProps) => - - {React.cloneElement(props.children, { doHidePanel, panelName })} +export const MPVPanelContent = ({doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, + panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight, minHeight, + ...props}: MPVPanelContentProps) => { + useEffect(() => { + if (panelRef && panelRef.current) { + panelRef.current.scrollIntoView({alignToTop: true}); + } + }, [panelRef]); + + const maxH = panelMaximized + ? '100%' + : maxHeight; + + return + {/* Element to scroll to when the panel is selected */} + + { forwardProps + ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, doUnMaximizePanel, panelName, panelMaximized }) + : props.children } + ; - -export interface MPVContainerDataProps { - panelNames?: string[]; } +export interface MPVPanelState { + name: string; + visible?: boolean; +} +interface MPVContainerDataProps { + panelStates?: MPVPanelState[]; +} type MPVContainerProps = MPVContainerDataProps & GridProps; // Grid container compatible component that also handles panel toggling. -const MPVContainerComponent = ({children, panelNames, classes, ...props}: MPVContainerProps & WithStyles) => { +const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVContainerProps & WithStyles) => { if (children === undefined || children === null || children === {}) { children = []; } else if (!isArray(children)) { children = [children]; } - const visibility = (children as ReactNodeArray).map(() => true); - const [panelVisibility, setPanelVisibility] = useState(visibility); + const initialVisibility = (children as ReactNodeArray).map((_, idx) => + !panelStates || // if panelStates wasn't passed, default to all visible panels + (panelStates[idx] && + (panelStates[idx].visible || panelStates[idx].visible === undefined))); + const [panelVisibility, setPanelVisibility] = useState(initialVisibility); + const [previousPanelVisibility, setPreviousPanelVisibility] = useState(initialVisibility); + const [highlightedPanel, setHighlightedPanel] = useState(-1); + const [selectedPanel, setSelectedPanel] = useState(-1); + const panelRef = useRef(null); let panels: JSX.Element[] = []; - let toggles: JSX.Element[] = []; + let buttons: JSX.Element[] = []; if (isArray(children)) { for (let idx = 0; idx < children.length; idx++) { - const toggleFn = (idx: number) => () => { + const showFn = (idx: number) => () => { + setPreviousPanelVisibility(initialVisibility); + setPanelVisibility([ + ...panelVisibility.slice(0, idx), + true, + ...panelVisibility.slice(idx+1) + ]); + setSelectedPanel(idx); + }; + const hideFn = (idx: number) => () => { + setPreviousPanelVisibility(initialVisibility); setPanelVisibility([ ...panelVisibility.slice(0, idx), - !panelVisibility[idx], + false, ...panelVisibility.slice(idx+1) ]) }; - const toggleIcon = panelVisibility[idx] - ? - : - const panelName = panelNames === undefined + const maximizeFn = (idx: number) => () => { + setPreviousPanelVisibility(panelVisibility); + // Maximize X == hide all but X + setPanelVisibility([ + ...panelVisibility.slice(0, idx).map(() => false), + true, + ...panelVisibility.slice(idx+1).map(() => false), + ]); + }; + const unMaximizeFn = (idx: number) => () => { + setPanelVisibility(previousPanelVisibility); + setSelectedPanel(idx); + } + const panelName = panelStates === undefined ? `Panel ${idx+1}` - : panelNames[idx] || `Panel ${idx+1}`; - const toggleVariant = panelVisibility[idx] - ? "raised" - : "flat"; - const toggleTooltip = panelVisibility[idx] - ? `Hide ${panelName} panel` - : `Show ${panelName} panel`; - - toggles = [ - ...toggles, - - ]; const aPanel = - + null}> {children[idx]} ; panels = [...panels, aPanel]; @@ -125,14 +207,17 @@ const MPVContainerComponent = ({children, panelNames, classes, ...props}: MPVCon }; return - - { toggles } + + { buttons.map((tgl, idx) => {tgl}) } + + setSelectedPanel(-1)}> + { panelVisibility.includes(true) + ? panels + : + + } - { panelVisibility.includes(true) - ? panels - : - - } ; };