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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | 1× 1× 1× 28× 5× 23× 5× 18× 18× 18× 18× 18× 18× 18× 18× 18× 5× 5× 3× 18× 3× 15× 2× 13× 18× 18× 18× 18× 21× 18× 9× 8× 8× 18× 10× 10× 10× 18× 19× 19× 18× 18× 18× 18× 18× 18× 18× 4× 14× 14× 14× 9× 14× 18× 8× 10× 5× 5× 5× 5× 5× 5× 5× 5× 35× 35× 5× 35× 35× 35× 35× 35× 35× 48× 48× 48× 7× 7× 14× 14× 7× 7× 7× 7× 7× 2× 5× 7× 28× 7× 28× 56× 40× 58× 40× 58× 26× 58× 40× 58× 49× | import { is } from '../../util/ModelUtil'; import { isAny } from '../modeling/util/ModelingUtil'; import { getMid, asTRBL, getOrientation } from 'diagram-js/lib/layout/LayoutUtil'; import { find, reduce } from 'min-dash'; var DEFAULT_HORIZONTAL_DISTANCE = 50; var MAX_HORIZONTAL_DISTANCE = 250; // padding to detect element placement var PLACEMENT_DETECTION_PAD = 10; /** * Find the new position for the target element to * connect to source. * * @param {djs.model.Shape} source * @param {djs.model.Shape} element * * @return {Point} */ export function getNewShapePosition(source, element) { if (is(element, 'bpmn:TextAnnotation')) { return getTextAnnotationPosition(source, element); } if (isAny(element, [ 'bpmn:DataObjectReference', 'bpmn:DataStoreReference' ])) { return getDataElementPosition(source, element); } Eif (is(element, 'bpmn:FlowNode')) { return getFlowNodePosition(source, element); } return getDefaultPosition(source, element); } /** * Always try to place element right of source; * compute actual distance from previous nodes in flow. */ export function getFlowNodePosition(source, element) { var sourceTrbl = asTRBL(source); var sourceMid = getMid(source); var horizontalDistance = getFlowNodeDistance(source, element); var orientation = 'left', rowSize = 80, margin = 30; if (is(source, 'bpmn:BoundaryEvent')) { orientation = getOrientation(source, source.host, -25); if (orientation.indexOf('top') !== -1) { margin *= -1; } } function getVerticalDistance(orient) { if (orient.indexOf('top') != -1) { return -1 * rowSize; } else if (orient.indexOf('bottom') != -1) { return rowSize; } else { return 0; } } var position = { x: sourceTrbl.right + horizontalDistance + element.width / 2, y: sourceMid.y + getVerticalDistance(orientation) }; var escapeDirection = { y: { margin: margin, rowSize: rowSize } }; return deconflictPosition(source, element, position, escapeDirection); } /** * Compute best distance between source and target, * based on existing connections to and from source. * * @param {djs.model.Shape} source * @param {djs.model.Shape} element * * @return {Number} distance */ export function getFlowNodeDistance(source, element) { var sourceTrbl = asTRBL(source); // is connection a reference to consider? function isReference(c) { return is(c, 'bpmn:SequenceFlow'); } function toTargetNode(weight) { return function(shape) { return { shape: shape, weight: weight, distanceTo: function(shape) { var shapeTrbl = asTRBL(shape); return shapeTrbl.left - sourceTrbl.right; } }; }; } function toSourceNode(weight) { return function(shape) { return { shape: shape, weight: weight, distanceTo: function(shape) { var shapeTrbl = asTRBL(shape); return sourceTrbl.left - shapeTrbl.right; } }; }; } // we create a list of nodes to take into consideration // for calculating the optimal flow node distance // // * weight existing target nodes higher than source nodes // * only take into account individual nodes once // var nodes = reduce([].concat( getTargets(source, isReference).map(toTargetNode(5)), getSources(source, isReference).map(toSourceNode(1)) ), function(nodes, node) { // filter out shapes connected twice via source or target nodes[node.shape.id + '__weight_' + node.weight] = node; return nodes; }, {}); // compute distances between source and incoming nodes; // group at the same time by distance and expose the // favourite distance as { fav: { count, value } }. var distancesGrouped = reduce(nodes, function(result, node) { var shape = node.shape, weight = node.weight, distanceTo = node.distanceTo; var fav = result.fav, currentDistance, currentDistanceCount, currentDistanceEntry; currentDistance = distanceTo(shape); // ignore too far away peers // or non-left to right modeled nodes if (currentDistance < 0 || currentDistance > MAX_HORIZONTAL_DISTANCE) { return result; } currentDistanceEntry = result[String(currentDistance)] = result[String(currentDistance)] || { value: currentDistance, count: 0 }; // inc diff count currentDistanceCount = currentDistanceEntry.count += 1 * weight; if (!fav || fav.count < currentDistanceCount) { result.fav = currentDistanceEntry; } return result; }, { }); if (distancesGrouped.fav) { return distancesGrouped.fav.value; } else { return DEFAULT_HORIZONTAL_DISTANCE; } } /** * Always try to place text annotations top right of source. */ export function getTextAnnotationPosition(source, element) { var sourceTrbl = asTRBL(source); var position = { x: sourceTrbl.right + element.width / 2, y: sourceTrbl.top - 50 - element.height / 2 }; var escapeDirection = { y: { margin: -30, rowSize: 20 } }; return deconflictPosition(source, element, position, escapeDirection); } /** * Always put element bottom right of source. */ export function getDataElementPosition(source, element) { var sourceTrbl = asTRBL(source); var position = { x: sourceTrbl.right - 10 + element.width / 2, y: sourceTrbl.bottom + 40 + element.width / 2 }; var escapeDirection = { x: { margin: 30, rowSize: 30 } }; return deconflictPosition(source, element, position, escapeDirection); } /** * Always put element right of source per default. */ export function getDefaultPosition(source, element) { var sourceTrbl = asTRBL(source); var sourceMid = getMid(source); // simply put element right next to source return { x: sourceTrbl.right + DEFAULT_HORIZONTAL_DISTANCE + element.width / 2, y: sourceMid.y }; } /** * Returns all connected elements around the given source. * * This includes: * * - connected elements * - host connected elements * - attachers connected elements * * @param {djs.model.Shape} source * @param {djs.model.Shape} element * * @return {Array<djs.model.Shape>} */ function getAutoPlaceClosure(source, element) { var allConnected = getConnected(source); if (source.host) { allConnected = allConnected.concat(getConnected(source.host)); } Eif (source.attachers) { allConnected = allConnected.concat(source.attachers.reduce(function(shapes, attacher) { return shapes.concat(getConnected(attacher)); }, [])); } return allConnected; } /** * Return target at given position, if defined. * * This takes connected elements from host and attachers * into account, too. */ export function getConnectedAtPosition(source, position, element) { var bounds = { x: position.x - (element.width / 2), y: position.y - (element.height / 2), width: element.width, height: element.height }; var closure = getAutoPlaceClosure(source, element); return find(closure, function(target) { Iif (target === element) { return false; } var orientation = getOrientation(target, bounds, PLACEMENT_DETECTION_PAD); return orientation === 'intersect'; }); } /** * Returns a new, position for the given element * based on the given element that is not occupied * by some element connected to source. * * Take into account the escapeDirection (where to move * on positining clashes) in the computation. * * @param {djs.model.Shape} source * @param {djs.model.Shape} element * @param {Point} position * @param {Object} escapeDelta * * @return {Point} */ export function deconflictPosition(source, element, position, escapeDelta) { function nextPosition(existingElement) { var newPosition = { x: position.x, y: position.y }; [ 'x', 'y' ].forEach(function(axis) { var axisDelta = escapeDelta[axis]; if (!axisDelta) { return; } var dimension = axis === 'x' ? 'width' : 'height'; var margin = axisDelta.margin, rowSize = axisDelta.rowSize; if (margin < 0) { newPosition[axis] = Math.min( existingElement[axis] + margin - element[dimension] / 2, position[axis] - rowSize + margin ); } else { newPosition[axis] = Math.max( existingTarget[axis] + existingTarget[dimension] + margin + element[dimension] / 2, position[axis] + rowSize + margin ); } }); return newPosition; } var existingTarget; // deconflict position until free slot is found while ((existingTarget = getConnectedAtPosition(source, position, element))) { position = nextPosition(existingTarget); } return position; } // helpers ////////////////////// function noneFilter() { return true; } function getConnected(element, connectionFilter) { return [].concat( getTargets(element, connectionFilter), getSources(element, connectionFilter) ); } function getSources(shape, connectionFilter) { if (!connectionFilter) { connectionFilter = noneFilter; } return shape.incoming.filter(connectionFilter).map(function(c) { return c.source; }); } function getTargets(shape, connectionFilter) { if (!connectionFilter) { connectionFilter = noneFilter; } return shape.outgoing.filter(connectionFilter).map(function(c) { return c.target; }); } |