selected: false,
expanded: false,
status: TreeNodeStatus.INITIAL
-
})(tree), createTree<CollectionDirectory | CollectionFile>());
};
)(subtree) as Tree<T>;
export const setNode = <T>(node: TreeNode<T>) => (tree: Tree<T>): Tree<T> => {
- return pipe(
- (tree: Tree<T>) => getNode(node.id)(tree) === node
- ? tree
- : { ...tree, [node.id]: node },
- addChild(node.parent, node.id)
- )(tree);
+ if (tree[node.id] && tree[node.id] === node) { return tree; }
+
+ tree[node.id] = node;
+ if (tree[node.parent]) {
+ tree[node.parent].children = Array.from(new Set([...tree[node.parent].children, node.id]));
+ }
+ return tree;
};
export const getNodeValue = (id: string) => <T>(tree: Tree<T>) => {
toggleAncestorsSelection(id),
toggleDescendantsSelection(id))(tree)
: tree;
-
};
export const selectNode = (id: string) => <T>(tree: Tree<T>) => {
Object
.keys(tree)
.filter(id => getNode(id)(tree)!.parent === TREE_ROOT_ID);
-
-
-const addChild = (parentId: string, childId: string) => <T>(tree: Tree<T>): Tree<T> => {
- if (childId === "") {
- return tree;
- }
- const node = getNode(parentId)(tree);
- if (node) {
- const children = node.children.some(id => id === childId)
- ? node.children
- : [...node.children, childId];
-
- const newNode = children === node.children
- ? node
- : { ...node, children };
-
- return setNode(newNode)(tree);
- }
- return tree;
-};
return getTagValue(element, 'D:resourcetype', '')
? createCollectionDirectory(data)
: createCollectionFile({ ...data, size });
-
});
};
import { CollectionFileType } from "~/models/collection-file";
export const collectionPanelFilesReducer = (state: CollectionPanelFilesState = createTree(), action: CollectionPanelFilesAction) => {
+ // Low-level tree handling setNode() func does in-place data modifications
+ // for performance reasons, so we pass a copy of 'state' to avoid side effects.
return collectionPanelFilesAction.match(action, {
SET_COLLECTION_FILES: files =>
- mergeCollectionPanelFilesStates(state, mapTree(mapCollectionFileToCollectionPanelFile)(files)),
+ mergeCollectionPanelFilesStates({...state}, mapTree(mapCollectionFileToCollectionPanelFile)(files)),
TOGGLE_COLLECTION_FILE_COLLAPSE: data =>
- toggleCollapse(data.id)(state),
+ toggleCollapse(data.id)({...state}),
- TOGGLE_COLLECTION_FILE_SELECTION: data => [state]
+ TOGGLE_COLLECTION_FILE_SELECTION: data => [{...state}]
.map(toggleSelected(data.id))
.map(toggleAncestors(data.id))
.map(toggleDescendants(data.id))[0],
SELECT_ALL_COLLECTION_FILES: () =>
- mapTreeValues(v => ({ ...v, selected: true }))(state),
+ mapTreeValues(v => ({ ...v, selected: true }))({...state}),
UNSELECT_ALL_COLLECTION_FILES: () =>
- mapTreeValues(v => ({ ...v, selected: false }))(state),
+ mapTreeValues(v => ({ ...v, selected: false }))({...state}),
default: () => state
}) as CollectionPanelFilesState;
showSelection?: boolean;
relatedTreePickers?: string[];
disableActivation?: string[];
- loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, inlcudeFiles?: boolean) => void;
+ loadRootItem: (item: TreeItem<ProjectsTreePickerRootItem>, pickerId: string, includeCollections?: boolean, includeFiles?: boolean) => void;
}
export type ProjectsTreePickerProps = ProjectsTreePickerDataProps & Partial<PickedTreePickerProps>;