Skip to content

Commit 5b7aa03

Browse files
Unfolded nodes are attached to the actual parent node
1 parent 1f8c736 commit 5b7aa03

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iknow-entity-browser",
3-
"version": "0.5.8",
3+
"version": "0.5.9",
44
"description": "Visualizer for iKnow entities",
55
"main": "gulpfile.babel.js",
66
"scripts": {

src/static/js/graph/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { onModelUpdate, unfold } from "../model";
1+
import { onModelUpdate, unfold, dropNodes } from "../model";
22
import { updateSelection, selectAll, deselectAll } from "../selection";
33

44
let shiftKey, ctrlKey,
@@ -226,10 +226,16 @@ export function update (g = lastGraph, reset = false) {
226226
.classed("selected", (p) => p.selected)
227227
.call(dragger)
228228
.on("dblclick", function (d) {
229+
if (d.type !== "folder")
230+
return;
229231
d3.event.stopPropagation();
230-
if (unfold(d)) {
232+
let left;
233+
if (d.parent && (left = unfold(d, d.parent))) {
231234
d.fx = d.x; d.fy = d.y;
232-
setTimeout(() => d.fx = d.fy = null, 500);
235+
setTimeout(() => d.fx = d.fy = null, 1000);
236+
}
237+
if (left === 0) {
238+
dropNodes(d);
233239
}
234240
})
235241
.on("click", function (d) {

src/static/js/model/index.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,35 @@ let SIZE_CRITERIA = "frequency",
1212
updateCallbacks = [];
1313

1414
function fold (tree) {
15-
if (!tree.children || tree.children.length <= MAX_CHILDREN)
16-
return tree;
1715
for (let cld of tree.children) {
1816
fold(cld);
1917
}
20-
let rest = tree.children.splice(MAX_CHILDREN);
21-
tree.children.push({
22-
label: `${ rest.length } more`,
23-
type: "folder",
24-
edgeType: rest[0].edgeType,
25-
_children: rest,
26-
children: [],
27-
radius: 10,
28-
entities: [],
29-
id: additionalNodeId--,
30-
parent: tree
31-
});
18+
if (tree.children && tree.children.length > MAX_CHILDREN) {
19+
let rest = tree.children.splice(MAX_CHILDREN);
20+
tree._children = rest;
21+
tree.children.push({
22+
label: `${ rest.length } more`,
23+
type: "folder",
24+
edgeType: rest[0].edgeType,
25+
children: [],
26+
radius: 10,
27+
entities: [],
28+
id: additionalNodeId--,
29+
parent: tree
30+
});
31+
}
32+
// let rest = tree.children.splice(MAX_CHILDREN);
33+
// tree.children.push({
34+
// label: `${ rest.length } more`,
35+
// type: "folder",
36+
// edgeType: rest[0].edgeType,
37+
// _children: rest,
38+
// children: [],
39+
// radius: 10,
40+
// entities: [],
41+
// id: additionalNodeId--,
42+
// parent: tree
43+
// });
3244
return tree;
3345
}
3446

@@ -232,21 +244,23 @@ function resetChildrenPosition (parent, children = []) {
232244

233245
/**
234246
* Unfold the folder by specified amount of nodes.
247+
* @param folderNode - Node representing a folder
235248
* @param node - Node to unfold
236249
* @param [children=20] - Number of nodes to unfold
237250
* @returns {number} - Number of nodes left to unfold
238251
*/
239-
export function unfold (node, children = 20) {
240-
if (node.type !== "folder" || !node._children || !node._children.length)
252+
export function unfold (folderNode, node, children = 20) {
253+
if (folderNode.type !== "folder" || !node._children || !node._children.length)
241254
return 0;
242255
let newId = additionalNodeId--,
243-
oldId = node.id,
256+
oldId = folderNode.id,
244257
f = () => {
245-
let next = node._children.splice(0, children),
246-
left = node._children.length;
247-
node.id = newId;
258+
let next = node._children.slice(0, children);
259+
node._children = node._children.slice(children);
260+
let left = node._children.length;
261+
folderNode.id = newId;
248262
node.children = node.children.concat(next);
249-
node.label = left > 0 ? `${ left } more` : `Others`;
263+
folderNode.label = left > 0 ? `${ left } more` : `Others`;
250264
resetChildrenPosition(node, next);
251265
dataUpdated();
252266
return {
@@ -258,10 +272,12 @@ export function unfold (node, children = 20) {
258272
history.createState({
259273
redo: f,
260274
undo: () => {
261-
let part = node.children.splice(-res.unfolded);
275+
let part = node.children.slice(-res.unfolded);
276+
node.children = node.children.slice(0, node.children.length - part.length);
262277
node._children = part.concat(node._children);
263-
node.id = oldId;
264-
node.label = node._children.length > 0 ? `${ node._children.length } more` : `Others`;
278+
folderNode.id = oldId;
279+
folderNode.label =
280+
node._children.length > 0 ? `${ node._children.length } more` : `Others`;
265281
dataUpdated();
266282
}
267283
});

0 commit comments

Comments
 (0)