Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
fix: merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseeborn committed Feb 17, 2020
2 parents 2de96fc + 5afe3e2 commit 5aebaa2
Show file tree
Hide file tree
Showing 9 changed files with 891 additions and 582 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@nebula.js/supernova": "0.1.0-alpha.30"
},
"devDependencies": {
"@after-work.js/aw": "6.0.10",
"@after-work.js/aw": "6.0.11",
"@babel/cli": "7.8.4",
"@babel/core": "7.8.4",
"@babel/preset-env": "7.8.4",
Expand All @@ -63,7 +63,7 @@
"eslint-config-airbnb-base": "14.0.0",
"eslint-plugin-import": "2.20.1",
"eslint-plugin-mocha": "6.2.2",
"husky": "4.2.1",
"husky": "4.2.3",
"lint-staged": "10.0.7",
"mocha-junit-reporter": "1.23.3",
"shx": "^0.3.2",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function supernova(env) {
},
icon: {
d:
'M9.5,15 C9.77614237,15 10,15.2238576 10,15.5 C10,15.7761424 9.77614237,16 9.5,16 L6.5,16 C6.22385763,16 6,15.7761424 6,15.5 C6,15.2238576 6.22385763,15 6.5,15 L9.5,15 Z M15.5,13 C15.7761424,13 16,13.2238576 16,13.5 L16,15 C16,15.5522847 15.5522847,16 15,16 L13.5,16 C13.2238576,16 13,15.7761424 13,15.5 C13,15.2238576 13.2238576,15 13.5,15 L14.4,15 C14.7313708,15 15,14.7313708 15,14.4 L15,13.5 C15,13.2238576 15.2238576,13 15.5,13 Z M0.5,13 C0.776142375,13 1,13.2238576 1,13.5 L1,14.4 C1,14.7313708 1.26862915,15 1.6,15 L2.5,15 C2.77614237,15 3,15.2238576 3,15.5 C3,15.7761424 2.77614237,16 2.5,16 L1,16 C0.44771525,16 1.28867715e-16,15.5522847 6.123234e-17,15 L1.5308085e-16,13.5 C1.19263162e-16,13.2238576 0.223857625,13 0.5,13 Z M7.5,2.5 L7.5,5.5 L5.5,5.5 L5.5,7.5 L7,7.5 L7,6.5 L12,6.5 L12,9.5 L7,9.5 L7,8.5 L5.5,8.5 L5.5,11.2 C5.5,11.3420161 5.5986801,11.4609842 5.73121275,11.4920768 L5.8,11.5 L9,11.5 L9,10.5 L14,10.5 L14,13.5 L9,13.5 L9,12.5 L5.8,12.5 C5.12690296,12.5 4.57328475,11.9884503 4.50671175,11.3329174 L4.5,11.2 L4.5,5.5 L2.5,5.5 L2.5,2.5 L7.5,2.5 Z M0.5,6 C0.776142375,6 1,6.22385763 1,6.5 L1,9.5 C1,9.77614237 0.776142375,10 0.5,10 C0.223857625,10 0,9.77614237 0,9.5 L0,6.5 C0,6.22385763 0.223857625,6 0.5,6 Z M15.5,6 C15.7761424,6 16,6.22385763 16,6.5 L16,9.5 C16,9.77614237 15.7761424,10 15.5,10 C15.2238576,10 15,9.77614237 15,9.5 L15,6.5 C15,6.22385763 15.2238576,6 15.5,6 Z M2.5,2.48689958e-14 C2.77614237,2.48689958e-14 3,0.223857625 3,0.5 C3,0.776142375 2.77614237,1 2.5,1 L1.6,1 C1.26862915,1 1,1.26862915 1,1.6 L1,2.5 C1,2.77614237 0.776142375,3 0.5,3 C0.223857625,3 0,2.77614237 0,2.5 L0,1 C0,0.44771525 0.44771525,2.48689958e-14 1,2.48689958e-14 L2.5,2.48689958e-14 Z M15,2.48689958e-14 C15.5522847,2.48689958e-14 16,0.44771525 16,1 L16,2.5 C16,2.77614237 15.7761424,3 15.5,3 C15.2238576,3 15,2.77614237 15,2.5 L15,1.6 C15,1.26862915 14.7313708,1 14.4,1 L13.5,1 C13.2238576,1 13,0.776142375 13,0.5 C13,0.223857625 13.2238576,2.48689958e-14 13.5,2.48689958e-14 L15,2.48689958e-14 Z M9.5,2.51445413e-14 C9.77614237,2.51614501e-14 10,0.223857625 10,0.5 C10,0.776142375 9.77614237,1 9.5,1 L6.5,1 C6.22385763,1 6,0.776142375 6,0.5 C6,0.223857625 6.22385763,2.45765414e-14 6.5,2.45934502e-14 L9.5,2.51445413e-14 Z',
'M8,3 L8,6 L6,6 L6,9 L7.492,9 L7.49291992,8 L12.4929199,8 L12.4929199,11 L7.49291992,11 L7.492,10 L6,10 L6,13.7 C6,13.8420161 6.0986801,13.9609842 6.23121275,13.9920768 L6.3,14 L9.5,14 L9.5,13 L14.5,13 L14.5,16 L9.5,16 L9.5,15 L6.3,15 C5.62690296,15 5.07328475,14.4884503 5.00671175,13.8329174 L5,13.7 L5,6 L3,6 L3,3 L8,3 Z M0.5,13 C0.776142375,13 1,13.2238576 1,13.5 L1,14.4 C1,14.7313708 1.26862915,15 1.6,15 L2.5,15 C2.77614237,15 3,15.2238576 3,15.5 C3,15.7761424 2.77614237,16 2.5,16 L1,16 C0.44771525,16 0,15.5522847 0,15 L0,13.5 C0,13.2238576 0.223857625,13 0.5,13 Z M0.5,6 C0.776142375,6 1,6.22385763 1,6.5 L1,9.5 C1,9.77614237 0.776142375,10 0.5,10 C0.223857625,10 0,9.77614237 0,9.5 L0,6.5 C0,6.22385763 0.223857625,6 0.5,6 Z M15.5,6 C15.7761424,6 16,6.22385763 16,6.5 L16,9.5 C16,9.77614237 15.7761424,10 15.5,10 C15.2238576,10 15,9.77614237 15,9.5 L15,6.5 C15,6.22385763 15.2238576,6 15.5,6 Z M2.5,0 C2.77614237,0 3,0.223857625 3,0.5 C3,0.776142375 2.77614237,1 2.5,1 L1.6,1 C1.26862915,1 1,1.26862915 1,1.6 L1,2.5 C1,2.77614237 0.776142375,3 0.5,3 C0.223857625,3 0,2.77614237 0,2.5 L0,1 C0,0.44771525 0.44771525,0 1,0 L2.5,0 Z M15,-4.4408921e-16 C15.5522847,-4.4408921e-16 16,0.44771525 16,1 L16,2.5 C16,2.77614237 15.7761424,3 15.5,3 C15.2238576,3 15,2.77614237 15,2.5 L15,1.6 C15,1.26862915 14.7313708,1 14.4,1 L13.5,1 C13.2238576,1 13,0.776142375 13,0.5 C13,0.223857625 13.2238576,-4.4408921e-16 13.5,-4.4408921e-16 L15,-4.4408921e-16 Z M9.5,-2.22044605e-16 C9.77614237,-2.22044605e-16 10,0.223857625 10,0.5 C10,0.776142375 9.77614237,1 9.5,1 L6.5,1 C6.22385763,1 6,0.776142375 6,0.5 C6,0.223857625 6.22385763,-2.22044605e-16 6.5,-2.22044605e-16 L9.5,-2.22044605e-16 Z',
},
active: linked,
}),
Expand Down
99 changes: 99 additions & 0 deletions src/tree/__tests__/render.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { hierarchy } from 'd3';
import { filterTree } from '../render';

