X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/b58e3122a2574d81618bca58e27b046f82400f49..41f6f1e495c82fcfa79b87cf718fa2e9cd91c726:/src/models/collection-file.ts diff --git a/src/models/collection-file.ts b/src/models/collection-file.ts index 7bf6df74..91008d1f 100644 --- a/src/models/collection-file.ts +++ b/src/models/collection-file.ts @@ -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; @@ -13,6 +14,7 @@ export enum CollectionFileType { export interface CollectionDirectory { path: string; + url: string; id: string; name: string; type: CollectionFileType.DIRECTORY; @@ -20,16 +22,22 @@ export interface CollectionDirectory { export interface CollectionFile { path: string; + url: string; id: string; name: string; size: number; type: CollectionFileType.FILE; } +export interface CollectionUploadFile { + name: string; +} + export const createCollectionDirectory = (data: Partial): CollectionDirectory => ({ id: '', name: '', path: '', + url: '', type: CollectionFileType.DIRECTORY, ...data }); @@ -38,7 +46,35 @@ export const createCollectionFile = (data: Partial): CollectionF id: '', name: '', path: '', + url: '', size: 0, type: CollectionFileType.FILE, ...data -}); \ No newline at end of file +}); + +export const createCollectionFilesTree = (data: Array, 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()); +}; + +const getParentId = (item: CollectionDirectory | CollectionFile) => + item.path + ? join('', [getCollectionId(item.id), item.path]) + : item.path; + +const getCollectionId = pipe( + split('/'), + head, +); \ No newline at end of file