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

fix(auto-place): fix east and west in #getConnectedDistance #459

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions lib/features/auto-place/AutoPlaceUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function getConnectedDistance(source, hints) {
}

var defaultDistance = hints.defaultDistance || DEFAULT_DISTANCE,
direction = hints.direction || 'w',
direction = hints.direction || 'e',
filter = hints.filter,
getWeight = hints.getWeight || getDefaultWeight,
maxDistance = hints.maxDistance || DEFAULT_MAX_DISTANCE,
Expand All @@ -158,11 +158,11 @@ export function getConnectedDistance(source, hints) {
}
} else if (direction === 'w') {
if (reference === 'start') {
return asTRBL(b).left - asTRBL(a).right;
return asTRBL(a).left - asTRBL(b).right;
} else if (reference === 'center') {
return getMid(b).x - asTRBL(a).right;
return asTRBL(a).left - getMid(b).x;
} else {
return asTRBL(b).right - asTRBL(a).right;
return asTRBL(a).left - asTRBL(b).left;
}
} else if (direction === 's') {
if (reference === 'start') {
Expand All @@ -174,11 +174,11 @@ export function getConnectedDistance(source, hints) {
}
} else {
if (reference === 'start') {
return asTRBL(a).left - asTRBL(b).right;
return asTRBL(b).left - asTRBL(a).right;
} else if (reference === 'center') {
return asTRBL(a).left - getMid(b).x;
return getMid(b).x - asTRBL(a).right;
} else {
return asTRBL(a).left - asTRBL(b).left;
return asTRBL(b).right - asTRBL(a).right;
}
}
}
Expand All @@ -188,7 +188,7 @@ export function getConnectedDistance(source, hints) {
.map(function(connection) {
return {
id: connection.source.id,
distance: getDistance(source, connection.source),
distance: getDistance(connection.source, source),
weight: getWeight(connection)
};
});
Expand Down
263 changes: 188 additions & 75 deletions test/spec/features/auto-place/AutoPlaceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,104 +422,215 @@ describe('features/auto-place', function() {

describe('direction and reference hint', function() {

function expectConnectedDistance(position, hints, distance) {
return inject(function(modeling) {
describe('distance to source', function() {

// given
modeling.appendShape(shape, newShape, position);
function expectConnectedSourceDistance(position, hints, distance) {
return inject(function(canvas, modeling) {

// when
var connectedDistance = getConnectedDistance(shape, assign(hints, {
maxDistance: 1000
}));
// given
modeling.appendShape(shape, newShape, position, canvas.getRootElement(), {
connectionTarget: shape
});

// when
var connectedDistance = getConnectedDistance(shape, assign(hints, {
maxDistance: 1000
}));

// then
expect(connectedDistance).to.equal(distance);
});
}


it('direction w, reference start', expectConnectedSourceDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'start' },
100
));


it('direction w, reference center', expectConnectedSourceDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'center' },
150
));


it('direction w, reference end', expectConnectedSourceDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'end' },
200
));

// then
expect(connectedDistance).to.equal(distance);
});
}

it('direction s, reference start', expectConnectedSourceDistance(
{ x: 50, y: -150 },
{ direction: 's', reference: 'start' },
100
));

it ('direction w, reference start', expectConnectedDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'start' },
100
));

it('direction s, reference center', expectConnectedSourceDistance(
{ x: 50, y: -150 },
{ direction: 's', reference: 'center' },
150
));

it('direction w, reference center', expectConnectedDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'center' },
150
));

it('direction s, reference end', expectConnectedSourceDistance(
{ x: 50, y: -150 },
{ direction: 's', reference: 'end' },
200
));

it ('direction w, reference end', expectConnectedDistance(
{ x: 250, y: 50 },
{ direction: 'w', reference: 'end' },
200
));

it('direction e, reference start', expectConnectedSourceDistance(
{ x: -150, y: 50 },
{ direction: 'e', reference: 'start' },
100
));

it('direction s, reference start', expectConnectedDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'start' },
100
));

it('direction e, reference center', expectConnectedSourceDistance(
{ x: -150, y: 50 },
{ direction: 'e', reference: 'center' },
150
));

