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

86.05% Statements 37/43
72.22% Branches 13/18
76.92% Functions 10/13
86.05% Lines 37/43

    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

    30×

    30×

    30×

    30×

    1×
    95×

    1×
    72×

    72×
    72×

    1×

    1×
    55×

    1×
    4×

    1×

    55×

    55×

    55×
    28×
    28×

    55×

    28×

    1×

    143×

    143×
    42×

    143×
    112×

    143×
    113×

    1×
    162×

    162×

    1×

    28×

    28×

     import {
      forEach
    } from 'min-dash';

    import {
      snapTo
    } from './SnapUtil';

    /**
     * A snap context, containing the (possibly incomplete)
     * mappings of drop targets (to identify the snapping)
     * to computed snap points.
     */
    export default function SnapContext() {

      /**
       * Map<String, SnapPoints> mapping drop targets to
       * a list of possible snappings.
       *
       * @type {Object}
       */
      this._targets = {};

      /**
       * Map<String, Point> initial positioning of element
       * regarding various snap directions.
       *
       * @type {Object}
       */
      this._snapOrigins = {};

      /**
       * List of snap locations
       *
       * @type {Array<String>}
       */
      this._snapLocations = [];

      /**
       * Map<String, Array<Point>> of default snapping locations
       *
       * @type {Object}
       */
      this._defaultSnaps = {};
    }

    SnapContext.prototype.getSnapOrigin = function(snapLocation) {
      return this._snapOrigins[snapLocation];
    };

    SnapContext.prototype.setSnapOrigin = function(snapLocation, initialValue) {
      this._snapOrigins[snapLocation] = initialValue;

      Eif (this._snapLocations.indexOf(snapLocation) === -1) {
        this._snapLocations.push(snapLocation);
      }
    };

    SnapContext.prototype.addDefaultSnap = function(type, point) {

      var snapValues = this._defaultSnaps[type];

      if (!snapValues) {
        snapValues = this._defaultSnaps[type] = [];
      }

      snapValues.push(point);
    };

    /**
     * Return a number of initialized snaps, i.e. snap locations such as
     * top-left, mid, bottom-right and so forth.
     *
     * @return {Array<String>} snapLocations
     */
    SnapContext.prototype.getSnapLocations = function() {
      return this._snapLocations;
    };

    /**
     * Set the snap locations for this context.
     *
     * The order of locations determines precedence.
     *
     * @param {Array<String>} snapLocations
     */
    SnapContext.prototype.setSnapLocations = function(snapLocations) {
      this._snapLocations = snapLocations;
    };

    /**
     * Get snap points for a given target
     *
     * @param {Element|String} target
     */
    SnapContext.prototype.pointsForTarget = function(target) {

      var targetId = target.id || target;

      var snapPoints = this._targets[targetId];

      if (!snapPoints) {
        snapPoints = this._targets[targetId] = new SnapPoints();
        snapPoints.initDefaults(this._defaultSnaps);
      }

      return snapPoints;
    };

    /**
     * Creates the snap points and initializes them with the
     * given default values.
     *
     * @param {Object<String, Array<Point>>} [defaultPoints]
     */
    export function SnapPoints(defaultSnaps) {

      /**
       * Map<String, Map<(x|y), Array<Number>>> mapping snap locations,
       * i.e. top-left, bottom-right, center to actual snap values.
       *
       * @type {Object}
       */
      this._snapValues = {};
    }

    SnapPoints.prototype.add = function(snapLocation, point) {

      var snapValues = this._snapValues[snapLocation];

      if (!snapValues) {
        snapValues = this._snapValues[snapLocation] = { x: [], y: [] };
      }

      if (snapValues.x.indexOf(point.x) === -1) {
        snapValues.x.push(point.x);
      }

      if (snapValues.y.indexOf(point.y) === -1) {
        snapValues.y.push(point.y);
      }
    };

    SnapPoints.prototype.snap = function(point, snapLocation, axis, tolerance) {
      var snappingValues = this._snapValues[snapLocation];

      return snappingValues && snapTo(point[axis], snappingValues[axis], tolerance);
    };

    /**
     * Initialize a number of default snapping points.
     *
     * @param  {Object} defaultSnaps
     */
    SnapPoints.prototype.initDefaults = function(defaultSnaps) {

      var self = this;

      forEach(defaultSnaps || {}, function(snapPoints, snapLocation) {
        forEach(snapPoints, function(point) {
          self.add(snapLocation, point);
        });
      });
    };