Skip to content

Commit

Permalink
feat(linkTools.Button): allow distance to be defined via callback (#2743
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kumilingus authored Aug 26, 2024
1 parent ae95768 commit 5168a3a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
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

0 comments on commit 5168a3a

Please sign in to comment.