Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/selectors/getFlattenedTree.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
export const isNodeExpanded = node => node.state && node.state.expanded;
export const nodeHasChildren = node => node.children && node.children.length;

export const getFlattenedTree = (nodes, parents = []) =>
nodes.reduce((flattenedTree, node) => {
export const getFlattenedTree = (nodes, parents = []) => {
const flattenedTree = [];
const stack = [...nodes.map(node => ({node, parents}))];

while (stack.length > 0) {
const {node, parents} = stack.shift();
const deepness = parents.length;
const nodeWithHelpers = {...node, deepness, parents};

if (!nodeHasChildren(node) || !isNodeExpanded(node)) {
return [...flattenedTree, nodeWithHelpers];
flattenedTree.push(nodeWithHelpers);
} else {
flattenedTree.push(nodeWithHelpers);
stack.unshift(...node.children.map(child => ({node: child, parents: [...parents, node.id]})));
}
}

return [...flattenedTree, nodeWithHelpers, ...getFlattenedTree(node.children, [...parents, node.id])];
}, []);
return flattenedTree;
};

export const getFlattenedTreePaths = (nodes, parents = []) => {
const paths = [];
const stack = [...nodes.map(node => ({node, parents}))];

for (const node of nodes) {
while (stack.length > 0) {
const {node, parents} = stack.shift();
const {id} = node;

if (!nodeHasChildren(node) || !isNodeExpanded(node)) {
paths.push(parents.concat(id));
paths.push([...parents, id]);
} else {
paths.push(parents.concat(id));
paths.push(...getFlattenedTreePaths(node.children, [...parents, id]));
paths.push([...parents, id]);
stack.unshift(...node.children.map(child => ({node: child, parents: [...parents, id]})));
}
}

return paths;
};

Expand Down
8 changes: 6 additions & 2 deletions src/state/TreeStateModifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class TreeStateModifiers {
static editNodeAt = (state, index, nodeUpdate) => {
const node = TreeState.getNodeAt(state, index);
const updatedNode = typeof nodeUpdate === 'function' ? nodeUpdate(node) : nodeUpdate;
const flattenedTree = [...state.flattenedTree];
let flattenedTree = [...state.flattenedTree];
const flattenedNodeMap = flattenedTree[index];
const parents = flattenedNodeMap.slice(0, flattenedNodeMap.length - 1);

Expand All @@ -37,7 +37,11 @@ export default class TreeStateModifiers {
if (isNodeExpanded(updatedNode)) {
const updatedNodeSubTree = getFlattenedTreePaths([updatedNode], parents);

flattenedTree.splice(index + 1, 0, ...updatedNodeSubTree.slice(1));
flattenedTree = [
...flattenedTree.slice(0, index + 1),
...updatedNodeSubTree.slice(1),
...flattenedTree.slice(index + 1 + numberOfVisibleDescendants),
];
} else {
flattenedTree.splice(index + 1, numberOfVisibleDescendants);
}
Expand Down