const nodes = {
id: '1',
elemNo: 1,
children: [
{
id: '2',
elemNo: 2,
children: [
{
id: '3',
elemNo: 3,
parent: {
data: {
id: '2',
},
},
children: [
{
id: '5',
elemNo: 798,
parent: {
id: '3',
},
},
],
},
],
},
{
id: '4',
elemNo: 88,
children: [],
},
],
};

describe('render', () => {
describe('filterTree', () => {
let topId;
let isExpanded;
let expandedChildren;
let nodeTree;
beforeEach(() => {
topId = '1';
isExpanded = false;
expandedChildren = [];
nodeTree = hierarchy(nodes);
});

it('should return the only top node', () => {
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(1);
expect(result[0].data.id).to.equal('1');
});

it('should return only the top node when topId not found', () => {
topId = 'someInsaneId';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(1);
expect(result[0].data.id).to.equal('1');
});

it('should return the top node and children', () => {
isExpanded = true;
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(3);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4']);
});

it('should return the top node and children and grandchildren of expanded', () => {
isExpanded = true;
expandedChildren = ['2'];
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(4);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '3', '4']);
});

it('should return the top node and children with navigation mode free', () => {
nodeTree.data.navigationMode = 'free';
isExpanded = true;
topId = '2';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(4);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4', '3']);
});

it('should return the top node and parents children with navigation mode free', () => {
nodeTree.data.navigationMode = 'free';
isExpanded = true;
topId = '5';
const result = filterTree({ topId, isExpanded, expandedChildren }, nodeTree);
expect(result.length).to.equal(5);
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4', '3', '5']);
});
});
});
61 changes: 36 additions & 25 deletions src/tree/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import selections from '../utils/selections';
import { haveNoChildren } from '../utils/tree-utils';
import constants from './size-constants';

