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

96.81% Statements 91/94
78.79% Branches 52/66
100% Functions 14/14
96.81% Lines 91/94
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389                                                                                                                                        1189×     1189× 317× 317× 317×   317× 12×     317×                               1189× 626× 626×   626× 145×     481× 481×   481× 203×       278× 245×     33×     33×         33×                 1189× 333× 333× 333×     333× 51×         1189×   46× 46×         46×         46×       46×       46× 39×         46×                   99× 99× 99× 99× 99× 99×     99× 98×     99× 97×     99×     1189×         595×     595× 496×     99×   99×         1189× 138× 138× 138×   138×   138×       25× 25×           1189×   310× 310× 310× 310×   310×   10× 10× 10×     10×   10×   10×                                                         10×   10×                           10×       10×   10×                     10×                                                                                                         10× 40×           10×   10×  
import {
  assign
} from 'min-dash';
 
import inherits from 'inherits';
 
import {
  is,
  getBusinessObject
} from '../../../util/ModelUtil';
 
import {
  isLabelExternal,
  getExternalLabelMid,
  hasExternalLabel
} from '../../../util/LabelUtil';
 
import {
  getLabel
} from '../../label-editing/LabelUtil';
 
import {
  getLabelAdjustment
} from './util/LabelLayoutUtil';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import {
  getNewAttachPoint
} from 'diagram-js/lib/util/AttachUtil';
 
import {
  getMid,
  roundPoint
} from 'diagram-js/lib/layout/LayoutUtil';
 
import {
  delta
} from 'diagram-js/lib/util/PositionUtil';
 
import {
  sortBy
} from 'min-dash';
 
import {
  getDistancePointLine,
  perpendicularFoot
} from './util/GeometricUtil';
 
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,
        hints = context.hints || {};
 
    if (hints.createElementsBehavior === false) {
      return;
    }
 
    var element = context.shape || context.connection,
        businessObject = element.businessObject;
 
    if (!isLabelExternal(element)) {
      return;
    }
 
    // only create label if attribute available
    if (!getLabel(element)) {
      return;
    }
 
    var labelCenter = getExternalLabelMid(element);
 
    // we don't care about x and y
    var labelDimensions = textRenderer.getExternalLabelBounds(
      DEFAULT_LABEL_DIMENSIONS,
      getLabel(element)
    );
 
    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 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 = !!hints.connectionStart;
    }
 
    if (typeof hints.endChanged === 'undefined') {
      hints.endChanged = !!hints.connectionEnd;
    }
 
    return getLabelAdjustment(label, newWaypoints, oldWaypoints, hints);
  }
 
  this.postExecute([
    'connection.layout',
    '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;
    }
  });
 
 
  // move external label after resizing
  this.postExecute('shape.resize', function(event) {
 
    var context = event.context,
        shape = context.shape,
        newBounds = context.newBounds,
        oldBounds = context.oldBounds;
 
    if (hasExternalLabel(shape)) {
 
      var label = shape.label,
          labelMid = getMid(label),
          edges = asEdges(oldBounds);
 
      // get nearest border point to label as reference point
      var referencePoint = getReferencePoint(labelMid, edges);
 
      var delta = getReferencePointDelta(referencePoint, oldBounds, newBounds);
 
      modeling.moveShape(label, delta);
 
    }
 
  });
 
}
 
inherits(LabelBehavior, CommandInterceptor);
 
LabelBehavior.$inject = [
  'eventBus',
  'modeling',
  'bpmnFactory',
  'textRenderer'
];
 
// helpers //////////////////////
 
/**
 * Calculates a reference point delta relative to a new position
 * of a certain element's bounds
 *
 * @param {Point} point
 * @param {Bounds} oldBounds
 * @param {Bounds} newBounds
 *
 * @return {Delta} delta
 */
export function getReferencePointDelta(referencePoint, oldBounds, newBounds) {
 
  var newReferencePoint = getNewAttachPoint(referencePoint, oldBounds, newBounds);
 
  return roundPoint(delta(newReferencePoint, referencePoint));
}
 
/**
 * Generates the nearest point (reference point) for a given point
 * onto given set of lines
 *
 * @param {Array<Point, Point>} lines
 * @param {Point} point
 *
 * @param {Point}
 */
export function getReferencePoint(point, lines) {
 
  Iif (!lines.length) {
    return;
  }
 
  var nearestLine = getNearestLine(point, lines);
 
  return perpendicularFoot(point, nearestLine);
}
 
/**
 * Convert the given bounds to a lines array containing all edges
 *
 * @param {Bounds|Point} bounds
 *
 * @return Array<Point>
 */
export function asEdges(bounds) {
  return [
    [ // top
      {
        x: bounds.x,
        y: bounds.y
      },
      {
        x: bounds.x + (bounds.width || 0),
        y: bounds.y
      }
    ],
    [ // right
      {
        x: bounds.x + (bounds.width || 0),
        y: bounds.y
      },
      {
        x: bounds.x + (bounds.width || 0),
        y: bounds.y + (bounds.height || 0)
      }
    ],
    [ // bottom
      {
        x: bounds.x,
        y: bounds.y + (bounds.height || 0)
      },
      {
        x: bounds.x + (bounds.width || 0),
        y: bounds.y + (bounds.height || 0)
      }
    ],
    [ // left
      {
        x: bounds.x,
        y: bounds.y
      },
      {
        x: bounds.x,
        y: bounds.y + (bounds.height || 0)
      }
    ]
  ];
}
 
/**
 * Returns the nearest line for a given point by distance
 * @param {Point} point
 * @param Array<Point> lines
 *
 * @return Array<Point>
 */
function getNearestLine(point, lines) {
 
  var distances = lines.map(function(l) {
    return {
      line: l,
      distance: getDistancePointLine(point, l)
    };
  });
 
  var sorted = sortBy(distances, 'distance');
 
  return sorted[0].line;
}