# all files / diagram-js/lib/features/snapping/ SnapUtil.js

82.35% Statements 28/34
77.78% Branches 21/27
77.78% Functions 7/9
82.35% Lines 28/34

    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
    1441×
    1×

    130×

    130×
    371×

    371×
    53×

    47×

    51×

    124×

    124×

    277×

    277×
    224×

    53×
    46×

    7×

    215×

    215×

    215×

    215×

    215×

    215×

    215×

    215×
    215×

    215×

    48×
     var abs = Math.abs,
        round = Math.round;

    /**
     * Snap value to a collection of reference values.
     *
     * @param  {Number} value
     * @param  {Array<Number>} values
     * @param  {Number} [tolerance=10]
     *
     * @return {Number} the value we snapped to or null, if none snapped
     */
    export function snapTo(value, values, tolerance) {
      tolerance = tolerance === undefined ? 10 : tolerance;

      var idx, snapValue;

      for (idx = 0; idx < values.length; idx++) {
        snapValue = values[idx];

        if (abs(snapValue - value) <= tolerance) {
          return snapValue;
        }
      }
    }

    export function topLeft(bounds) {
      return {
        x: bounds.x,
        y: bounds.y
      };
    }

    export function topRight(bounds) {
      return {
        x: bounds.x + bounds.width,
        y: bounds.y
      };
    }

    export function bottomLeft(bounds) {
      return {
        x: bounds.x,
        y: bounds.y + bounds.height
      };
    }

    export function bottomRight(bounds) {
      return {
        x: bounds.x + bounds.width,
        y: bounds.y + bounds.height
      };
    }

    export function mid(bounds, defaultValue) {

      Iif (!bounds || isNaN(bounds.x) || isNaN(bounds.y)) {
        return defaultValue;
      }

      return {
        x: round(bounds.x + bounds.width / 2),
        y: round(bounds.y + bounds.height / 2)
      };
    }

    /**
     * Retrieve the snap state of the given event.
     *
     * @param  {Event} event
     * @param  {String} axis
     *
     * @return {Boolean} the snapped state
     *
     */
    export function isSnapped(event, axis) {
      var snapped = event.snapped;

      if (!snapped) {
        return false;
      }

      if (typeof axis === 'string') {
        return snapped[axis];
      }

      return snapped.x && snapped.y;
    }

    /**
     * Set the given event as snapped.
     *
     * This method may change the x and/or y position of the shape
     * from the given event!
     *
     * @param {Event} event
     * @param {String} axis
     * @param {Number|Boolean} value
     *
     * @return {Number} old value
     */
    export function setSnapped(event, axis, value) {
      Iif (typeof axis !== 'string') {
        throw new Error('axis must be in [x, y]');
      }

      Iif (typeof value !== 'number' && value !== false) {
        throw new Error('value must be Number or false');
      }

      var delta,
          previousValue = event[axis];

      var snapped = event.snapped = (event.snapped || {});

      Iif (value === false) {
        snapped[axis] = false;
      } else {
        snapped[axis] = true;

        delta = value - previousValue;

        event[axis] += delta;
        event['d' + axis] += delta;
      }

      return previousValue;
    }

    /**
     * Get children of a shape.
     *
     * @param {djs.model.Shape} parent
     *
     * @returns {Array<djs.model.Shape|djs.model.Connection>}
     */
    export function getChildren(parent) {
      return parent.children || [];
    }