All files / react-archer/lib SvgArrow.js

91.23% Statements 52/57
68% Branches 17/25
100% Functions 6/6
91.23% Lines 52/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x     1x 1x 1x   1x   1x   1x   1x   2x     5x           4x   1x             5x 5x 5x   5x 5x   5x       3x 2x         1x 1x                   3x 2x         1x 1x                 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x   1x 1x 1x   1x 1x 1x   1x 1x 1x   1x   1x             1x         1x   1x                   1x
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.computeEndingPointAccordingToArrow = computeEndingPointAccordingToArrow;
exports.computeStartingAnchorPosition = computeStartingAnchorPosition;
exports.computeEndingAnchorPosition = computeEndingAnchorPosition;
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function computeEndingArrowDirectionVector(endingAnchor) {
  switch (endingAnchor) {
    case 'left':
      return { arrowX: -1, arrowY: 0 };
    case 'right':
      return { arrowX: 1, arrowY: 0 };
    case 'top':
      return { arrowX: 0, arrowY: -1 };
    case 'bottom':
      return { arrowX: 0, arrowY: 1 };
    default:
      return { arrowX: 0, arrowY: 0 };
  }
}
 
function computeEndingPointAccordingToArrow(xEnd, yEnd, arrowLength, strokeWidth, endingAnchor) {
  var _computeEndingArrowDi = computeEndingArrowDirectionVector(endingAnchor),
      arrowX = _computeEndingArrowDi.arrowX,
      arrowY = _computeEndingArrowDi.arrowY;
 
  var xe = xEnd + arrowX * arrowLength * strokeWidth / 2;
  var ye = yEnd + arrowY * arrowLength * strokeWidth / 2;
 
  return { xe: xe, ye: ye };
}
 
function computeStartingAnchorPosition(xs, ys, xe, ye, startingAnchor) {
  if (startingAnchor === 'top' || startingAnchor === 'bottom') {
    return {
      xa1: xs,
      ya1: ys + (ye - ys) / 2
    };
  }
  Eif (startingAnchor === 'left' || startingAnchor === 'right') {
    return {
      xa1: xs + (xe - xs) / 2,
      ya1: ys
    };
  }
 
  return { xa1: xs, ya1: ys };
}
 
function computeEndingAnchorPosition(xs, ys, xe, ye, endingAnchor) {
  if (endingAnchor === 'top' || endingAnchor === 'bottom') {
    return {
      xa2: xe,
      ya2: ye - (ye - ys) / 2
    };
  }
  Eif (endingAnchor === 'left' || endingAnchor === 'right') {
    return {
      xa2: xe - (xe - xs) / 2,
      ya2: ye
    };
  }
 
  return { xa2: xe, ya2: ye };
}
 
var SvgArrow = function SvgArrow(_ref) {
  var startingPoint = _ref.startingPoint,
      startingAnchor = _ref.startingAnchor,
      endingPoint = _ref.endingPoint,
      endingAnchor = _ref.endingAnchor,
      strokeColor = _ref.strokeColor,
      arrowLength = _ref.arrowLength,
      strokeWidth = _ref.strokeWidth;
 
  var actualArrowLength = arrowLength * 2;
 
  var xs = startingPoint.x;
  var ys = startingPoint.y;
 
  var _computeEndingPointAc = computeEndingPointAccordingToArrow(endingPoint.x, endingPoint.y, actualArrowLength, strokeWidth, endingAnchor),
      xe = _computeEndingPointAc.xe,
      ye = _computeEndingPointAc.ye;
 
  var _computeStartingAncho = computeStartingAnchorPosition(xs, ys, xe, ye, startingAnchor),
      xa1 = _computeStartingAncho.xa1,
      ya1 = _computeStartingAncho.ya1;
 
  var _computeEndingAnchorP = computeEndingAnchorPosition(xs, ys, xe, ye, endingAnchor),
      xa2 = _computeEndingAnchorP.xa2,
      ya2 = _computeEndingAnchorP.ya2;
 
  var pathString = 'M' + xs + ',' + ys + ' ' + ('C' + xa1 + ',' + ya1 + ' ' + xa2 + ',' + ya2 + ' ') + (xe + ',' + ye);
 
  return _react2.default.createElement('path', {
    d: pathString,
    style: { fill: 'none', stroke: strokeColor, strokeWidth: strokeWidth },
    markerEnd: 'url(#arrow)'
  });
};
 
var pointPropType = _propTypes2.default.shape({
  x: _propTypes2.default.number,
  y: _propTypes2.default.number
});
 
var anchorType = _propTypes2.default.oneOf(['top', 'bottom', 'left', 'right']);
 
SvgArrow.propTypes = {
  startingPoint: pointPropType,
  startingAnchor: anchorType,
  endingPoint: pointPropType,
  endingAnchor: anchorType,
  strokeColor: _propTypes2.default.string,
  arrowLength: _propTypes2.default.number,
  strokeWidth: _propTypes2.default.number
};
 
exports.default = SvgArrow;