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

100% Statements 23/23
91.67% Branches 11/12
100% Functions 4/4
100% Lines 23/23

    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

    1189×
    1189×

    1×

    1×
    6×

    6×
    6×

    6×
    21×

    21×
    4×

    17×
    4×

    13×
    4×

    9×
    3×

    6×
    3×

    3×
    3×

    21×

    1×

     import { forEach } from 'min-dash';

    /**
     * A handler that align elements in a certain way.
     *
     */
    export default function AlignElements(modeling, canvas) {
      this._modeling = modeling;
      this._canvas = canvas;
    }

    AlignElements.$inject = [ 'modeling', 'canvas' ];

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

      var elements = context.elements,
          alignment = context.alignment;

      forEach(elements, function(element) {
        var delta = {
          x: 0,
          y: 0
        };

        if (alignment.left) {
          delta.x = alignment.left - element.x;

        } else if (alignment.right) {
          delta.x = (alignment.right - element.width) - element.x;

        } else if (alignment.center) {
          delta.x = (alignment.center - Math.round(element.width / 2)) - element.x;

        } else if (alignment.top) {
          delta.y = alignment.top - element.y;

        } else if (alignment.bottom) {
          delta.y = (alignment.bottom - element.height) - element.y;

        } else Eif (alignment.middle) {
          delta.y = (alignment.middle - Math.round(element.height / 2)) - element.y;
        }

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

    AlignElements.prototype.postExecute = function(context) {

    };