X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/28bb06ae7b9983da793f2459c5cc3e8debd71949..ae7d952a97542c2cfc12f6f41ab0de93af278919:/src/components/tree/tree.tsx

diff --git a/src/components/tree/tree.tsx b/src/components/tree/tree.tsx
index d8397d66..5dd45878 100644
--- a/src/components/tree/tree.tsx
+++ b/src/components/tree/tree.tsx
@@ -8,68 +8,97 @@ import ListItem from "@material-ui/core/ListItem/ListItem";
 import { StyleRulesCallback, Theme, withStyles, WithStyles } from '@material-ui/core/styles';
 import { ReactElement } from "react";
 import Collapse from "@material-ui/core/Collapse/Collapse";
+import CircularProgress from '@material-ui/core/CircularProgress';
 
-type CssRules = 'list' | 'activeArrow' | 'arrow' | 'arrowRotate';
-
-const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    list: {
-        paddingBottom: '3px',
-        paddingTop: '3px',
-    },
-    activeArrow: {
-        color: '#4285F6',
-        position: 'absolute',
-    },
-    arrow: {
-        position: 'absolute',
-    },
-    arrowRotate: {
-        transform: 'rotate(-90deg)',
-    }
-});
+export enum TreeItemStatus {
+    Initial,
+    Pending,
+    Loaded
+}
 
 export interface TreeItem<T> {
     data: T;
     id: string;
     open: boolean;
     active: boolean;
+    status: TreeItemStatus;
+    toggled?: boolean;
     items?: Array<TreeItem<T>>;
 }
 
 interface TreeProps<T> {
     items?: Array<TreeItem<T>>;
     render: (item: TreeItem<T>, level?: number) => ReactElement<{}>;
-    toggleItem: (id: string) => any;
+    toggleItemOpen: (id: string, status: TreeItemStatus) => void;
+    toggleItemActive: (id: string) => void;
     level?: number;
 }
 
 class Tree<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
-    renderArrow (items: boolean, arrowClass: string, open: boolean){
-        return <i className={`${arrowClass} ${open ? "fas fa-caret-down" : `fas fa-caret-down ${this.props.classes.arrowRotate}`}`} />
+    renderArrow(status: TreeItemStatus, arrowClass: string, open: boolean, id: string) {
+        const { arrowTransition, arrowVisibility, arrowRotate } = this.props.classes;
+        return <i onClick={() => this.props.toggleItemOpen(id, status)}
+            className={`
+                    ${arrowClass} 
+                    ${status === TreeItemStatus.Pending ? arrowVisibility : ''} 
+                    ${open ? `fas fa-caret-down ${arrowTransition}` : `fas fa-caret-down ${arrowRotate}`}`} />;
     }
     render(): ReactElement<any> {
         const level = this.props.level ? this.props.level : 0;
-        const {classes, render, toggleItem, items} = this.props;
-        const {list, arrow, activeArrow} = classes;
+        const { classes, render, toggleItemOpen, items, toggleItemActive } = this.props;
+        const { list, inactiveArrow, activeArrow, loader } = classes;
         return <List component="div" className={list}>
             {items && items.map((it: TreeItem<T>, idx: number) =>
-             <div key={`item/${level}/${idx}`}>
-                <ListItem button onClick={() => toggleItem(it.id)} className={list} style={{paddingLeft: (level + 1) * 20}}>
-                    {this.renderArrow(true, it.active ? activeArrow : arrow, it.open)}
-                    {render(it, level)}
-                </ListItem>
-                {it.items && it.items.length > 0 &&
-                <Collapse in={it.open} timeout="auto" unmountOnExit>
-                    <StyledTree
-                        items={it.items}
-                        render={render}
-                        toggleItem={toggleItem}
-                        level={level + 1}/>
-                </Collapse>}
-             </div>)}
-        </List>
+                <div key={`item/${level}/${idx}`}>
+                    <ListItem button className={list} style={{ paddingLeft: (level + 1) * 20 }} onClick={() => toggleItemActive(it.id)}>
+                        {it.status === TreeItemStatus.Pending ? <CircularProgress size={10} className={loader} /> : null}
+                        {it.toggled && it.items && it.items.length === 0 ? null : this.renderArrow(it.status, it.active ? activeArrow : inactiveArrow, it.open, it.id)}
+                        {render(it, level)}
+                    </ListItem>
+                    {it.items && it.items.length > 0 &&
+                        <Collapse in={it.open} timeout="auto" unmountOnExit>
+                            <StyledTree
+                                items={it.items}
+                                render={render}
+                                toggleItemOpen={toggleItemOpen}
+                                toggleItemActive={toggleItemActive}
+                                level={level + 1} />
+                        </Collapse>}
+                </div>)}
+        </List>;
     }
 }
 
+type CssRules = 'list' | 'activeArrow' | 'inactiveArrow' | 'arrowRotate' | 'arrowTransition' | 'loader' | 'arrowVisibility';
+
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    list: {
+        paddingBottom: '3px',
+        paddingTop: '3px',
+    },
+    activeArrow: {
+        color: '#4285F6',
+        position: 'absolute',
+    },
+    inactiveArrow: {
+        position: 'absolute',
+    },
+    arrowTransition: {
+        transition: 'all 0.1s ease',
+    },
+    arrowRotate: {
+        transition: 'all 0.1s ease',
+        transform: 'rotate(-90deg)',
+    },
+    arrowVisibility: {
+        opacity: 0,
+    },
+    loader: {
+        position: 'absolute',
+        transform: 'translate(0px)',
+        top: '3px'
+    }
+});
+
 const StyledTree = withStyles(styles)(Tree);
-export default StyledTree
+export default StyledTree;