it('direction s, reference center', expectConnectedDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'center' },
150
));

it('direction e, reference end', expectConnectedSourceDistance(
{ x: -150, y: 50 },
{ direction: 'e', reference: 'end' },
200
));

it('direction s, reference end', expectConnectedDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'end' },
200
));

it('direction n, reference start', expectConnectedSourceDistance(
{ x: 50, y: 250 },
{ direction: 'n', reference: 'start' },
100
));

it('direction e, reference start', expectConnectedDistance(
{ x: -250, y: 50 },
{ direction: 'e', reference: 'start' },
200
));

it('direction n, reference center', expectConnectedSourceDistance(
{ x: 50, y: 250 },
{ direction: 'n', reference: 'center' },
150
));

it('direction e, reference center', expectConnectedDistance(
{ x: -250, y: 50 },
{ direction: 'e', reference: 'center' },
250
));

it('direction n, reference end', expectConnectedSourceDistance(
{ x: 50, y: 250 },
{ direction: 'n', reference: 'end' },
200
));

});


describe('distance to target', function() {

function expectConnectedTargetDistance(position, hints, distance) {
return inject(function(modeling) {

// given
modeling.appendShape(shape, newShape, position);

// when
var connectedDistance = getConnectedDistance(shape, assign(hints, {
maxDistance: 1000
}));

// then
expect(connectedDistance).to.equal(distance);
});
}


it('direction e, reference end', expectConnectedDistance(
{ x: -250, y: 50 },
{ direction: 'e', reference: 'end' },
300
));
it('direction w, reference start', expectConnectedTargetDistance(
{ x: -150, y: 50 },
{ direction: 'w', reference: 'start' },
100
));

it('direction n, reference start', expectConnectedDistance(
{ x: 50, y: -250 },
{ direction: 'n', reference: 'start' },
200
));

it('direction w, reference center', expectConnectedTargetDistance(
{ x: -150, y: 50 },
{ direction: 'w', reference: 'center' },
150
));

it('direction n, reference center', expectConnectedDistance(
{ x: 50, y: -250 },
{ direction: 'n', reference: 'center' },
250
));

it('direction w, reference end', expectConnectedTargetDistance(
{ x: -150, y: 50 },
{ direction: 'w', reference: 'end' },
200
));

it('direction n, reference end', expectConnectedDistance(
{ x: 50, y: -250 },
{ direction: 'n', reference: 'end' },
300
));

it('direction s, reference start', expectConnectedTargetDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'start' },
100
));


it('direction s, reference center', expectConnectedTargetDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'center' },
150
));


it('direction s, reference end', expectConnectedTargetDistance(
{ x: 50, y: 250 },
{ direction: 's', reference: 'end' },
200
));


it('direction e, reference start', expectConnectedTargetDistance(
{ x: 250, y: 50 },
{ direction: 'e', reference: 'start' },
100
));


it('direction e, reference center', expectConnectedTargetDistance(
{ x: 250, y: 50 },
{ direction: 'e', reference: 'center' },
150
));


it('direction e, reference end', expectConnectedTargetDistance(
{ x: 250, y: 50 },
{ direction: 'e', reference: 'end' },
200
));

it('direction n, reference start', expectConnectedTargetDistance(
{ x: 50, y: -150 },
{ direction: 'n', reference: 'start' },
100
));


it('direction n, reference center', expectConnectedTargetDistance(
{ x: 50, y: -150 },
{ direction: 'n', reference: 'center' },
150
));


it('direction n, reference end', expectConnectedTargetDistance(
{ x: 50, y: -150 },
{ direction: 'n', reference: 'end' },
200
));

});

});

Expand Down Expand Up @@ -585,13 +696,15 @@ describe('features/auto-place', function() {
height: 100
});

// source
modeling.createShape(shape1, {
x: 300,
y: 200
x: -250,
y: 50
}, root);

modeling.connect(shape1, shape);

// target
modeling.createShape(newShape, {
x: 250,
y: 50
Expand Down Expand Up @@ -624,7 +737,7 @@ describe('features/auto-place', function() {
});

// then
expect(connectedDistance).to.equal(150);
expect(connectedDistance).to.equal(200);
});

});
Expand Down