all files / Github/diagram-js/lib/features/bendpoints/ BendpointSnapping.js

88.76% Statements 79/89
80.36% Branches 45/56
100% Functions 9/9
88.64% Lines 78/88
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                              100× 100×   198× 20×                             80×       37× 37×                     10× 10× 10× 10× 10× 10× 10× 10×   10×       10×             10× 10×     10×     10×   10×     59× 57×   57× 24×     57× 33×         10×     98× 10× 10× 10× 10×     10×         10× 10×       10× 10×     10×               10×       10×                 40× 40× 40×   40× 21×     19×   19×   19×     38× 30×   30× 30×       19×       98×   40× 40× 40× 40× 40× 40×     40×         40× 40×     40× 40×     40×               40×     40× 12×          
import {
  assign,
  forEach,
  isArray
} from 'min-dash';
 
import { setSnapped } from '../snapping/SnapUtil';
 
var abs= Math.abs,
    round = Math.round;
 
var TOLERANCE = 10;
 
 
export default function BendpointSnapping(eventBus) {
 
  function snapTo(values, value) {
 
    Eif (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) {
    Eif (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;
 
    Iif (snapPoints) {
      return snapPoints;
    }
 
    var referenceWaypoints = [
      waypoints[segmentStartIndex - 1],
      segmentStart,
      segmentEnd,
      waypoints[segmentEndIndex + 1]
    ];
 
    Eif (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;
 
    Iif (!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
    });
 
    // only set snapped if actually snapped
    Iif (cx || snapPoints.vertical.indexOf(x) !== -1) {
      setSnapped(event, 'x', sx);
    }
 
    if (cy || snapPoints.horizontal.indexOf(y) !== -1) {
      setSnapped(event, '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', 'bendpoint.move.end' ], 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;
 
    Iif (!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
    });
 
    // only set snapped if actually snapped
    if (cx || snapPoints.vertical.indexOf(x) !== -1) {
      setSnapped(event, 'x', sx);
    }
 
    if (cy || snapPoints.horizontal.indexOf(y) !== -1) {
      setSnapped(event, 'y', sy);
    }
  });
}
 
 
BendpointSnapping.$inject = [ 'eventBus' ];