Skip to content

Commit

Permalink
Fix a bug that will not be able to add new child nodes if rootNode is…
Browse files Browse the repository at this point in the history
… null
  • Loading branch information
cheton committed Jun 2, 2016
1 parent b017187 commit f8b1759
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/infinite-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ const ensureNodeInstance = (node) => {
return true;
};

const createRootNode = () => new Node({
parent: null,
children: [],
state: {
depth: -1,
open: true, // always open
path: '',
prefixMask: '',
total: 0
}
});

class InfiniteTree extends events.EventEmitter {
options = {
autoOpen: false,
Expand All @@ -50,7 +62,7 @@ class InfiniteTree extends events.EventEmitter {
};
state = {
openNodes: [],
rootNode: null,
rootNode: createRootNode(),
selectedNode: null
};
clusterize = null;
Expand Down Expand Up @@ -474,7 +486,7 @@ class InfiniteTree extends events.EventEmitter {
this.nodes = [];
this.rows = [];
this.state.openNodes = [];
this.state.rootNode = null;
this.state.rootNode = createRootNode();
this.state.selectedNode = null;
}
// Closes a node to hide its children.
Expand Down Expand Up @@ -661,23 +673,24 @@ class InfiniteTree extends events.EventEmitter {
this.nodeTable.clear();

this.state.openNodes = this.nodes.filter((node) => (node.hasChildren() && node.state.open));
this.state.rootNode = ((node = null) => {
this.state.selectedNode = null;

const rootNode = ((node = null) => {
// Finding the root node
while (node && node.parent !== null) {
node = node.parent;
}
return node;
})((this.nodes.length > 0) ? this.nodes[0] : null);
this.state.selectedNode = null;

if (this.state.rootNode) {
// Update the lookup table with newly added nodes
this.flattenChildNodes(this.state.rootNode).forEach((node) => {
if (node.id !== undefined) {
this.nodeTable.set(node.id, node);
}
});
}
this.state.rootNode = rootNode || createRootNode(); // Create a new root node if rootNode is null

// Update the lookup table with newly added nodes
this.flattenChildNodes(this.state.rootNode).forEach((node) => {
if (node.id !== undefined) {
this.nodeTable.set(node.id, node);
}
});

// Update rows
this.rows = this.nodes.map(node => this.options.rowRenderer(node, this.options));
Expand Down

0 comments on commit f8b1759

Please sign in to comment.