X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/b4b78a306a93fc566ce9a442d8008beaff81cb64..51f7337782c238ecb7d43490268bf90b856150a2:/src/components/multi-panel-view/multi-panel-view.tsx?ds=inline

diff --git a/src/components/multi-panel-view/multi-panel-view.tsx b/src/components/multi-panel-view/multi-panel-view.tsx
index 6778b526..48241c0b 100644
--- a/src/components/multi-panel-view/multi-panel-view.tsx
+++ b/src/components/multi-panel-view/multi-panel-view.tsx
@@ -2,8 +2,16 @@
 //
 // 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';
@@ -32,7 +40,9 @@ interface MPVHideablePanelDataProps {
     name: string;
     visible: boolean;
     maximized: boolean;
+    illuminated: boolean;
     children: ReactNode;
+    panelRef?: MutableRefObject<any>;
 }
 
 interface MPVHideablePanelActionProps {
@@ -42,16 +52,20 @@ interface MPVHideablePanelActionProps {
 
 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
 
-const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, ...props}: MPVHideablePanelProps) =>
+const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, illuminated, ...props}: MPVHideablePanelProps) =>
     visible
     ? <>
-        {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel,panelName: name, panelMaximized: maximized })}
+        {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })}
     </>
     : null;
 
 interface MPVPanelDataProps {
     panelName?: string;
     panelMaximized?: boolean;
+    panelIlluminated?: boolean;
+    panelRef?: MutableRefObject<any>;
+    forwardProps?: boolean;
+    maxHeight?: string;
 }
 
 interface MPVPanelActionProps {
@@ -65,10 +79,31 @@ 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, doMaximizePanel, panelName, panelMaximized, ...props}: MPVPanelContentProps) =>
-    <Grid item {...props}>
-        {React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })}
+export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName,
+    panelMaximized, panelIlluminated, panelRef, forwardProps, maxHeight,
+    ...props}: MPVPanelContentProps) => {
+    useEffect(() => {
+        if (panelRef && panelRef.current) {
+            panelRef.current.scrollIntoView({behavior: 'smooth'});
+        }
+    }, [panelRef]);
+
+    // If maxHeight is set, only apply it when not maximized
+    const mh = maxHeight
+        ? panelMaximized
+            ? '100%'
+            : maxHeight
+        : undefined;
+
+    return <Grid item style={{maxHeight: mh}} {...props}>
+        <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
+        <Paper style={{height: '100%'}} elevation={panelIlluminated ? 8 : 0}>
+            { forwardProps
+                ? React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })
+                : props.children }
+        </Paper>
     </Grid>;
+}
 
 export interface MPVPanelState {
     name: string;
@@ -87,20 +122,29 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
         children = [children];
     }
     const visibility = (children as ReactNodeArray).map((_, idx) =>
-        !!!panelStates || // if panelStates wasn't passed, default to all visible panels
+        !panelStates || // if panelStates wasn't passed, default to all visible panels
             (panelStates[idx] &&
                 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
     const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
+    const [brightenedPanel, setBrightenedPanel] = useState<number>(-1);
+    const panelRef = useRef<any>(null);
 
     let panels: JSX.Element[] = [];
     let toggles: JSX.Element[] = [];
 
     if (isArray(children)) {
         for (let idx = 0; idx < children.length; idx++) {
-            const toggleFn = (idx: number) => () => {
+            const showFn = (idx: number) => () => {
+                setPanelVisibility([
+                    ...panelVisibility.slice(0, idx),
+                    true,
+                    ...panelVisibility.slice(idx+1)
+                ]);
+            };
+            const hideFn = (idx: number) => () => {
                 setPanelVisibility([
                     ...panelVisibility.slice(0, idx),
-                    !panelVisibility[idx],
+                    false,
                     ...panelVisibility.slice(idx+1)
                 ])
             };
@@ -118,21 +162,28 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
             const panelName = panelStates === undefined
                 ? `Panel ${idx+1}`
                 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
-            const toggleVariant = panelVisibility[idx]
-                ? "raised"
-                : "flat";
+            const toggleVariant = "outlined";
             const toggleTooltip = panelVisibility[idx]
-                ? `Hide ${panelName} panel`
-                : `Show ${panelName} panel`;
+                ? ''
+                :`Show ${panelName} panel`;
             const panelIsMaximized = panelVisibility[idx] &&
                 panelVisibility.filter(e => e).length === 1;
 
+            let brightenerTimer: NodeJS.Timer;
             toggles = [
                 ...toggles,
                 <Tooltip title={toggleTooltip} disableFocusListener>
                     <Button variant={toggleVariant} size="small" color="primary"
                         className={classNames(classes.button)}
-                        onClick={toggleFn(idx)}>
+                        onMouseEnter={() => {
+                            brightenerTimer = setTimeout(
+                                () => setBrightenedPanel(idx), 100);
+                        }}
+                        onMouseLeave={() => {
+                            brightenerTimer && clearTimeout(brightenerTimer);
+                            setBrightenedPanel(-1);
+                        }}
+                        onClick={showFn(idx)}>
                             {panelName}
                             {toggleIcon}
                     </Button>
@@ -140,9 +191,10 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
             ];
 
             const aPanel =
-                <MPVHideablePanel visible={panelVisibility[idx]} name={panelName}
-                    maximized={panelIsMaximized}
-                    doHidePanel={toggleFn(idx)} doMaximizePanel={maximizeFn(idx)}>
+                <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
+                    panelRef={(idx === brightenedPanel) ? panelRef : undefined}
+                    maximized={panelIsMaximized} illuminated={idx === brightenedPanel}
+                    doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)}>
                     {children[idx]}
                 </MPVHideablePanel>;
             panels = [...panels, aPanel];
@@ -151,7 +203,7 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
 
     return <Grid container {...props}>
         <Grid container item direction="row">
-            { toggles.map(tgl => <Grid item>{tgl}</Grid>) }
+            { toggles.map((tgl, idx) => <Grid item key={idx}>{tgl}</Grid>) }
         </Grid>
         <Grid container item {...props} xs className={classes.content}>
             { panelVisibility.includes(true)
