Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / models / collection-file.ts
index a4b656c51397c80bbd0319b73bf7f18d6d12323b..3688557a6154c8e8a3fc9dca87d486d004a6b754 100644 (file)
@@ -2,7 +2,8 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Tree } from './tree';
+import { Tree, createTree, setNode, TreeNodeStatus } from './tree';
+import { head, split, pipe, join } from 'lodash/fp';
 
 export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
 
@@ -13,6 +14,7 @@ export enum CollectionFileType {
 
 export interface CollectionDirectory {
     path: string;
+    url: string;
     id: string;
     name: string;
     type: CollectionFileType.DIRECTORY;
@@ -20,6 +22,7 @@ export interface CollectionDirectory {
 
 export interface CollectionFile {
     path: string;
+    url: string;
     id: string;
     name: string;
     size: number;
@@ -34,6 +37,7 @@ export const createCollectionDirectory = (data: Partial<CollectionDirectory>): C
     id: '',
     name: '',
     path: '',
+    url: '',
     type: CollectionFileType.DIRECTORY,
     ...data
 });
@@ -42,7 +46,35 @@ export const createCollectionFile = (data: Partial<CollectionFile>): CollectionF
     id: '',
     name: '',
     path: '',
+    url: '',
     size: 0,
     type: CollectionFileType.FILE,
     ...data
 });
+
+export const createCollectionFilesTree = (data: Array<CollectionDirectory | CollectionFile>, joinParents: Boolean = true) => {
+    const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY);
+    directories.sort((a, b) => a.path.localeCompare(b.path));
+    const files = data.filter(item => item.type === CollectionFileType.FILE);
+    return [...directories, ...files]
+        .reduce((tree, item) => setNode({
+            children: [],
+            id: item.id,
+            parent: joinParents ? getParentId(item) : '',
+            value: item,
+            active: false,
+            selected: false,
+            expanded: false,
+            status: TreeNodeStatus.INITIAL
+        })(tree), createTree<CollectionDirectory | CollectionFile>());
+};
+
+const getParentId = (item: CollectionDirectory | CollectionFile) =>
+    item.path
+        ? join('', [getCollectionResourceCollectionUuid(item.id), item.path])
+        : item.path;
+
+export const getCollectionResourceCollectionUuid = pipe(
+    split('/'),
+    head,
+);