export const getSign = (d, { topId, isExpanded, expandedChildren }) => {
export const getSign = (d, { topId, isExpanded, expandedChildren }, ancestorIds) => {
if (
(d.data.id === topId && isExpanded) ||
(d.parent && d.parent.data.id === topId && expandedChildren.includes(d.data.id))
(d.parent && d.parent.data.id === topId && expandedChildren.includes(d.data.id)) ||
ancestorIds.includes(d.data.id)
) {
return '-';
}

return '+';
};

export const getNewState = (d, { topId, isExpanded, expandedChildren }) => {
export const getNewState = (d, { topId, isExpanded, expandedChildren }, ancestorIds) => {
if (d.data.id === topId) {
// top
isExpanded = !isExpanded;
expandedChildren = [];
} else if (ancestorIds.includes(d.data.id)) {
// ancestors
topId = d.parent ? d.parent.data.id : d.data.id;
isExpanded = !!d.parent;
expandedChildren = [];
} else if (d.parent.data.id === topId) {
// children
const expandedHaveNoChildren = d.parent.children
Expand Down Expand Up @@ -60,10 +66,13 @@ export default function box(
setStateCallback,
selectionState,
sel,
allowInteractions
allowInteractions,
navigationMode
) {
const { cardWidth, cardHeight, buttonWidth, buttonHeight, buttonMargin, rootDiameter, tooltipWidth, tooltipHeight } = constants;
const { topId, isExpanded } = expandedState;
const topNode = nodes.find(node => node.data.id === topId);
const ancestorIds = topNode.parent ? topNode.parent.ancestors().map(anc => anc.data.id) : [];

// dummy root
divBox
Expand Down Expand Up @@ -130,29 +139,31 @@ export default function box(
.attr('id', d => `${d.data.id}-expand`)
.on('click', d => {
if (allowInteractions) {
setStateCallback(getNewState(d, expandedState));
setStateCallback(getNewState(d, expandedState, ancestorIds));
}
})
.html(d => `${getSign(d, expandedState)} ${d.data.children.length}`);
.html(d => `${getSign(d, expandedState, ancestorIds)} ${d.data.children.length}`);

// go up
divBox
.selectAll('.sn-org-nodes')
.data(nodes.filter(node => node.data.id === topId && node.parent))
.enter()
.append('div')
.attr('class', 'sn-org-traverse')
.attr(
'style',
d =>
`width:${buttonWidth}px;height:${buttonHeight}px;top:${y(d) - buttonHeight - buttonMargin}px;left:${x(d) +
(cardWidth - buttonWidth) / 2}px;`
)
.attr('id', d => `${d.data.id}-up`)
.on('click', d => {
if (allowInteractions) {
setStateCallback(getNewUpState(d, isExpanded));
}
})
.html('↑');
if (navigationMode !== 'free') {
divBox
.selectAll('.sn-org-nodes')
.data(nodes.filter(node => node.data.id === topId && node.parent))
.enter()
.append('div')
.attr('class', 'sn-org-traverse')
.attr(
'style',
d =>
`width:${buttonWidth}px;height:${buttonHeight}px;top:${y(d) - buttonHeight - buttonMargin}px;left:${x(d) +
(cardWidth - buttonWidth) / 2}px;`
)
.attr('id', d => `${d.data.id}-up`)
.on('click', d => {
if (allowInteractions) {
setStateCallback(getNewUpState(d, isExpanded));
}
})
.html('↑');
}
}
49 changes: 34 additions & 15 deletions src/tree/box.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ describe('box', () => {
};
});
it('should return - when d is the top node and is expanded', () => {
expect(getSign(d, expandedState)).to.equal('-');
expect(getSign(d, expandedState, [])).to.equal('-');
});
it('should return - when d is the child of the top node and is expanded', () => {
d = d.children[0];
expandedState.expandedChildren.push('2');
expect(getSign(d, expandedState)).to.equal('-');
expect(getSign(d, expandedState, [])).to.equal('-');
});
it('should return + not expanded', () => {
expandedState.isExpanded = false;
expect(getSign(d, expandedState)).to.equal('+');
expect(getSign(d, expandedState, [])).to.equal('+');
});
it('should return - not expanded when in ancestor IDs', () => {
expandedState.isExpanded = false;
d = { data: { id: '1982' } };
expect(getSign(d, expandedState, ['1982'])).to.equal('-');
});
});

Expand All @@ -46,17 +51,17 @@ describe('box', () => {
});
it('should return state with same id, toggled isExpanded and no expanded children', () => {
expectedState.isExpanded = false;
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return state with same id, isExpanded true and no expanded children', () => {
d = d.children[0];
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return state with same id, isExpanded true and added expanded child', () => {
d = d.children[1];
d.parent.children[0].children = undefined;
expectedState.expandedChildren = ['2', '4'];
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return state with same id, isExpanded true and added expanded child - even when one sibling has grend children', () => {
expandedState.expandedChildren = ['4'];
Expand All @@ -69,27 +74,41 @@ describe('box', () => {
data: {
id: '1',
},
children: [{
data: {
id: '2',
}
}],
}
children: [
{
data: {
id: '2',
},
},
],
},
});
d = d.children[2];
expectedState.expandedChildren = ['4', '10'];
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return state with same id, isExpanded true and swapped expaneded child', () => {
d = d.children[1];
expectedState.expandedChildren = ['4'];
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return state with new id, isExpanded true and clicked node as expanded', () => {
d = d.children[0].children[0];
expectedState.topId = '2';
expectedState.expandedChildren = ['3'];
expect(getNewState(d, expandedState)).to.deep.equal(expectedState);
expect(getNewState(d, expandedState, [])).to.deep.equal(expectedState);
});
it('should return ancestor state', () => {
expectedState.topId = '777';
expectedState.isExpanded = false;
d = { data: { id: '777' } };
expect(getNewState(d, expandedState, ['777'])).to.deep.equal(expectedState);
});

