all files / lib/features/bendpoints/ BendpointMove.js

98.41% Statements 62/63
93.75% Branches 30/32
100% Functions 6/6
98.41% Lines 62/63
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                            71×       71×       21× 21×   21×   19×   10×     21×             21×   21×                                     21× 21× 21×   21×           20×           71× 19×   19×   19×     19×   19×         71× 24×   24× 24×     71×                                                                                      
import {
  pointDistance,
  pointsOnLine
} from '../../util/Geometry';
 
var COMMAND_BENDPOINT_UPDATE = 'connection.updateWaypoints',
    COMMAND_RECONNECT_START = 'connection.reconnectStart',
    COMMAND_RECONNECT_END = 'connection.reconnectEnd';
 
var round = Math.round;
 
 
/**
 * A component that implements moving of bendpoints
 */
export default function BendpointMove(injector, eventBus, canvas, dragging, rules, modeling) {
 
  // optional connection docking integration
  var connectionDocking = injector.get('connectionDocking', false);
 
 
  // API
  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;
    } else
    if (!insert && bendpointIndex === waypoints.length - 1) {
      type = COMMAND_RECONNECT_END;
    } else {
      type = COMMAND_BENDPOINT_UPDATE;
    }
 
    context = {
      connection: connection,
      bendpointIndex: bendpointIndex,
      insert: insert,
      type: type
    };
 
    context.allowed = rules.allowed(context.type, context);
 
    dragging.init(event, 'bendpoint.move', {
      data: {
        connection: connection,
        connectionGfx: gfx,
        context: context
      }
    });
  };
 
 
  // DRAGGING IMPLEMENTATION
  function filterRedundantWaypoints(waypoints) {
 
    // alter copy of waypoints, not original
    waypoints = waypoints.slice();
 
    var idx = 0,
        point,
        previousPoint,
        nextPoint;
 
    while (waypoints[idx]) {
      point = waypoints[idx];
      previousPoint = waypoints[idx - 1];
      nextPoint = waypoints[idx + 1];
 
      if (pointDistance(point, nextPoint) === 0 ||
          pointsOnLine(previousPoint, nextPoint, point)) {
 
        // remove point, if overlapping with {nextPoint}
        // or on line with {previousPoint} -> {point} -> {nextPoint}
        waypoints.splice(idx, 1);
      } else {
        idx++;
      }
    }
 
    return waypoints;
  }
 
  eventBus.on('bendpoint.move.hover', function(e) {
    var context = e.context;
 
    context.hover = e.hover;
 
    Eif (e.hover) {
      // asks whether reconnect / bendpoint move / bendpoint add
      // is allowed at the given position
      var allowed = context.allowed = rules.allowed(context.type, context);
 
      if (allowed) {
        context.target = context.hover;
      }
    }
  });
 
  eventBus.on([ 'bendpoint.move.out', 'bendpoint.move.cleanup' ], function(event) {
    var context = event.context;
 
    context.target = null;
    context.allowed = false;
  });
 
  eventBus.on('bendpoint.move.end', function(e) {
 
    var context = e.context,
        connection = context.connection,
        originalWaypoints = connection.waypoints,
        newWaypoints = originalWaypoints.slice(),
        bendpointIndex = context.bendpointIndex,
        allowed = context.allowed,
        insert = context.insert,
        bendpoint,
        hints;
 
    // ensure we have actual pixel values bendpoint
    // coordinates (important when zoom level was > 1 during move)
    bendpoint = {
      x: round(e.x),
      y: round(e.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 Eif (allowed !== false && context.type === COMMAND_BENDPOINT_UPDATE) {
      if (insert) {
        // insert new bendpoint
        newWaypoints.splice(bendpointIndex, 0, bendpoint);
      } else {
        // swap previous waypoint with the moved one
        newWaypoints[bendpointIndex] = bendpoint;
      }
 
      // pass hints on the actual moved bendpoint
      // this is useful for connection and label layouting
      hints = {
        bendpointMove: {
          insert: insert,
          bendpointIndex: bendpointIndex
        }
      };
 
      if (connectionDocking) {
        // (0) temporarily assign new waypoints
        connection.waypoints = newWaypoints;
 
        // (1) crop connection with new waypoints and save them
        connection.waypoints = connectionDocking.getCroppedWaypoints(connection);
        newWaypoints = connection.waypoints;
 
        // (2) restore original waypoints to not mutate connection directly
        connection.waypoints = originalWaypoints;
      }
 
      modeling.updateWaypoints(context.connection, filterRedundantWaypoints(newWaypoints), hints);
    } else {
      return false;
    }
  });
}
 
BendpointMove.$inject = [
  'injector',
  'eventBus',
  'canvas',
  'dragging',
  'rules',
  'modeling'
];