Skip to content

Commit

Permalink
feat(modeling): connect reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Nov 5, 2019
1 parent 124f11b commit b786acc
Show file tree
Hide file tree
Showing 17 changed files with 416 additions and 230 deletions.
4 changes: 2 additions & 2 deletions lib/features/attach-support/AttachSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function AttachSupport(injector, eventBus, canvas, rules, modelin

// remove invalid outgoing connections
forEach(attacher.outgoing.slice(), function(connection) {
var allowed = rules.allowed('connection.reconnectStart', {
var allowed = rules.allowed('connection.reconnect', {
connection: connection,
source: connection.source,
target: connection.target
Expand All @@ -169,7 +169,7 @@ export default function AttachSupport(injector, eventBus, canvas, rules, modelin

// remove invalid incoming connections
forEach(attacher.incoming.slice(), function(connection) {
var allowed = rules.allowed('connection.reconnectEnd', {
var allowed = rules.allowed('connection.reconnect', {
connection: connection,
source: connection.source,
target: connection.target
Expand Down
157 changes: 120 additions & 37 deletions lib/features/bendpoints/BendpointMove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
filterRedundantWaypoints
} from '../../layout/LayoutUtil';

var COMMAND_BENDPOINT_UPDATE = 'connection.updateWaypoints',
COMMAND_RECONNECT_START = 'connection.reconnectStart',
COMMAND_RECONNECT_END = 'connection.reconnectEnd';

var round = Math.round;

var RECONNECT_START = 'reconnectStart',
RECONNECT_END = 'reconnectEnd',
UPDATE_WAYPOINTS = 'updateWaypoints';


/**
* A component that implements moving of bendpoints
Expand All @@ -22,27 +22,33 @@ export default function BendpointMove(injector, eventBus, canvas, dragging, rule
this.start = function(event, connection, bendpointIndex, insert) {

var type,
context,
waypoints = connection.waypoints,
gfx = canvas.getGraphics(connection);

if (!insert && bendpointIndex === 0) {
type = COMMAND_RECONNECT_START;
type = RECONNECT_START;
} else
if (!insert && bendpointIndex === waypoints.length - 1) {
type = COMMAND_RECONNECT_END;
type = RECONNECT_END;
} else {
type = COMMAND_BENDPOINT_UPDATE;
type = UPDATE_WAYPOINTS;
}

context = {
var command = type === 'updateWaypoints' ? 'connection.updateWaypoints' : 'connection.reconnect';

var allowed = rules.allowed(command, {
connection: connection,
bendpointIndex: bendpointIndex,
insert: insert,
type: type
};
source: connection.source,
target: connection.target
});

var allowed = context.allowed = rules.allowed(context.type, context);
if (allowed === false) {
allowed = rules.allowed(command, {
connection: connection,
source: connection.target,
target: connection.source
});
}

if (allowed === false) {
return;
Expand All @@ -52,67 +58,107 @@ export default function BendpointMove(injector, eventBus, canvas, dragging, rule
data: {
connection: connection,
connectionGfx: gfx,
context: context
context: {
allowed: allowed,
bendpointIndex: bendpointIndex,
connection: connection,
insert: insert,
type: type
}
}
});
};

eventBus.on('bendpoint.move.hover', function(event) {
var context = event.context;

context.hover = event.hover;
var context = event.context,
connection = context.connection,
source = connection.source,
target = connection.target,
hover = event.hover,
type = context.type;

if (event.hover) {
// cache hover state
context.hover = hover;

// asks whether reconnect / bendpoint move / bendpoint add
// is allowed at the given position
var allowed = context.allowed = rules.allowed(context.type, context);
var allowed;

if (allowed) {
context.target = context.hover;
}
if (!hover) {
return;
}

var command = type === UPDATE_WAYPOINTS ? 'connection.updateWaypoints' : 'connection.reconnect';

allowed = context.allowed = rules.allowed(command, {
connection: connection,
source: type === RECONNECT_START ? hover : source,
target: type === RECONNECT_END ? hover : target
});

if (allowed) {
context.source = type === RECONNECT_START ? hover : source;
context.target = type === RECONNECT_END ? hover : target;

return;
}

if (allowed === false) {
allowed = context.allowed = rules.allowed(command, {
connection: connection,
source: type === RECONNECT_END ? hover : target,
target: type === RECONNECT_START ? hover : source
});
}

if (allowed) {
context.source = type === RECONNECT_END ? hover : target;
context.target = type === RECONNECT_START ? hover : source;
}
});

eventBus.on([ 'bendpoint.move.out', 'bendpoint.move.cleanup' ], function(event) {
var context = event.context;

context.hover = null;
context.source = null;
context.target = null;

context.allowed = false;
});

eventBus.on('bendpoint.move.end', function(event) {

var context = event.context,
connection = context.connection,
source = context.source,
target = context.target,
type = context.type,
originalWaypoints = connection.waypoints,
newWaypoints = originalWaypoints.slice(),
bendpointIndex = context.bendpointIndex,
allowed = context.allowed,
insert = context.insert,
bendpoint,
hints;
hints = {};

// ensure we have actual pixel values bendpoint
// coordinates (important when zoom level was > 1 during move)
bendpoint = {
var docking = {
x: round(event.x),
y: round(event.y)
};

if (allowed && context.type === COMMAND_RECONNECT_START) {
modeling.reconnectStart(context.connection, context.target, bendpoint);
} else if (allowed && context.type === COMMAND_RECONNECT_END) {
modeling.reconnectEnd(context.connection, context.target, bendpoint);
} else if (allowed !== false && context.type === COMMAND_BENDPOINT_UPDATE) {
if (!allowed) {
return false;
}

if (type === UPDATE_WAYPOINTS) {
if (insert) {

// insert new bendpoint
newWaypoints.splice(bendpointIndex, 0, bendpoint);
newWaypoints.splice(bendpointIndex, 0, docking);
} else {

// swap previous waypoint with the moved one
newWaypoints[bendpointIndex] = bendpoint;
newWaypoints[bendpointIndex] = docking;
}

// pass hints on the actual moved bendpoint
Expand All @@ -139,7 +185,26 @@ export default function BendpointMove(injector, eventBus, canvas, dragging, rule

modeling.updateWaypoints(context.connection, filterRedundantWaypoints(newWaypoints), hints);
} else {
return false;

if (type === RECONNECT_START) {
hints.docking = 'source';

if (isReverse(context)) {
hints.docking = 'target';

hints.newWaypoints = newWaypoints.reverse();
}
} else if (type === RECONNECT_END) {
hints.docking = 'target';

if (isReverse(context)) {
hints.docking = 'source';

hints.newWaypoints = newWaypoints.reverse();
}
}

modeling.reconnect(connection, source, target, docking, hints);
}
});
}
Expand All @@ -151,4 +216,22 @@ BendpointMove.$inject = [
'dragging',
'rules',
'modeling'
];
];


// helpers //////////

function isReverse(context) {
var hover = context.hover,
source = context.source,
target = context.target,
type = context.type;

if (type === RECONNECT_START) {
return hover && target && hover === target;
}

if (type === RECONNECT_END) {
return hover && source && hover === source;
}
}
Loading

0 comments on commit b786acc

Please sign in to comment.