Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Jun 10, 2016
2 parents 21954fa + cc259c5 commit 7eb2e71
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 54 deletions.
12 changes: 8 additions & 4 deletions dist/infinite-tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! infinite-tree v1.5.0 | (c) 2016 Cheton Wu <[email protected]> | MIT | https://github.com/cheton/infinite-tree */
/*! infinite-tree v1.5.1 | (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 @@ -530,6 +530,10 @@ return /******/ (function(modules) { // webpackBootstrap
return false;
}

if (typeof index !== 'number') {
index = parentNode.children.length;
}

// Assign parent
newNodes.forEach(function (newNode) {
newNode.parent = parentNode;
Expand Down Expand Up @@ -2090,6 +2094,7 @@ return /******/ (function(modules) { // webpackBootstrap
// @param {boolean} [options.openAllNodes] True to open all nodes. Defaults to false.
// @param {array} [options.openNodes] An array that contains the ids of open nodes
// @return {array}
/* eslint no-console: 0 */
var flatten = function flatten() {
var nodes = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
Expand All @@ -2114,7 +2119,6 @@ return /******/ (function(modules) { // webpackBootstrap
parentNode = new _node2['default'](parentNode);
}
var rootNode = parentNode || new _node2['default']({ // defaults
label: '',
parent: null,
children: nodes,
state: {
Expand Down Expand Up @@ -2149,7 +2153,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (options.throwOnError) {
throw new Error('The node might have been corrupted: id=' + JSON.stringify(p.id) + ', state=' + JSON.stringify(p.state));
} else {
console && console.log('Error: The node might have been corrupted: id=%s, label=%s, parent=%s, children=%s, state=%s', JSON.stringify(p.id), JSON.stringify(p.label), p.parent, p.children, JSON.stringify(p.state));
console && console.log('Error: The node might have been corrupted: id=%s, parent=%s, children=%s, state=%s', JSON.stringify(p.id), p.parent, p.children, JSON.stringify(p.state));
}
}

Expand Down Expand Up @@ -2279,6 +2283,7 @@ return /******/ (function(modules) { // webpackBootstrap
'use strict';

exports.__esModule = true;
/* eslint no-restricted-syntax: 0 */
var extend = function extend(target) {
for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
sources[_key - 1] = arguments[_key];
Expand Down Expand Up @@ -2325,7 +2330,6 @@ return /******/ (function(modules) { // webpackBootstrap
_classCallCheck(this, Node);

this.id = null;
this.label = '';
this.parent = null;
this.children = [];
this.state = {};
Expand Down
6 changes: 3 additions & 3 deletions dist/infinite-tree.min.js

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": "1.5.0",
"version": "1.5.1",
"description": "A browser-ready tree library that can efficiently display a large amount of data using infinite scrolling.",
"homepage": "https://github.com/cheton/infinite-tree",
"main": "lib/index.js",
Expand Down
4 changes: 4 additions & 0 deletions src/infinite-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ class InfiniteTree extends events.EventEmitter {
return false;
}

if (typeof index !== 'number') {
index = parentNode.children.length;
}

// Assign parent
newNodes.forEach((newNode) => {
newNode.parent = parentNode;
Expand Down
164 changes: 118 additions & 46 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,88 @@ test('It should generate expected output for empty result', (t) => {
t.end();
});

test('tree.destroy', (t) => {
const el = getTreeElement();
const tree = new InfiniteTree(el, {
autoOpen: true,
data: { ...treeData }
});

t.notEqual(tree.clusterize, null);
t.notEqual(tree.contentElement, null);
t.notEqual(tree.scrollElement, null);
t.equal(tree.nodes.length, 12);
t.equal(tree.rows.length, 12);

tree.destroy();

t.equal(tree.clusterize, null);
t.equal(tree.contentElement, null);
t.equal(tree.scrollElement, null);
t.equal(tree.nodes.length, 0);
t.equal(tree.rows.length, 0);

t.end();
});

test('tree.addChildNodes', (t) => {
const el = getTreeElement();
const tree = new InfiniteTree(el, {
autoOpen: true,
data: { ...treeData }
});

const initialLength = tree.nodes.length;

{ // #1: Add a child node to the root node without specifying index
tree.addChildNodes({ id: 'new-node#1' });
t.equal(tree.nodes.length, initialLength + 1);
t.notEqual(tree.getNodeById('new-node#1'), null);
t.same(tree.getNodeById('new-node#1').getPreviousSibling(), tree.getNodeById('<root>'));
t.equal(tree.getNodeById('new-node#1').getNextSibling(), null);
}

{ // #2: Add a child node to the root node at the specified index
tree.addChildNodes({ id: 'new-node#2' }, 1);
t.equal(tree.nodes.length, initialLength + 2);
t.notEqual(tree.getNodeById('new-node#2'), null);
t.same(tree.getNodeById('new-node#2').getPreviousSibling(), tree.getNodeById('<root>'));
t.same(tree.getNodeById('new-node#2').getNextSibling(), tree.getNodeById('new-node#1'));
}

t.end();
});

test('tree.appendChildNode', (t) => {
const el = getTreeElement();
const tree = new InfiniteTree(el, {
autoOpen: true,
data: { ...treeData }
});

const initialLength = tree.nodes.length;

{ // #1: Append a child node to the root node
tree.appendChildNode({ id: 'new-node#1' });
t.equal(tree.nodes.length, initialLength + 1);
t.notEqual(tree.getNodeById('new-node#1'), null);
t.same(tree.getNodeById('new-node#1').getPreviousSibling(), tree.getNodeById('<root>'));
t.equal(tree.getNodeById('new-node#1').getNextSibling(), null);
}

t.end();
});

test('tree.clear', (t) => {
const el = getTreeElement();
const tree = new InfiniteTree(el, {
autoOpen: true,
data: { ...treeData }
});

t.same(tree.nodes.length, 12);
t.equal(tree.nodes.length, 12);
tree.clear();
t.same(tree.nodes.length, 0);
t.equal(tree.nodes.length, 0);

t.end();
});
Expand All @@ -94,23 +166,23 @@ test('tree.closeNode', (t) => {
});

// Close Node
t.same(tree.closeNode(), false);
t.same(tree.nodes.length, 12);
t.equal(tree.closeNode(), false);
t.equal(tree.nodes.length, 12);
tree.closeNode(tree.getNodeById('india'));
t.same(tree.nodes.length, 11);
t.equal(tree.nodes.length, 11);
tree.closeNode(tree.getNodeById('delta'));
t.same(tree.nodes.length, 9);
t.equal(tree.nodes.length, 9);
tree.closeNode(tree.getNodeById('hotel'));
t.same(tree.nodes.length, 8);
t.equal(tree.nodes.length, 8);
tree.closeNode(tree.getNodeById('charlie'));
t.same(tree.nodes.length, 6);
t.equal(tree.nodes.length, 6);
tree.closeNode(tree.getNodeById('bravo'));
t.same(tree.nodes.length, 3);
t.equal(tree.nodes.length, 3);
tree.closeNode(tree.getNodeById('<root>'), { silent: true }); // Prevent event from being triggered
t.same(tree.nodes.length, 1);
t.equal(tree.nodes.length, 1);

// Check event fired count
t.same(eventFiredCount, 5);
t.equal(eventFiredCount, 5);

t.end();
});
Expand Down Expand Up @@ -232,8 +304,8 @@ test('tree.getNodeById', (t) => {
const rootNode = tree.getNodeById('<root>');
const noneNode = tree.getNodeById('none');

t.same(rootNode.id, '<root>');
t.same(noneNode, null);
t.equal(rootNode.id, '<root>');
t.equal(noneNode, null);

t.end();
});
Expand Down Expand Up @@ -287,7 +359,7 @@ test('tree.getSelectedNode', (t) => {
});

const node = tree.getNodeById('<root>');
t.same(tree.selectNode(node), true);
t.equal(tree.selectNode(node), true);
t.same(tree.getSelectedNode(), node);

t.end();
Expand All @@ -307,23 +379,23 @@ test('tree.openNode', (t) => {
});

// Open Node
t.same(tree.openNode(), false);
t.same(tree.nodes.length, 1);
t.equal(tree.openNode(), false);
t.equal(tree.nodes.length, 1);
tree.openNode(tree.getNodeById('<root>'), { silent: true }); // Prevent event from being triggered
t.same(tree.nodes.length, 3);
t.equal(tree.nodes.length, 3);
tree.openNode(tree.getNodeById('bravo'));
t.same(tree.nodes.length, 6);
t.equal(tree.nodes.length, 6);
tree.openNode(tree.getNodeById('charlie'));
t.same(tree.nodes.length, 8);
t.equal(tree.nodes.length, 8);
tree.openNode(tree.getNodeById('hotel'));
t.same(tree.nodes.length, 9);
t.equal(tree.nodes.length, 9);
tree.openNode(tree.getNodeById('delta'));
t.same(tree.nodes.length, 11);
t.equal(tree.nodes.length, 11);
tree.openNode(tree.getNodeById('india'));
t.same(tree.nodes.length, 12);
t.equal(tree.nodes.length, 12);

// Check event fired count
t.same(eventFiredCount, 5);
t.equal(eventFiredCount, 5);

t.end();
});
Expand All @@ -336,16 +408,16 @@ test('tree.removeChildNodes', (t) => {
});

{ // #1: Pass empty parameters
t.same(tree.nodes.length, 12);
t.same(tree.removeChildNodes(), false);
t.same(tree.nodes.length, 12);
t.equal(tree.nodes.length, 12);
t.equal(tree.removeChildNodes(), false);
t.equal(tree.nodes.length, 12);
}

{ // #2: Remove a node
const node = tree.getNodeById('<root>');
t.same(tree.nodes.length, 12);
t.same(tree.removeChildNodes(node), true);
t.same(tree.nodes.length, 1);
t.equal(tree.nodes.length, 12);
t.equal(tree.removeChildNodes(node), true);
t.equal(tree.nodes.length, 1);
}

t.end();
Expand All @@ -359,16 +431,16 @@ test('tree.removeNode', (t) => {
});

{ // #1: Pass empty parameters
t.same(tree.nodes.length, 12);
t.same(tree.removeNode(), false);
t.same(tree.nodes.length, 12);
t.equal(tree.nodes.length, 12);
t.equal(tree.removeNode(), false);
t.equal(tree.nodes.length, 12);
}

{ // #2: Remove a node
const node = tree.getNodeById('<root>');
t.same(tree.nodes.length, 12);
t.same(tree.removeNode(node), true);
t.same(tree.nodes.length, 0);
t.equal(tree.nodes.length, 12);
t.equal(tree.removeNode(node), true);
t.equal(tree.nodes.length, 0);
}

t.end();
Expand Down Expand Up @@ -442,25 +514,25 @@ test('tree.toggleNode', (t) => {
});

// Toggle Node
t.same(tree.toggleNode(), false);
t.same(tree.nodes.length, 1);
t.equal(tree.toggleNode(), false);
t.equal(tree.nodes.length, 1);
tree.toggleNode(tree.getNodeById('<root>'), { silent: true }); // Prevent event from being triggered
t.same(tree.nodes.length, 3);
t.equal(tree.nodes.length, 3);
tree.toggleNode(tree.getNodeById('bravo'));
t.same(tree.nodes.length, 6);
t.equal(tree.nodes.length, 6);
tree.toggleNode(tree.getNodeById('charlie'));
t.same(tree.nodes.length, 8);
t.equal(tree.nodes.length, 8);
tree.toggleNode(tree.getNodeById('hotel'));
t.same(tree.nodes.length, 9);
t.equal(tree.nodes.length, 9);
tree.toggleNode(tree.getNodeById('delta'));
t.same(tree.nodes.length, 11);
t.equal(tree.nodes.length, 11);
tree.toggleNode(tree.getNodeById('india'));
t.same(tree.nodes.length, 12);
t.equal(tree.nodes.length, 12);
tree.toggleNode(tree.getNodeById('<root>'), { silent: true }); // Prevent event from being triggered
t.same(tree.nodes.length, 1);
t.equal(tree.nodes.length, 1);

// Check event fired count
t.same(eventFiredCount, 5);
t.equal(eventFiredCount, 5);

t.end();
});
Expand Down Expand Up @@ -504,7 +576,7 @@ test('tree.update', (t) => {
});

tree.update();
t.same(eventFiredCount, 2);
t.equal(eventFiredCount, 2);

t.end();
});
Expand Down

0 comments on commit 7eb2e71

Please sign in to comment.