Skip to content

Commit de8cba5

Browse files
committed
Fixed arrange functions (v0.0.2)
1 parent ec5c63e commit de8cba5

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "relatives-tree",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Calculates families and nodes positions for rendering a family tree",
55
"main": "lib/index.js",
66
"typings": "index.d.ts",

src/children/arrange.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import Store from '../store';
22
import Family from '../models/family';
3+
import Unit from '../models/unit';
34
import arrangeMiddle from '../middle/arrange';
45

56
export default (store: Store, family: Family): void => {
6-
const shift = family.pUnits[0].shift;
7-
if (shift === 0 || family.pID === null) return;
8-
7+
if (family.pID === null) return;
98
let right = 0;
109

1110
while (family) {
11+
const fUnit = family.pUnits[0];
12+
13+
const shift = fUnit.shift;
1214
const fRight = family.left + family.width;
1315
right = Math.max(right, fRight);
1416

1517
const pFamily = store.getFamily(family.pID as number); // TODO
1618

17-
const fUnit = family.pUnits[0];
18-
const fShift = fUnit.shift;
19-
19+
const cUnit = pFamily.cUnits.find(unit => unit.isSame(fUnit)) as Unit; // TODO
2020
const uIndex = pFamily.cUnits.findIndex(unit => (
2121
unit.nodes[0].id === fUnit.nodes[0].id
2222
));
2323

2424
if (uIndex === 0) {
25-
pFamily.left = family.left + fShift;
25+
const left = family.left + shift - cUnit.shift;
26+
pFamily.left = Math.max(pFamily.left, left);
2627
} else {
27-
const pUnit = pFamily.cUnits.find(unit => unit.isSame(fUnit));
28-
if (pUnit) pUnit.shift += fShift;
28+
cUnit.shift = family.left + fUnit.shift - pFamily.left;
2929
}
3030

3131
const next = pFamily.cUnits[uIndex + 1];
@@ -46,9 +46,9 @@ export default (store: Store, family: Family): void => {
4646
}
4747

4848
if (pFamily.pID === null) {
49-
const rNodes = [...store.families.values()].filter(f => f.type === 'root');
50-
const start = rNodes.findIndex(f => f.id === pFamily.id);
51-
arrangeMiddle(rNodes, start + 1, fRight);
49+
const rootFamily = [...store.families.values()].filter(f => f.type === 'root');
50+
const start = rootFamily.findIndex(f => f.id === pFamily.id);
51+
arrangeMiddle(rootFamily, start + 1, fRight);
5252
break;
5353
}
5454

src/parents/arrange.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ import Unit from '../models/unit';
44

55
export default (store: Store, family: Family): void => {
66
if (family.cID === null) return;
7-
87
let right = 0;
98

109
while (family) {
10+
const fUnit = family.cUnits[0];
11+
1112
if (family.pUnits.length === 2 && family.pCount > 2) {
12-
family.cUnits[0].shift = Math.floor(family.pUnits[1].shift / 2);
13+
fUnit.shift = Math.floor(family.pUnits[1].shift / 2);
1314
}
1415

15-
const shift = family.cUnits[0].shift;
16+
const shift = fUnit.shift;
1617
const fRight = family.left + family.width;
1718
right = Math.max(right, fRight);
1819

1920
const cFamily = store.getFamily(family.cID as number); // TODO
2021

2122
// root family
2223
if (cFamily.cID === null) {
23-
const cUnit = family.cUnits[0];
24-
cUnit.shift = (family.width - cUnit.size * 2) / 2;
24+
fUnit.shift = (family.width - fUnit.size * 2) / 2;
2525
break;
2626
}
2727

28-
const fUnit = family.cUnits[0];
2928
const pUnit = cFamily.pUnits.find(unit => unit.isSame(fUnit)) as Unit; // TODO
3029
const uIndex = cFamily.pUnits.findIndex(unit => (
3130
unit.nodes[0].id === fUnit.nodes[0].id

0 commit comments

Comments
 (0)