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

90.12% Statements 73/81
82.5% Branches 33/40
100% Functions 9/9
90% Lines 72/80
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                          144× 144×   257× 25×                             119×       56× 50×                     36× 36× 36× 36× 36× 36× 36× 36×   36×     31×             31× 21×     31× 20×     31×   31×     165× 155×   155× 87×     155× 68×         31×     71× 36× 36× 36× 36×     36×         36× 36×       36× 36×     36×                         36× 36× 36×   36× 18×     18×   18×   18×     36× 27×   27× 27×       18×       71×   36× 36× 36× 36× 36× 36×     36×         36× 36×       36× 36×     36×                  
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) {
 
    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) {
    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;
 
    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
    });
  });
 
 
  // 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
    });
  });
}
 
 
BendpointSnapping.$inject = [ 'eventBus' ];