Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14433_properties_i...
[arvados.git] / src / views-components / projects-tree-picker / generic-projects-tree-picker.tsx
index c877a1546602f566d66585fcb7a960d6fd1d57d9..fafb05056ca712b86b841bb221da6258129ae080 100644 (file)
@@ -5,6 +5,7 @@
 import * as React from "react";
 import { Dispatch } from "redux";
 import { connect } from "react-redux";
+import { isEqual } from 'lodash/fp';
 import { TreeItem, TreeItemStatus } from '~/components/tree/tree';
 import { ProjectResource } from "~/models/project";
 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
@@ -28,24 +29,29 @@ export interface ProjectsTreePickerDataProps {
     includeCollections?: boolean;
     includeFiles?: boolean;
     rootItemIcon: IconType;
-    rootItemSelection?: boolean;
-    projectsSelection?: boolean;
-    collectionsSelection?: boolean;
-    filesSelection?: boolean;
+    showSelection?: boolean;
+    relatedTreePickers?: string[];
+    disableActivation?: string[];
     loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, inlcudeFiles?: boolean) => void;
 }
 
 export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;
 
-const mapStateToProps = (_: any, { rootItemIcon, ...props }: ProjectsTreePickerProps) => ({
+const mapStateToProps = (_: any, { rootItemIcon, showSelection }: ProjectsTreePickerProps) => ({
     render: renderTreeItem(rootItemIcon),
-    showSelection: isSelectionVisible(props),
+    showSelection: isSelectionVisible(showSelection),
 });
 
-const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
+const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollections, includeFiles, relatedTreePickers, ...props }: ProjectsTreePickerProps): PickedTreePickerProps => ({
     onContextMenu: () => { return; },
     toggleItemActive: (event, item, pickerId) => {
-        dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId }));
+        
+        const { disableActivation = [] } = props;
+        if(disableActivation.some(isEqual(item.id))){
+            return;
+        }
+
+        dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: item.id, pickerId, relatedTreePickers }));
         if (props.toggleItemActive) {
             props.toggleItemActive(event, item, pickerId);
         }
@@ -66,8 +72,11 @@ const mapDispatchToProps = (dispatch: Dispatch, { loadRootItem, includeCollectio
             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
         }
     },
-    toggleItemSelection: (_, { id }, pickerId) => {
-        dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id, pickerId }));
+    toggleItemSelection: (event, item, pickerId) => {
+        dispatch<any>(treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: item.id, pickerId }));
+        if (props.toggleItemSelection) {
+            props.toggleItemSelection(event, item, pickerId);
+        }
     },
 });
 
@@ -92,26 +101,16 @@ const getProjectPickerIcon = ({ data }: TreeItem<ProjectsTreePickerItem>, rootIc
         return rootIcon;
     }
 };
-interface IsSelectionVisibleParams {
-    rootItemSelection?: boolean;
-    projectsSelection?: boolean;
-    collectionsSelection?: boolean;
-    filesSelection?: boolean;
-}
-const isSelectionVisible = (params: IsSelectionVisibleParams) =>
-    ({ data, status }: TreeItem<ProjectsTreePickerItem>) => {
-        if ('kind' in data) {
-            switch (data.kind) {
-                case ResourceKind.COLLECTION:
-                    return !!params.collectionsSelection;
-                default:
-                    return !!params.projectsSelection;
+
+const isSelectionVisible = (shouldBeVisible?: boolean) =>
+    ({ status, items }: TreeItem<ProjectsTreePickerItem>): boolean => {
+        if (shouldBeVisible) {
+            if (items && items.length > 0) {
+                return items.every(isSelectionVisible(shouldBeVisible));
             }
-        } else if ('type' in data) {
-            return !!params.filesSelection;
-        } else {
-            return !!params.rootItemSelection;
+            return status === TreeItemStatus.LOADED;
         }
+        return false;
     };
 
 const renderTreeItem = (rootItemIcon: IconType) => (item: TreeItem<ProjectResource>) =>