Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changeable root position (between children - default / corner) #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/flextree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {version} = packageInfo;
const defaults = Object.freeze({
children: data => data.children,
nodeSize: node => node.data.size,
rootBetweenChildren: true,
spacing: 0,
});

Expand All @@ -28,6 +29,7 @@ export default function flextree(options) {
function getFlexNode() {
const nodeSize = accessor('nodeSize');
const spacing = accessor('spacing');
const rootBetweenChildren = accessor('rootBetweenChildren');
return class FlexNode extends hierarchy.prototype.constructor {
constructor(data) {
super(data);
Expand All @@ -46,6 +48,7 @@ export default function flextree(options) {
get bottom() { return this.y + this.ySize; }
get left() { return this.x - this.xSize / 2; }
get right() { return this.x + this.xSize / 2; }
get rootBetweenChildren() { return rootBetweenChildren(); }
get root() {
const ancs = this.ancestors();
return ancs[ancs.length - 1];
Expand Down Expand Up @@ -89,6 +92,7 @@ export default function flextree(options) {
const FlexNode = getFlexNode();
const nodeSize = accessor('nodeSize');
const spacing = accessor('spacing');
const rootBetweenChildren = accessor('rootBetweenChildren');
return class extends FlexNode {
constructor(data) {
super(data);
Expand All @@ -105,6 +109,7 @@ export default function flextree(options) {
set x(v) { this.data.x = v; }
get y() { return this.data.y; }
set y(v) { this.data.y = v; }
get rootBetweenChildren() { return rootBetweenChildren(); }
update() {
layoutChildren(this);
resolveX(this);
Expand Down Expand Up @@ -326,8 +331,9 @@ const positionRoot = w => {
if (w.hasChildren) {
const k0 = w.firstChild;
const kf = w.lastChild;
const prelim = (k0.prelim + k0.relX - k0.xSize / 2 +
kf.relX + kf.prelim + kf.xSize / 2 ) / 2;
const prelim = w.rootBetweenChildren
? (k0.prelim + k0.relX - k0.xSize / 2 + kf.relX + kf.prelim + kf.xSize / 2 ) / 2
: 0;
Object.assign(w, {
prelim,
lExt: k0.lExt, lExtRelX: k0.lExtRelX,
Expand Down