Skip to content

Commit

Permalink
Added getExtendedNodes (v1.2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanichKotikov committed Jan 9, 2019
1 parent cb560da commit 6ddc57c
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 13 deletions.
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Family, IFamilyNode, IFamilyData } from './src/types';
import { Family, IFamilyNode, IFamilyExtNode, IFamilyData } from './src/types';

export {
Gender,
Expand All @@ -9,11 +9,9 @@ export {
ICanvasSize,
IRelation,
IFamilyNode,
IFamilyExtNode,
IConnector,
IFamilyData,
} from './src/types';

declare const hasHiddenRelatives: (family: Family, node: IFamilyNode) => boolean;
export { hasHiddenRelatives };

export default function (nodes: IFamilyNode[], rootId: string): IFamilyData;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "relatives-tree",
"version": "1.1.0",
"version": "1.2.0",
"description": "Calculates families and nodes positions for rendering a family tree",
"main": "lib/index.js",
"typings": "index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/children/byParents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export default (store: Store, parentIDs: string[], type: FamilyType = 'root', is
if (child.spouses.length) {
const { left, middle, right } = getSpouses(store, [child]);
[...left.map(node => [node]), middle, ...right.map(node => [node])].forEach(nodes => (
family.cUnits.push(new Unit(family.id, nodes))
family.cUnits.push(new Unit(family.id, nodes, true))
));
} else {
family.cUnits.push(new Unit(family.id, [child]));
family.cUnits.push(new Unit(family.id, [child], true));
}
});

Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import createChildren from './children/create';
import connectors from './connectors';
import correctPositions from './utils/correctPositions';
import getCanvasSize from './utils/getCanvasSize';
import getExtendedNodes from './utils/getExtendedNodes';
import { IFamilyNode, IFamilyData } from './types';

import hasHiddenRelatives from './utils/hasHiddenRelatives';
export { hasHiddenRelatives };

export default (nodes: IFamilyNode[], rootId: string): IFamilyData => {
const store = new Store(nodes, rootId);

Expand All @@ -25,6 +23,7 @@ export default (nodes: IFamilyNode[], rootId: string): IFamilyData => {
return {
families: families,
canvas: getCanvasSize(store),
nodes: getExtendedNodes(families),
connectors: connectors(families),
};
}
4 changes: 3 additions & 1 deletion src/models/unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { IFamilyNode } from '../types';
class Unit {

familyId: number;
isChild: boolean;
nodes: IFamilyNode[];
shift: number;

constructor(familyId: number, nodes: IFamilyNode[]) {
constructor(familyId: number, nodes: IFamilyNode[], isChild: boolean = false) {
this.familyId = familyId;
this.isChild = isChild;
this.nodes = nodes;
this.shift = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parents/byChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Unit from '../models/unit';

export default (store: Store, childIDs: string[]): Family => {
const family = new Family(store.getNextId(), 'parent');
const cUnit = new Unit(family.id, store.getNodes(childIDs));
const cUnit = new Unit(family.id, store.getNodes(childIDs), true);

cUnit.nodes.forEach((child, cIndex) => {
const parents = store
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ export interface IFamilyNode {
placeholder?: boolean;
}

export interface IFamilyExtNode extends IFamilyNode {
top: number;
left: number;
hasSubTree: boolean;
}

export interface IConnector {
points: [number, number, number, number];
}

export interface IFamilyData {
canvas: ICanvasSize;
families: Family[];
nodes: IFamilyExtNode[];
connectors: IConnector[];
}
29 changes: 29 additions & 0 deletions src/utils/getExtendedNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import hasHiddenRelatives from './hasHiddenRelatives';
import { flat } from './index';
import Family from '../models/family';
import Unit from '../models/unit';
import { IFamilyNode, IFamilyExtNode } from '../types';

const PARENTS = ['root', 'parent'];
const CHILDREN = ['root', 'child'];

const extendNode = (family: Family) => (unit: Unit) => (
unit.nodes.map((node: IFamilyNode, idx: number) => ({
...node,
top: family.top + (unit.isChild ? 2 : 0),
left: family.left + unit.shift + (idx * 2),
hasSubTree: hasHiddenRelatives(family, node),
}))
);

const getParentNodes = (family: Family) =>
(~PARENTS.indexOf(family.type) ? family.pUnits : []).map(extendNode(family));

const getChildNodes = (family: Family) =>
(~CHILDREN.indexOf(family.type) ? family.cUnits : []).map(extendNode(family));

const mapFamily = (family: Family) =>
[...getParentNodes(family), ...getChildNodes(family)].reduce(flat, []);

export default (families: Family[]): IFamilyExtNode[] =>
families.map(mapFamily).reduce(flat, []);
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Store from '../store';
import { IRelation, IFamilyNode } from '../types';

const flat = (items: any[], item: any) => items.concat(item);
const unique = (item: any, index: number, arr: any[]): boolean => arr.indexOf(item) === index;

const relToNode = (store: Store) => (rel: IRelation) => store.getNode(rel.id);
Expand All @@ -9,6 +10,7 @@ const hasDiffParents = (node: IFamilyNode): boolean =>
node.parents.map(rel => rel.type).filter(unique).length > 1;

export {
flat,
relToNode,
hasDiffParents,
};

0 comments on commit 6ddc57c

Please sign in to comment.