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

93.75% Statements 15/16
50% Branches 1/2
100% Functions 5/5
93.75% Lines 15/16

    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

    1128×

    1×

    1×

    1×

    1×

    53×

    53×

    53×

    53×

    1×

    1×
    3×

    3×

    53×
    106×

     import inherits from 'inherits';

    import CreateShapeHandler from './CreateShapeHandler';

    /**
     * A handler that attaches a label to a given target shape.
     *
     * @param {Canvas} canvas
     */
    export default function CreateLabelHandler(canvas) {
      CreateShapeHandler.call(this, canvas);
    }

    inherits(CreateLabelHandler, CreateShapeHandler);

    CreateLabelHandler.$inject = [ 'canvas' ];

    // api //////////////////////

    var originalExecute = CreateShapeHandler.prototype.execute;

    /**
     * Appends a label to a target shape.
     *
     * @method CreateLabelHandler#execute
     *
     * @param {Object} context
     * @param {ElementDescriptor} context.target the element the label is attached to
     * @param {ElementDescriptor} context.parent the parent object
     * @param {Point} context.position position of the new element
     */
    CreateLabelHandler.prototype.execute = function(context) {

      var label = context.shape;

      ensureValidDimensions(label);

      label.labelTarget = context.labelTarget;

      return originalExecute.call(this, context);
    };

    var originalRevert = CreateShapeHandler.prototype.revert;

    /**
     * Undo append by removing the shape
     */
    CreateLabelHandler.prototype.revert = function(context) {
      context.shape.labelTarget = null;

      return originalRevert.call(this, context);
    };

    // helpers //////////////////////

    function ensureValidDimensions(label) {
      // make sure a label has valid { width, height } dimensions
      [ 'width', 'height' ].forEach(function(prop) {
        Iif (typeof label[prop] === 'undefined') {
          label[prop] = 0;
        }
      });
    }