Merge branch '19434-collapse-left-panel' closes #19434
[arvados.git] / src / views / run-process-panel / inputs / directory-array-input.tsx
index 6da3210371490e968a041e9bc90adc59b5f2f8e4..e64dca0e49b9b8a8e55514267718108b82557662 100644 (file)
@@ -2,41 +2,42 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
+import React from 'react';
 import {
     isRequiredInput,
     DirectoryArrayCommandInputParameter,
     Directory,
     CWLType
-} from '~/models/workflow';
+} from 'models/workflow';
 import { Field } from 'redux-form';
-import { ERROR_MESSAGE } from '~/validators/require';
-import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, Divider, Grid, WithStyles, Typography } from '@material-ui/core';
+import { ERROR_MESSAGE } from 'validators/require';
+import { Input, Dialog, DialogTitle, DialogContent, DialogActions, Button, Divider, WithStyles, Typography } from '@material-ui/core';
 import { GenericInputProps, GenericInput } from './generic-input';
-import { ProjectsTreePicker } from '~/views-components/projects-tree-picker/projects-tree-picker';
+import { ProjectsTreePicker } from 'views-components/projects-tree-picker/projects-tree-picker';
 import { connect, DispatchProp } from 'react-redux';
-import { initProjectsTreePicker, getSelectedNodes, treePickerActions, getProjectsTreePickerIds, getAllNodes } from '~/store/tree-picker/tree-picker-actions';
-import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
+import { initProjectsTreePicker, getSelectedNodes, treePickerActions, getProjectsTreePickerIds, getAllNodes } from 'store/tree-picker/tree-picker-actions';
+import { ProjectsTreePickerItem } from 'store/tree-picker/tree-picker-middleware';
 import { createSelector, createStructuredSelector } from 'reselect';
-import { ChipsInput } from '~/components/chips-input/chips-input';
+import { ChipsInput } from 'components/chips-input/chips-input';
 import { identity, values, noop } from 'lodash';
 import { InputProps } from '@material-ui/core/Input';
-import { TreePicker } from '~/store/tree-picker/tree-picker';
-import { RootState } from '~/store/store';
-import { Chips } from '~/components/chips/chips';
+import { TreePicker } from 'store/tree-picker/tree-picker';
+import { RootState } from 'store/store';
+import { Chips } from 'components/chips/chips';
 import withStyles, { StyleRulesCallback } from '@material-ui/core/styles/withStyles';
-import { CollectionResource } from '~/models/collection';
-import { ResourceKind } from '~/models/resource';
+import { CollectionResource } from 'models/collection';
+import { ResourceKind } from 'models/resource';
 
 export interface DirectoryArrayInputProps {
     input: DirectoryArrayCommandInputParameter;
+    options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
 }
 
 export const DirectoryArrayInput = ({ input }: DirectoryArrayInputProps) =>
     <Field
         name={input.id}
         commandInput={input}
-        component={DirectoryArrayInputComponent}
+        component={DirectoryArrayInputComponent as any}
         parse={parseDirectories}
         format={formatDirectories}
         validate={validationSelector(input)} />;
@@ -58,7 +59,7 @@ const parse = (directory: CollectionResource): Directory => ({
 });
 
 const formatDirectories = (directories: Directory[] = []) =>
