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

feat(linkTools.Button): allow distance to be defined via callback #2743

Merged
merged 1 commit into from
Aug 26, 2024
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
5 changes: 4 additions & 1 deletion packages/joint-core/src/linkTools/Button.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export const Button = ToolView.extend({
},
getLinkMatrix() {
const { relatedView: view, options } = this;
const { offset = 0, distance = 0, rotate, scale } = options;
const { offset = 0, distance: distanceOpt = 0, rotate, scale } = options;
const distance = (typeof distanceOpt === 'function')
? distanceOpt.call(this, view, this)
: distanceOpt;
let tangent, position, angle;
if (util.isPercentage(distance)) {
tangent = view.getTangentAtRatio(parseFloat(distance) / 100);
Expand Down
28 changes: 28 additions & 0 deletions packages/joint-core/test/jointjs/dia/linkTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,32 @@ QUnit.module('linkTools', function(hooks) {
});
});

QUnit.module('Button', function() {

QUnit.test('distance as number', function(assert) {
const targetX = 333;
const distance = -17;
const button = new joint.linkTools.Button({ distance });
linkView.addTools(new joint.dia.ToolsView({ tools: [button] }));
// move the target cell to the right, to make the link horizontal
link.getTargetCell().position(targetX, link.getSourceCell().position().x);
assert.equal(button.el.getCTM().e, targetX + distance);
});

QUnit.test('distance as function', function(assert) {
const targetX = 333;
const distance = -17;
const distanceSpy = sinon.spy(() => distance);
const button = new joint.linkTools.Button({ distance: distanceSpy });
linkView.addTools(new joint.dia.ToolsView({ tools: [button] }));
assert.ok(distanceSpy.calledOnce);
assert.ok(distanceSpy.calledWithExactly(linkView, button));
assert.ok(distanceSpy.calledOn(button));
// move the target cell to the right, to make the link horizontal
link.getTargetCell().position(targetX, link.getSourceCell().position().x);
assert.ok(distanceSpy.calledTwice);
assert.equal(button.el.getCTM().e, targetX + distance);
});
});

});
4 changes: 4 additions & 0 deletions packages/joint-core/test/ts/toolsView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ new elementTools.HoverConnect({
trackWidth: 10,
trackPath: (view) => view.model.attr(['body', 'd']),
});

new linkTools.Button({
distance: (view) => view.getConnectionLength() < 100 ? 20 : '50%'
});
8 changes: 6 additions & 2 deletions packages/joint-core/types/joint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4390,10 +4390,14 @@ export namespace linkTools {

namespace Button {

type ActionCallback = (evt: dia.Event, view: dia.LinkView, tool: dia.ToolView) => void;
type ActionCallback = (evt: dia.Event, view: dia.LinkView, tool: Button) => void;

type Distance = number | string;

type DistanceCallback = (this: Button, view: dia.LinkView, tool: Button) => Distance;

interface Options extends dia.ToolView.Options {
distance?: number | string;
distance?: Distance | DistanceCallback;
offset?: number;
rotate?: boolean;
action?: ActionCallback;
Expand Down
Loading