Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Apr 7, 2016
2 parents 93b7d26 + 64d4aab commit 5e0a65f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 94 deletions.
99 changes: 55 additions & 44 deletions dist/infinite-tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! infinite-tree v0.7.0 | (c) 2016 Cheton Wu <[email protected]> | MIT | https://github.com/cheton/infinite-tree */
/*! infinite-tree v0.8.0 | (c) 2016 Cheton Wu <[email protected]> | MIT | https://github.com/cheton/infinite-tree */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -366,72 +366,82 @@ return /******/ (function(modules) { // webpackBootstrap
this.contentElement = null;
this.scrollElement = null;
};
// Inserts a new child node to a parent node at the specified index.
// * If the parent is null or undefined, inserts the child at the specified index in the top-level.
// * If the parent has children, the method adds the child to it at the specified index.
// * If the parent does not have children, the method adds the child to the parent.
// Adds an array of new child nodes to a parent node at the specified index.
// * If the parent is null or undefined, inserts new childs at the specified index in the top-level.
// * If the parent has children, the method adds the new child to it at the specified index.
// * If the parent does not have children, the method adds the new child to the parent.
// * If the index value is greater than or equal to the number of children in the parent, the method adds the child at the end of the children.
// @param {Object} newNode The new child node.
// @param {number} index The 0-based index of where to insert the child node. Defaults to 0 for negative index.
// @param {Array} newNodes An array of new child nodes.
// @param {number} [index] The 0-based index of where to insert the child node.
// @param {Node} parentNode The Node object that defines the parent node.
// @return {boolean} Returns true on success, false otherwise.


InfiniteTree.prototype.addChildNodeAt = function addChildNodeAt(newNode, index, parentNode) {
InfiniteTree.prototype.addChildNodes = function addChildNodes(newNodes, index, parentNode) {
var _this3 = this;

// Defaults to rootNode if the parentNode is not specified
parentNode = parentNode || this.state.rootNode;

ensureNodeInstance(parentNode);

if (!newNode) {
newNodes = [].concat(newNodes || []); // Ensure array
if (newNodes.length === 0) {
return false;
}
index = Number(index) || 0;
if (index < 0) {
index = 0;
}

// Inserts the new child at the specified index
newNode.parent = parentNode;
parentNode.children.splice(index, 0, newNode);
if ((typeof index === 'undefined' ? 'undefined' : _typeof(index)) === 'object') {
// The 'object' type might be Node or null
parentNode = index || this.state.rootNode; // Defaults to rootNode if not specified
index = parentNode.children.length;
} else {
parentNode = parentNode || this.state.rootNode; // Defaults to rootNode if not specified
}

var deleteCount = parentNode.state.total;
ensureNodeInstance(parentNode);

// Update index
index = parentNode.children.indexOf(newNode);
// Assign parent
newNodes.forEach(function (newNode) {
newNode.parent = parentNode;
});

var nodes = (0, _flattree.flatten)(parentNode.children, { openNodes: this.state.openNodes });
// Insert new child node at the specified index
parentNode.children.splice.apply(parentNode.children, [index, 0].concat(newNodes));

// Update newNode
newNode = parentNode.getChildAt(index);
// Get the index of the first new node within the array of child nodes
index = parentNode.children.indexOf(newNodes[0]);

var deleteCount = parentNode.state.total;
var nodes = (0, _flattree.flatten)(parentNode.children, { openNodes: this.state.openNodes });
var rows = nodes.map(function (node) {
return _this3.options.rowRenderer(node, _this3.options);
});
var parentOffset = this.nodes.indexOf(parentNode);

// Update nodes & rows
this.nodes.splice.apply(this.nodes, [parentOffset + 1, deleteCount].concat(nodes));
this.rows.splice.apply(this.rows, [parentOffset + 1, deleteCount].concat(rows));
if (parentNode === this.state.rootNode) {
this.nodes = nodes;
this.rows = rows;
} else {
var parentOffset = this.nodes.indexOf(parentNode);
if (parentOffset >= 0) {
// Update nodes & rows
this.nodes.splice.apply(this.nodes, [parentOffset + 1, deleteCount].concat(nodes));
this.rows.splice.apply(this.rows, [parentOffset + 1, deleteCount].concat(rows));

// Update the row corresponding to the parent node
this.rows[parentOffset] = this.options.rowRenderer(parentNode, this.options);
}
}

// Update the lookup table with newly added nodes
this.flattenNode(newNode).forEach(function (node) {
if (node.id !== undefined) {
_this3.nodeTable.set(node.id, node);
}
parentNode.children.slice(index).forEach(function (childNode) {
_this3.flattenNode(childNode).forEach(function (node) {
if (node.id !== undefined) {
_this3.nodeTable.set(node.id, node);
}
});
});

// Update the row corresponding to the parent node
this.rows[parentOffset] = this.options.rowRenderer(parentNode, this.options);

// Updates list with new data
this.update();

return true;
};
// Adds a node to the end of the list of children of a specified parent node.
// Adds a new child node to the end of the list of children of a specified parent node.
// * If the parent is null or undefined, inserts the child at the specified index in the top-level.
// * If the parent has children, the method adds the child as the last child.
// * If the parent does not have children, the method adds the child to the parent.
Expand All @@ -443,11 +453,10 @@ return /******/ (function(modules) { // webpackBootstrap
InfiniteTree.prototype.appendChildNode = function appendChildNode(newNode, parentNode) {
// Defaults to rootNode if the parentNode is not specified
parentNode = parentNode || this.state.rootNode;

ensureNodeInstance(parentNode);

var index = parentNode.children.length;
return this.addChildNodeAt(newNode, index, parentNode);
var newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
};
// Clears the tree.

Expand Down Expand Up @@ -631,7 +640,8 @@ return /******/ (function(modules) { // webpackBootstrap
ensureNodeInstance(referenceNode);
var parentNode = referenceNode.getParent();
var index = parentNode.children.indexOf(referenceNode) + 1;
return this.addChildNodeAt(newNode, index, parentNode);
var newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
};
// Inserts the specified node before the reference node.
// @param {Object} newNode The new sibling node.
Expand All @@ -643,7 +653,8 @@ return /******/ (function(modules) { // webpackBootstrap
ensureNodeInstance(referenceNode);
var parentNode = referenceNode.getParent();
var index = parentNode.children.indexOf(referenceNode);
return this.addChildNodeAt(newNode, index, parentNode);
var newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
};
// Loads data in the tree.
// @param {object|array} data The data is an object or array of objects that defines the node.
Expand Down
6 changes: 3 additions & 3 deletions dist/infinite-tree.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "infinite-tree",
"version": "0.7.0",
"version": "0.8.0",
"description": "A browser-ready tree library that can efficiently display a large tree with smooth scrolling.",
"homepage": "https://github.com/cheton/infinite-tree",
"main": "lib/index.js",
Expand Down
96 changes: 53 additions & 43 deletions src/infinite-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,66 +269,75 @@ class InfiniteTree extends events.EventEmitter {
this.contentElement = null;
this.scrollElement = null;
}
// Inserts a new child node to a parent node at the specified index.
// * If the parent is null or undefined, inserts the child at the specified index in the top-level.
// * If the parent has children, the method adds the child to it at the specified index.
// * If the parent does not have children, the method adds the child to the parent.
// Adds an array of new child nodes to a parent node at the specified index.
// * If the parent is null or undefined, inserts new childs at the specified index in the top-level.
// * If the parent has children, the method adds the new child to it at the specified index.
// * If the parent does not have children, the method adds the new child to the parent.
// * If the index value is greater than or equal to the number of children in the parent, the method adds the child at the end of the children.
// @param {Object} newNode The new child node.
// @param {number} index The 0-based index of where to insert the child node. Defaults to 0 for negative index.
// @param {Array} newNodes An array of new child nodes.
// @param {number} [index] The 0-based index of where to insert the child node.
// @param {Node} parentNode The Node object that defines the parent node.
// @return {boolean} Returns true on success, false otherwise.
addChildNodeAt(newNode, index, parentNode) {
// Defaults to rootNode if the parentNode is not specified
parentNode = parentNode || this.state.rootNode;

ensureNodeInstance(parentNode);

if (!newNode) {
addChildNodes(newNodes, index, parentNode) {
newNodes = [].concat(newNodes || []); // Ensure array
if (newNodes.length === 0) {
return false;
}
index = Number(index) || 0;
if (index < 0) {
index = 0;
}

// Inserts the new child at the specified index
newNode.parent = parentNode;
parentNode.children.splice(index, 0, newNode);
if (typeof index === 'object') { // The 'object' type might be Node or null
parentNode = index || this.state.rootNode; // Defaults to rootNode if not specified
index = parentNode.children.length;
} else {
parentNode = parentNode || this.state.rootNode; // Defaults to rootNode if not specified
}

const deleteCount = parentNode.state.total;
ensureNodeInstance(parentNode);

// Update index
index = parentNode.children.indexOf(newNode);
// Assign parent
newNodes.forEach((newNode) => {
newNode.parent = parentNode;
});

const nodes = flatten(parentNode.children, { openNodes: this.state.openNodes });
// Insert new child node at the specified index
parentNode.children.splice.apply(parentNode.children, [index, 0].concat(newNodes));

// Update newNode
newNode = parentNode.getChildAt(index);
// Get the index of the first new node within the array of child nodes
index = parentNode.children.indexOf(newNodes[0]);

const deleteCount = parentNode.state.total;
const nodes = flatten(parentNode.children, { openNodes: this.state.openNodes });
const rows = nodes.map(node => this.options.rowRenderer(node, this.options));
const parentOffset = this.nodes.indexOf(parentNode);

// Update nodes & rows
this.nodes.splice.apply(this.nodes, [parentOffset + 1, deleteCount].concat(nodes));
this.rows.splice.apply(this.rows, [parentOffset + 1, deleteCount].concat(rows));
if (parentNode === this.state.rootNode) {
this.nodes = nodes;
this.rows = rows;
} else {
const parentOffset = this.nodes.indexOf(parentNode);
if (parentOffset >= 0) {
// Update nodes & rows
this.nodes.splice.apply(this.nodes, [parentOffset + 1, deleteCount].concat(nodes));
this.rows.splice.apply(this.rows, [parentOffset + 1, deleteCount].concat(rows));

// Update the row corresponding to the parent node
this.rows[parentOffset] = this.options.rowRenderer(parentNode, this.options);
}
}

// Update the lookup table with newly added nodes
this.flattenNode(newNode).forEach((node) => {
if (node.id !== undefined) {
this.nodeTable.set(node.id, node);
}
parentNode.children.slice(index).forEach((childNode) => {
this.flattenNode(childNode).forEach((node) => {
if (node.id !== undefined) {
this.nodeTable.set(node.id, node);
}
});
});

// Update the row corresponding to the parent node
this.rows[parentOffset] = this.options.rowRenderer(parentNode, this.options);

// Updates list with new data
this.update();

return true;
}
// Adds a node to the end of the list of children of a specified parent node.
// Adds a new child node to the end of the list of children of a specified parent node.
// * If the parent is null or undefined, inserts the child at the specified index in the top-level.
// * If the parent has children, the method adds the child as the last child.
// * If the parent does not have children, the method adds the child to the parent.
Expand All @@ -338,11 +347,10 @@ class InfiniteTree extends events.EventEmitter {
appendChildNode(newNode, parentNode) {
// Defaults to rootNode if the parentNode is not specified
parentNode = parentNode || this.state.rootNode;

ensureNodeInstance(parentNode);

const index = parentNode.children.length;
return this.addChildNodeAt(newNode, index, parentNode);
const newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
}
// Clears the tree.
clear() {
Expand Down Expand Up @@ -502,7 +510,8 @@ class InfiniteTree extends events.EventEmitter {
ensureNodeInstance(referenceNode);
const parentNode = referenceNode.getParent();
const index = parentNode.children.indexOf(referenceNode) + 1;
return this.addChildNodeAt(newNode, index, parentNode);
const newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
}
// Inserts the specified node before the reference node.
// @param {Object} newNode The new sibling node.
Expand All @@ -512,7 +521,8 @@ class InfiniteTree extends events.EventEmitter {
ensureNodeInstance(referenceNode);
const parentNode = referenceNode.getParent();
const index = parentNode.children.indexOf(referenceNode);
return this.addChildNodeAt(newNode, index, parentNode);
const newNodes = [].concat(newNode || []); // Ensure array
return this.addChildNodes(newNodes, index, parentNode);
}
// Loads data in the tree.
// @param {object|array} data The data is an object or array of objects that defines the node.
Expand Down

0 comments on commit 5e0a65f

Please sign in to comment.