-    directories.map(format);
+    directories ? directories.map(format) : [];
 
 const format = ({ location = '', basename = '' }: Directory): FormattedDirectory => ({
     portableDataHash: location.replace('keep:', ''),
@@ -93,7 +94,9 @@ const mapStateToProps = createStructuredSelector({
 });
 
 const DirectoryArrayInputComponent = connect(mapStateToProps)(
-    class DirectoryArrayInputComponent extends React.Component<DirectoryArrayInputComponentProps & GenericInputProps & DispatchProp, DirectoryArrayInputComponentState> {
+    class DirectoryArrayInputComponent extends React.Component<DirectoryArrayInputComponentProps & GenericInputProps & DispatchProp & {
+        options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
+    }, DirectoryArrayInputComponentState> {
         state: DirectoryArrayInputComponentState = {
             open: false,
             directories: [],
@@ -157,7 +160,7 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
                 .reduce((directories, { value }) =>
                     'kind' in value &&
                         value.kind === ResourceKind.COLLECTION &&
-                        formattedDirectories.find(({ portableDataHash }) => value.portableDataHash === portableDataHash)
+                        formattedDirectories.find(({ portableDataHash, name }) => value.portableDataHash === portableDataHash && value.name === name)
                         ? directories.concat(value)
                         : directories, initialDirectories);
 
@@ -179,7 +182,7 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
             });
 
             const orderedDirectories = formattedDirectories.reduce((dirs, formattedDir) => {
-                const dir = directories.find(({ portableDataHash }) => portableDataHash === formattedDir.portableDataHash);
+                const dir = directories.find(({ portableDataHash, name }) => portableDataHash === formattedDir.portableDataHash && name === formattedDir.name);
                 return dir
                     ? [...dirs, dir]
                     : dirs;
@@ -191,7 +194,7 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
 
         refreshDirectories = () => {
             clearTimeout(this.directoryRefreshTimeout);
-            this.directoryRefreshTimeout = setTimeout(this.setSelectedFiles);
+            this.directoryRefreshTimeout = window.setTimeout(this.setSelectedFiles);
         }
 
         setSelectedFiles = () => {
@@ -211,7 +214,7 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
 
         chipsInput = () =>
             <ChipsInput
-                value={this.props.input.value}
+                values={this.props.input.value}
                 onChange={noop}
                 disabled={this.props.commandInput.disabled}
                 createNewValue={identity}
@@ -228,30 +231,17 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
                 onBlur={this.props.input.onBlur}
                 disabled={this.props.commandInput.disabled} />
 
-        dialog = () =>
-            <Dialog
-                open={this.state.open}
-                onClose={this.closeDialog}
-                fullWidth
-                maxWidth='md' >
-                <DialogTitle>Choose collections</DialogTitle>
-                <DialogContent>
-                    <this.dialogContent />
-                </DialogContent>
-                <DialogActions>
-                    <Button onClick={this.closeDialog}>Cancel</Button>
-                    <Button
-                        variant='contained'
-                        color='primary'
-                        onClick={this.submit}>Ok</Button>
-                </DialogActions>
-            </Dialog>
-
         dialogContentStyles: StyleRulesCallback<DialogContentCssRules> = ({ spacing }) => ({
             root: {
                 display: 'flex',
                 flexDirection: 'column',
-                height: `${spacing.unit * 8}vh`,
+            },
+            pickerWrapper: {
+                display: 'flex',
+                flexDirection: 'column',
+                flexBasis: `${spacing.unit * 8}vh`,
+                flexShrink: 1,
+                minHeight: 0,
             },
             tree: {
                 flex: 3,
@@ -266,21 +256,44 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
                 padding: `${spacing.unit}px 0`,
                 overflowX: 'hidden',
             },
-        })
+        });
+
+        dialog = withStyles(this.dialogContentStyles)(
+            ({ classes }: WithStyles<DialogContentCssRules>) =>
+                <Dialog
+                    open={this.state.open}
+                    onClose={this.closeDialog}
+                    fullWidth
+                    maxWidth='md' >
+                    <DialogTitle>Choose collections</DialogTitle>
+                    <DialogContent className={classes.root}>
+                        <this.dialogContent />
+                    </DialogContent>
+                    <DialogActions>
+                        <Button onClick={this.closeDialog}>Cancel</Button>
+                        <Button
+                            data-cy='ok-button'
+                            variant='contained'
+                            color='primary'
+                            onClick={this.submit}>Ok</Button>
+                    </DialogActions>
+                </Dialog>
+        );
 
         dialogContent = withStyles(this.dialogContentStyles)(
             ({ classes }: WithStyles<DialogContentCssRules>) =>
-                <div className={classes.root}>
+                <div className={classes.pickerWrapper}>
                     <div className={classes.tree}>
                         <ProjectsTreePicker
                             pickerId={this.props.commandInput.id}
                             includeCollections
                             showSelection
+                            options={this.props.options}
                             toggleItemSelection={this.refreshDirectories} />
                     </div>
                     <Divider />
                     <div className={classes.chips}>
-                        <Typography variant='subheading'>Selected collections ({this.state.directories.length}):</Typography>
+                        <Typography variant='subtitle1'>Selected collections ({this.state.directories.length}):</Typography>
                         <Chips
                             orderable
                             deletable
@@ -293,7 +306,4 @@ const DirectoryArrayInputComponent = connect(mapStateToProps)(
 
     });
 
-type DialogContentCssRules = 'root' | 'tree' | 'divider' | 'chips';
-
-
-
+type DialogContentCssRules = 'root' | 'pickerWrapper' | 'tree' | 'divider' | 'chips';