it('should return parent of ancestor state', () => {
expectedState.topId = '888';
d = { data: { id: '777' }, parent: { data: { id: '888' } } };
expect(getNewState(d, expandedState, ['777', '888'])).to.deep.equal(expectedState);
});
});

Expand Down
8 changes: 5 additions & 3 deletions src/tree/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export default function position(orientation, element) {
if (!d.parent[axis]) {
d.parent[axis] = widthTranslation(d.parent, axis);
}
d[axis] = d.parent.data.id !== 'Root' && haveNoChildren(d.parent.children)
? d.parent[axis] + buttonMargin
: d.parent[axis] + (d.data.childNumber - (d.parent.children.length - 1) / 2) * widthSpacing;
d[axis] =
d.parent.data.id !== 'Root' && haveNoChildren(d.parent.children)
? d.parent[axis] + buttonMargin
: d.parent[axis] + (d.data.childNumber - (d.parent.children.length - 1) / 2) * widthSpacing;
} else if (!d.children) {
d[axis] = (element.clientWidth - cardWidth) / 2;
d.zoomFactor = 1;
} else {
// In case of zoom mode we need to have the tree moved to the right from the start
const neededWidth = (cardWidth + widthMargin) * d.children.length - widthMargin;
Expand Down

0 comments on commit 5aebaa2

Please sign in to comment.