Skip to content

Commit 07cec31

Browse files
committed
Use shouldLoadNodes option to determine whether to load nodes on-demand (#30)
1 parent 0dced25 commit 07cec31

File tree

8 files changed

+76
-23
lines changed

8 files changed

+76
-23
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ const tree = new InfiniteTree({
6666
drop: function(event, options) {
6767
}
6868
},
69-
loadNodes: function(parentNode, next) { // Load node on demand
69+
shouldLoadNodes: function(parentNode) {
70+
if (!parentNode.hasChildren() && parentNode.loadOnDemand) {
71+
return true;
72+
}
73+
return false;
74+
},
75+
loadNodes: function(parentNode, next) {
7076
// Loading...
7177
const nodes = [];
7278
nodes.length = 1000;

dist/infinite-tree.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! infinite-tree v1.15.0 | (c) 2018 Cheton Wu <cheton@gmail.com> | MIT | https://github.com/cheton/infinite-tree */
1+
/*! infinite-tree v1.16.0 | (c) 2018 Cheton Wu <cheton@gmail.com> | MIT | https://github.com/cheton/infinite-tree */
22
(function webpackUniversalModuleDefinition(root, factory) {
33
if(typeof exports === 'object' && typeof module === 'object')
44
module.exports = factory();
@@ -919,6 +919,7 @@ var InfiniteTree = function (_events$EventEmitter) {
919919
_this.options = {
920920
autoOpen: false,
921921
droppable: false,
922+
shouldLoadNodes: null,
922923
loadNodes: null,
923924
rowRenderer: _renderer.defaultRowRenderer,
924925
selectable: true,
@@ -2068,7 +2069,9 @@ var InfiniteTree = function (_events$EventEmitter) {
20682069
return true;
20692070
}
20702071

2071-
if (!node.hasChildren() && node.loadOnDemand) {
2072+
var shouldLoadNodes = typeof this.options.shouldLoadNodes === 'function' ? !!this.options.shouldLoadNodes(node) : !node.hasChildren() && node.loadOnDemand;
2073+
2074+
if (shouldLoadNodes) {
20722075
if (typeof this.options.loadNodes !== 'function') {
20732076
return false;
20742077
}

dist/infinite-tree.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/common.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "infinite-tree",
3-
"version": "1.15.0",
3+
"version": "1.16.0",
44
"description": "A browser-ready tree library that can efficiently display a large amount of data using infinite scrolling.",
55
"homepage": "https://github.com/cheton/infinite-tree",
66
"main": "lib/index.js",
@@ -44,32 +44,32 @@
4444
"devDependencies": {
4545
"babel-cli": "^6.26.0",
4646
"babel-core": "^6.26.0",
47-
"babel-eslint": "^8.2.1",
48-
"babel-loader": "^7.1.2",
47+
"babel-eslint": "^8.2.2",
48+
"babel-loader": "^7.1.4",
4949
"babel-plugin-transform-es3-member-expression-literals": "^6.22.0",
5050
"babel-plugin-transform-es3-property-literals": "^6.22.0",
5151
"babel-preset-env": "^1.6.1",
5252
"babel-preset-stage-0": "^6.24.1",
5353
"coveralls": "^3.0.0",
54-
"css-loader": "^0.28.9",
55-
"eslint": "^4.15.0",
54+
"css-loader": "^0.28.11",
55+
"eslint": "^4.19.1",
5656
"eslint-config-trendmicro": "^1.3.0",
5757
"eslint-import-resolver-webpack": "^0.8.4",
58-
"eslint-loader": "^1.9.0",
59-
"eslint-plugin-import": "^2.8.0",
58+
"eslint-loader": "^2.0.0",
59+
"eslint-plugin-import": "^2.9.0",
6060
"eslint-plugin-jsx-a11y": "^6.0.3",
61-
"eslint-plugin-react": "^7.5.1",
62-
"file-loader": "^1.1.6",
63-
"jsdom": "^11.5.1",
61+
"eslint-plugin-react": "^7.7.0",
62+
"file-loader": "^1.1.11",
63+
"jsdom": "^11.6.2",
6464
"json-loader": "^0.5.7",
65-
"lodash": "^4.17.4",
65+
"lodash": "^4.17.5",
6666
"nib": "^1.1.2",
67-
"style-loader": "^0.19.1",
67+
"style-loader": "^0.20.3",
6868
"stylint": "^1.5.9",
6969
"stylus": "^0.54.5",
70-
"stylus-loader": "^3.0.1",
71-
"tap": "^11.0.1",
72-
"url-loader": "^0.6.2",
70+
"stylus-loader": "^3.0.2",
71+
"tap": "^11.1.3",
72+
"url-loader": "^1.0.1",
7373
"webpack": "^3.10.0",
7474
"webpack-dev-server": "^2.11.0"
7575
},

src/infinite-tree.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class InfiniteTree extends events.EventEmitter {
6666
options = {
6767
autoOpen: false,
6868
droppable: false,
69+
shouldLoadNodes: null,
6970
loadNodes: null,
7071
rowRenderer: defaultRowRenderer,
7172
selectable: true,
@@ -1150,7 +1151,11 @@ class InfiniteTree extends events.EventEmitter {
11501151
return true;
11511152
}
11521153

1153-
if (!node.hasChildren() && node.loadOnDemand) {
1154+
const shouldLoadNodes = (typeof this.options.shouldLoadNodes === 'function')
1155+
? !!(this.options.shouldLoadNodes(node))
1156+
: !node.hasChildren() && node.loadOnDemand;
1157+
1158+
if (shouldLoadNodes) {
11541159
if (typeof this.options.loadNodes !== 'function') {
11551160
return false;
11561161
}

test/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,45 @@ test('loadOnDemand', (t) => {
164164
t.ok(tree.openNode(tree.getNodeById('<root>')));
165165
});
166166

167+
test('shouldLoadNodes', (t) => {
168+
const el = getTreeElement();
169+
const tree = new InfiniteTree(el, {
170+
autoOpen: true,
171+
data: {
172+
id: '<root>',
173+
loadNodes: true
174+
},
175+
shouldLoadNodes: (node) => {
176+
return !node.hasChildren() && node.loadNodes;
177+
},
178+
loadNodes: (node, next) => {
179+
t.equal(tree.getNodeById('<root>').getChildren().length, 0);
180+
181+
// Asynchronous
182+
setTimeout(() => {
183+
next(null, [], () => {
184+
t.equal(tree.getNodeById('<root>').getChildren().length, 0);
185+
t.equal(tree.state.openNodes.length, 1);
186+
t.equal(node.state.open, true);
187+
t.end();
188+
});
189+
}, 250);
190+
191+
// Asynchronous
192+
setTimeout(() => {
193+
const data = getTreeData();
194+
next(null, data.children, () => {
195+
t.equal(tree.getNodeById('<root>').getChildren().length, 2);
196+
t.equal(tree.state.openNodes.length, 1);
197+
t.end();
198+
});
199+
}, 500);
200+
}
201+
});
202+
203+
t.ok(tree.openNode(tree.getNodeById('<root>')));
204+
});
205+
167206
test('tree.destroy', (t) => {
168207
const el = getTreeElement();
169208
const tree = new InfiniteTree(el, {

0 commit comments

Comments
 (0)