# all files / diagram-js/lib/features/modeling/cmd/ DistributeElementsHandler.js

96.72% Statements 59/61
89.29% Branches 25/28
100% Functions 12/12
96.72% Lines 59/61

    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

    1189×

    1×

    1×

    1×
    3×

    3×
    3×
    3×

    15×
    15×

    19×

    3×

    10×

    14×

    14×

    14×

    5×

    5×

    3×
    3×
    3×

    3×

    3×

    16×
    11×
    6×

    6×

    11×

    5×

    5×

    5×

    5×

    5×

    5×

    14×

    14×
    5×

    5×

    9×

    5×
    4×

    3×

    3×

    3×

    3×
    16×

    16×
    6×

    10×

    10×

    10×
    18×
    18×

    18×
    6×

    18×
    18×

    18×

    1×

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

    /**
     * A handler that distributes elements evenly.
     */
    export default function DistributeElements(modeling) {
      this._modeling = modeling;
    }

    DistributeElements.$inject = [ 'modeling' ];

    var OFF_AXIS = {
      x: 'y',
      y: 'x'
    };

    DistributeElements.prototype.preExecute = function(context) {
      var modeling = this._modeling;

      var groups = context.groups,
          axis = context.axis,
          dimension = context.dimension;

      function updateRange(group, element) {
        group.range.min = Math.min(element[axis], group.range.min);
        group.range.max = Math.max(element[axis] + element[dimension], group.range.max);
      }

      function center(element) {
        return element[axis] + element[dimension] / 2;
      }

      function lastIdx(arr) {
        return arr.length - 1;
      }

      function rangeDiff(range) {
        return range.max - range.min;
      }

      function centerElement(refCenter, element) {
        var delta = { y: 0 };

        delta[axis] = refCenter - center(element);

        if (delta[axis]) {

          delta[OFF_AXIS[axis]] = 0;

          modeling.moveElements([ element ], delta, element.parent);
        }
      }

      var firstGroup = groups[0],
          lastGroupIdx = lastIdx(groups),
          lastGroup = groups[ lastGroupIdx ];

      var margin,
          spaceInBetween,
          groupsSize = 0; // the size of each range

      forEach(groups, function(group, idx) {
        var sortedElements,
            refElem,
            refCenter;

        if (group.elements.length < 2) {
          if (idx && idx !== groups.length - 1) {
            updateRange(group, group.elements[0]);

            groupsSize += rangeDiff(group.range);
          }
          return;
        }

        sortedElements = sortBy(group.elements, axis);

        refElem = sortedElements[0];

        Iif (idx === lastGroupIdx) {
          refElem = sortedElements[lastIdx(sortedElements)];
        }

        refCenter = center(refElem);

        // wanna update the ranges after the shapes have been centered
        group.range = null;

        forEach(sortedElements, function(element) {

          centerElement(refCenter, element);

          if (group.range === null) {
            group.range = {
              min: element[axis],
              max: element[axis] + element[dimension]
            };

            return;
          }

          // update group's range after centering the range elements
          updateRange(group, element);
        });

        if (idx && idx !== groups.length - 1) {
          groupsSize += rangeDiff(group.range);
        }
      });

      spaceInBetween = Math.abs(lastGroup.range.min - firstGroup.range.max);

      margin = Math.round((spaceInBetween - groupsSize) / (groups.length - 1));

      Iif (margin < groups.length - 1) {
        return;
      }

      forEach(groups, function(group, groupIdx) {
        var delta = {},
            prevGroup;

        if (group === firstGroup || group === lastGroup) {
          return;
        }

        prevGroup = groups[groupIdx - 1];

        group.range.max = 0;

        forEach(group.elements, function(element, idx) {
          delta[OFF_AXIS[axis]] = 0;
          delta[axis] = (prevGroup.range.max - element[axis]) + margin;

          if (group.range.min !== element[axis]) {
            delta[axis] += element[axis] - group.range.min;
          }

          Eif (delta[axis]) {
            modeling.moveElements([ element ], delta, element.parent);
          }

          group.range.max = Math.max(element[axis] + element[dimension], idx ? group.range.max : 0);
        });
      });
    };

    DistributeElements.prototype.postExecute = function(context) {

    };