123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import {
- assign,
- forEach,
- isArray
- } from 'min-dash';
- var abs= Math.abs,
- round = Math.round;
- var TOLERANCE = 10;
- export default function BendpointSnapping(eventBus) {
- function snapTo(values, value) {
- if (isArray(values)) {
- var i = values.length;
- while (i--) if (abs(values[i] - value) <= TOLERANCE) {
- return values[i];
- }
- } else {
- values = +values;
- var rem = value % values;
- if (rem < TOLERANCE) {
- return value - rem;
- }
- if (rem > values - TOLERANCE) {
- return value - rem + values;
- }
- }
- return value;
- }
- function mid(element) {
- if (element.width) {
- return {
- x: round(element.width / 2 + element.x),
- y: round(element.height / 2 + element.y)
- };
- }
- }
- // connection segment snapping //////////////////////
- function getConnectionSegmentSnaps(context) {
- var snapPoints = context.snapPoints,
- connection = context.connection,
- waypoints = connection.waypoints,
- segmentStart = context.segmentStart,
- segmentStartIndex = context.segmentStartIndex,
- segmentEnd = context.segmentEnd,
- segmentEndIndex = context.segmentEndIndex,
- axis = context.axis;
- if (snapPoints) {
- return snapPoints;
- }
- var referenceWaypoints = [
- waypoints[segmentStartIndex - 1],
- segmentStart,
- segmentEnd,
- waypoints[segmentEndIndex + 1]
- ];
- if (segmentStartIndex < 2) {
- referenceWaypoints.unshift(mid(connection.source));
- }
- if (segmentEndIndex > waypoints.length - 3) {
- referenceWaypoints.unshift(mid(connection.target));
- }
- context.snapPoints = snapPoints = { horizontal: [] , vertical: [] };
- forEach(referenceWaypoints, function(p) {
- // we snap on existing bendpoints only,
- // not placeholders that are inserted during add
- if (p) {
- p = p.original || p;
- if (axis === 'y') {
- snapPoints.horizontal.push(p.y);
- }
- if (axis === 'x') {
- snapPoints.vertical.push(p.x);
- }
- }
- });
- return snapPoints;
- }
- eventBus.on('connectionSegment.move.move', 1500, function(event) {
- var context = event.context,
- snapPoints = getConnectionSegmentSnaps(context),
- x = event.x,
- y = event.y,
- sx, sy;
- if (!snapPoints) {
- return;
- }
- // snap
- sx = snapTo(snapPoints.vertical, x);
- sy = snapTo(snapPoints.horizontal, y);
- // correction x/y
- var cx = (x - sx),
- cy = (y - sy);
- // update delta
- assign(event, {
- dx: event.dx - cx,
- dy: event.dy - cy,
- x: sx,
- y: sy
- });
- });
- // bendpoint snapping //////////////////////
- function getBendpointSnaps(context) {
- var snapPoints = context.snapPoints,
- waypoints = context.connection.waypoints,
- bendpointIndex = context.bendpointIndex;
- if (snapPoints) {
- return snapPoints;
- }
- var referenceWaypoints = [ waypoints[bendpointIndex - 1], waypoints[bendpointIndex + 1] ];
- context.snapPoints = snapPoints = { horizontal: [] , vertical: [] };
- forEach(referenceWaypoints, function(p) {
- // we snap on existing bendpoints only,
- // not placeholders that are inserted during add
- if (p) {
- p = p.original || p;
- snapPoints.horizontal.push(p.y);
- snapPoints.vertical.push(p.x);
- }
- });
- return snapPoints;
- }
- eventBus.on('bendpoint.move.move', 1500, function(event) {
- var context = event.context,
- snapPoints = getBendpointSnaps(context),
- target = context.target,
- targetMid = target && mid(target),
- x = event.x,
- y = event.y,
- sx, sy;
- if (!snapPoints) {
- return;
- }
- // snap
- sx = snapTo(targetMid ? snapPoints.vertical.concat([ targetMid.x ]) : snapPoints.vertical, x);
- sy = snapTo(targetMid ? snapPoints.horizontal.concat([ targetMid.y ]) : snapPoints.horizontal, y);
- // correction x/y
- var cx = (x - sx),
- cy = (y - sy);
- // update delta
- assign(event, {
- dx: event.dx - cx,
- dy: event.dy - cy,
- x: event.x - cx,
- y: event.y - cy
- });
- });
- }
- BendpointSnapping.$inject = [ 'eventBus' ];
|