all files / lib/features/modeling/behavior/ LabelBehavior.js

97.1% Statements 67/69
86.36% Branches 38/44
100% Functions 8/8
97.1% Lines 67/69
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                                                                  963×     963× 296× 296× 296×   296× 10×     296×                               963× 510×   510× 510×   510× 245×       265× 237×     28×     28×         28×                 963× 278× 278× 278×     278× 28×         963×   37× 37×         37×         37×       37×       37× 31×         37×                   93× 93× 93× 93× 93× 93× 93×     93× 92×     93× 91×     93×     963×             524×     524× 431×     93×   93×         963× 109× 109× 109×   109×   109×       14× 14×                      
import {
  assign
} from 'min-dash';
 
import inherits from 'inherits';
 
import {
  is,
  getBusinessObject
} from '../../../util/ModelUtil';
 
import {
  isLabelExternal,
  getExternalLabelMid,
} from '../../../util/LabelUtil';
 
import {
  getLabelAdjustment
} from './util/LabelLayoutUtil';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
var DEFAULT_LABEL_DIMENSIONS = {
  width: 90,
  height: 20
};
 
var NAME_PROPERTY = 'name';
var TEXT_PROPERTY = 'text';
 
/**
 * A component that makes sure that external labels are added
 * together with respective elements and properly updated (DI wise)
 * during move.
 *
 * @param {EventBus} eventBus
 * @param {Modeling} modeling
 * @param {BpmnFactory} bpmnFactory
 * @param {TextRenderer} textRenderer
 */
export default function LabelBehavior(
    eventBus, modeling, bpmnFactory,
    textRenderer) {
 
  CommandInterceptor.call(this, eventBus);
 
  // update label if name property was updated
  this.postExecute('element.updateProperties', function(e) {
    var context = e.context,
        element = context.element,
        properties = context.properties;
 
    if (NAME_PROPERTY in properties) {
      modeling.updateLabel(element, properties[NAME_PROPERTY]);
    }
 
    if (TEXT_PROPERTY in properties
        && is(element, 'bpmn:TextAnnotation')) {
 
      var newBounds = textRenderer.getTextAnnotationBounds(
        {
          x: element.x,
          y: element.y,
          width: element.width,
          height: element.height
        },
        properties[TEXT_PROPERTY] || ''
      );
 
      modeling.updateLabel(element, properties.text, newBounds);
    }
  });
 
  // create label shape after shape/connection was created
  this.postExecute([ 'shape.create', 'connection.create' ], function(e) {
    var context = e.context;
 
    var element = context.shape || context.connection,
        businessObject = element.businessObject;
 
    if (!isLabelExternal(element)) {
      return;
    }
 
    // only create label if name
    if (!businessObject.name) {
      return;
    }
 
    var labelCenter = getExternalLabelMid(element);
 
    // we don't care about x and y
    var labelDimensions = textRenderer.getExternalLabelBounds(
      DEFAULT_LABEL_DIMENSIONS,
      businessObject.name || ''
    );
 
    modeling.createLabel(element, labelCenter, {
      id: businessObject.id + '_label',
      businessObject: businessObject,
      width: labelDimensions.width,
      height: labelDimensions.height
    });
  });
 
  // update label after label shape was deleted
  this.postExecute('shape.delete', function(event) {
    var context = event.context,
        labelTarget = context.labelTarget,
        hints = context.hints || {};
 
    // check if label
    if (labelTarget && hints.unsetLabel !== false) {
      modeling.updateLabel(labelTarget, null, null, { removeShape: false });
    }
  });
 
  // update di information on label creation
  this.postExecute([ 'label.create' ], function(event) {
 
    var context = event.context,
        element = context.shape,
        businessObject,
        di;
 
    // we want to trigger on real labels only
    Iif (!element.labelTarget) {
      return;
    }
 
    // we want to trigger on BPMN elements only
    Iif (!is(element.labelTarget || element, 'bpmn:BaseElement')) {
      return;
    }
 
    businessObject = element.businessObject,
    di = businessObject.di;
 
 
    if (!di.label) {
      di.label = bpmnFactory.create('bpmndi:BPMNLabel', {
        bounds: bpmnFactory.create('dc:Bounds')
      });
    }
 
    assign(di.label.bounds, {
      x: element.x,
      y: element.y,
      width: element.width,
      height: element.height
    });
  });
 
  function getVisibleLabelAdjustment(event) {
 
    var command = event.command,
        context = event.context,
        connection = context.connection,
        label = connection.label,
        hints = assign({}, context.hints),
        newWaypoints = context.newWaypoints || connection.waypoints,
        oldWaypoints = context.oldWaypoints;
 
 
    if (typeof hints.startChanged === 'undefined') {
      hints.startChanged = (command === 'connection.reconnectStart');
    }
 
    if (typeof hints.endChanged === 'undefined') {
      hints.endChanged = (command === 'connection.reconnectEnd');
    }
 
    return getLabelAdjustment(label, newWaypoints, oldWaypoints, hints);
  }
 
  this.postExecute([
    'connection.layout',
    'connection.reconnectEnd',
    'connection.reconnectStart',
    'connection.updateWaypoints'
  ], function(event) {
 
    var label = event.context.connection.label,
        labelAdjustment;
 
    if (!label) {
      return;
    }
 
    labelAdjustment = getVisibleLabelAdjustment(event);
 
    modeling.moveShape(label, labelAdjustment);
  });
 
 
  // keep label position on shape replace
  this.postExecute([ 'shape.replace' ], function(event) {
    var context = event.context,
        newShape = context.newShape,
        oldShape = context.oldShape;
 
    var businessObject = getBusinessObject(newShape);
 
    if (businessObject
      && isLabelExternal(businessObject)
      && oldShape.label
      && newShape.label) {
      newShape.label.x = oldShape.label.x;
      newShape.label.y = oldShape.label.y;
    }
  });
 
}
 
inherits(LabelBehavior, CommandInterceptor);
 
LabelBehavior.$inject = [
  'eventBus',
  'modeling',
  'bpmnFactory',
  'textRenderer'
];