# all files / diagram-js/lib/features/align-elements/ AlignElements.js

93.1% Statements 54/58
78.95% Branches 15/19
100% Functions 15/15
93.1% Lines 54/58

    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

    4×

    6×

    8×

    1×

    4×

    3×

    59×

    1×

    1×
    6×
    6×
    6×

    6×
    3×
    3×

    6×

    1×
    12×

    1×
    6×
    6×
    6×
    6×
    6×
    6×

    2×

    6×
    2×

    4×
    2×

    2×

    2×

    2×
    7×

    7×

    7×

    2×
    7×

    7×

    2×

    2×

    2×
    7×

    2×

    2×

    6×

    1×
    6×

    6×
    21×

    6×

    6×

    6×

    6×

     import {
      filter,
      forEach,
      sortBy
    } from 'min-dash';

    function last(arr) {
      return arr && arr[arr.length - 1];
    }

    function sortTopOrMiddle(element) {
      return element.y;
    }

    function sortLeftOrCenter(element) {
      return element.x;
    }

    /**
     * Sorting functions for different types of alignment
     *
     * @type {Object}
     *
     * @return {Function}
     */
    var ALIGNMENT_SORTING = {
      left: sortLeftOrCenter,
      center: sortLeftOrCenter,
      right: function(element) {
        return element.x + element.width;
      },
      top: sortTopOrMiddle,
      middle: sortTopOrMiddle,
      bottom: function(element) {
        return element.y + element.height;
      }
    };

    export default function AlignElements(modeling) {
      this._modeling = modeling;
    }

    AlignElements.$inject = [ 'modeling' ];

    /**
     * Get the relevant "axis" and "dimension" related to the current type of alignment
     *
     * @param  {String} type left|right|center|top|bottom|middle
     *
     * @return {Object} { axis, dimension }
     */
    AlignElements.prototype._getOrientationDetails = function(type) {
      var vertical = [ 'top', 'bottom', 'middle' ],
          axis = 'x',
          dimension = 'width';

      if (vertical.indexOf(type) !== -1) {
        axis = 'y';
        dimension = 'height';
      }

      return {
        axis: axis,
        dimension: dimension
      };
    };

    AlignElements.prototype._isType = function(type, types) {
      return types.indexOf(type) !== -1;
    };

    /**
     * Get a point on the relevant axis where elements should align to
     *
     * @param  {String} type left|right|center|top|bottom|middle
     * @param  {Array} sortedElements
     *
     * @return {Object}
     */
    AlignElements.prototype._alignmentPosition = function(type, sortedElements) {
      var orientation = this._getOrientationDetails(type),
          axis = orientation.axis,
          dimension = orientation.dimension,
          alignment = {},
          centers = {},
          hasSharedCenters = false,
          centeredElements,
          firstElement,
          lastElement;

      function getMiddleOrTop(first, last) {
        return Math.round((first[axis] + last[axis] + last[dimension]) / 2);
      }

      if (this._isType(type, [ 'left', 'top' ])) {
        alignment[type] = sortedElements[0][axis];

      } else if (this._isType(type, [ 'right', 'bottom' ])) {
        lastElement = last(sortedElements);

        alignment[type] = lastElement[axis] + lastElement[dimension];

      } else Eif (this._isType(type, [ 'center', 'middle' ])) {

        // check if there is a center shared by more than one shape
        // if not, just take the middle of the range
        forEach(sortedElements, function(element) {
          var center = element[axis] + Math.round(element[dimension] / 2);

          Iif (centers[center]) {
            centers[center].elements.push(element);
          } else {
            centers[center] = {
              elements: [ element ],
              center: center
            };
          }
        });

        centeredElements = sortBy(centers, function(center) {
          Iif (center.elements.length > 1) {
            hasSharedCenters = true;
          }

          return center.elements.length;
        });

        Iif (hasSharedCenters) {
          alignment[type] = last(centeredElements).center;

          return alignment;
        }

        firstElement = sortedElements[0];

        sortedElements = sortBy(sortedElements, function(element) {
          return element[axis] + element[dimension];
        });

        lastElement = last(sortedElements);

        alignment[type] = getMiddleOrTop(firstElement, lastElement);
      }

      return alignment;
    };

    /**
     * Executes the alignment of a selection of elements
     *
     * @param  {Array} elements [description]
     * @param  {String} type left|right|center|top|bottom|middle
     */
    AlignElements.prototype.trigger = function(elements, type) {
      var modeling = this._modeling;

      var filteredElements = filter(elements, function(element) {
        return !(element.waypoints || element.host || element.labelTarget);
      });

      var sortFn = ALIGNMENT_SORTING[type];

      var sortedElements = sortBy(filteredElements, sortFn);

      var alignment = this._alignmentPosition(type, sortedElements);

      modeling.alignElements(sortedElements, alignment);
    };