2015-04-22 07:08:38 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
|
|
|
|
var assign = require('lodash/object/assign');
|
|
|
|
|
|
|
|
var BaseLayouter = require('diagram-js/lib/layout/BaseLayouter'),
|
|
|
|
ManhattanLayout = require('diagram-js/lib/layout/ManhattanLayout');
|
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
var LayoutUtil = require('diagram-js/lib/layout/LayoutUtil');
|
|
|
|
|
|
|
|
var getMid = LayoutUtil.getMid,
|
|
|
|
getOrientation = LayoutUtil.getOrientation;
|
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var is = require('../../util/ModelUtil').is;
|
2015-04-22 07:08:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
function BpmnLayouter() {}
|
|
|
|
|
|
|
|
inherits(BpmnLayouter, BaseLayouter);
|
|
|
|
|
|
|
|
module.exports = BpmnLayouter;
|
|
|
|
|
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
BpmnLayouter.prototype.layoutConnection = function(connection, layoutHints) {
|
2015-04-22 07:08:38 +00:00
|
|
|
var source = connection.source,
|
|
|
|
target = connection.target,
|
|
|
|
waypoints = connection.waypoints,
|
|
|
|
start,
|
|
|
|
end;
|
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
var manhattanOptions,
|
2015-04-22 07:08:38 +00:00
|
|
|
updatedWaypoints;
|
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
start = getConnectionDocking(waypoints, 0, source);
|
|
|
|
end = getConnectionDocking(waypoints, waypoints && waypoints.length - 1, target);
|
|
|
|
|
|
|
|
// TODO (nre): support vertical modeling
|
|
|
|
// and invert preferredLayouts accordingly
|
|
|
|
|
2015-10-13 09:07:35 +00:00
|
|
|
if ((is(connection, 'bpmn:Association') || is(connection, 'bpmn:DataAssociation')) && waypoints) {
|
|
|
|
return waypoints;
|
|
|
|
}
|
2015-04-22 07:08:38 +00:00
|
|
|
|
|
|
|
// manhattan layout sequence / message flows
|
|
|
|
if (is(connection, 'bpmn:MessageFlow')) {
|
2015-07-29 13:11:54 +00:00
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'straight', 'v:v' ]
|
2015-04-22 07:08:38 +00:00
|
|
|
};
|
2015-08-24 09:28:54 +00:00
|
|
|
|
|
|
|
if (is(target, 'bpmn:Event')) {
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'v:v' ]
|
|
|
|
};
|
|
|
|
}
|
2015-07-29 13:11:54 +00:00
|
|
|
} else
|
|
|
|
|
2015-04-22 07:08:38 +00:00
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
// layout all connection between flow elements h:h,
|
|
|
|
//
|
|
|
|
// except for
|
|
|
|
//
|
|
|
|
// (1) outgoing of BoundaryEvents -> layout h:v or v:h based on attach orientation
|
|
|
|
// (2) incoming / outgoing of Gateway -> v:h (outgoing), h:v (incoming)
|
|
|
|
//
|
2015-04-22 07:08:38 +00:00
|
|
|
if (is(connection, 'bpmn:SequenceFlow')) {
|
2015-07-29 13:11:54 +00:00
|
|
|
|
|
|
|
// make sure boundary event connections do
|
|
|
|
// not look ugly =:>
|
|
|
|
if (is(source, 'bpmn:BoundaryEvent')) {
|
|
|
|
|
|
|
|
var orientation = getAttachOrientation(source);
|
|
|
|
|
|
|
|
if (/left|right/.test(orientation)) {
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'h:v' ]
|
|
|
|
};
|
|
|
|
} else
|
|
|
|
|
|
|
|
if (/top|bottom/.test(orientation)) {
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'v:h' ]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
|
|
|
|
if (is(source, 'bpmn:Gateway')) {
|
|
|
|
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'v:h' ]
|
|
|
|
};
|
|
|
|
} else
|
|
|
|
|
|
|
|
if (is(target, 'bpmn:Gateway')) {
|
|
|
|
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'h:v' ]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply horizontal love <3
|
|
|
|
else {
|
|
|
|
manhattanOptions = {
|
|
|
|
preferredLayouts: [ 'h:h' ]
|
|
|
|
};
|
|
|
|
}
|
2015-04-22 07:08:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
if (manhattanOptions) {
|
2015-04-22 07:08:38 +00:00
|
|
|
|
2015-07-29 13:11:54 +00:00
|
|
|
manhattanOptions = assign(manhattanOptions, layoutHints);
|
2015-04-22 07:08:38 +00:00
|
|
|
|
|
|
|
updatedWaypoints =
|
|
|
|
ManhattanLayout.repairConnection(
|
2015-07-29 13:11:54 +00:00
|
|
|
source, target,
|
|
|
|
start, end,
|
2015-04-22 07:08:38 +00:00
|
|
|
waypoints,
|
2015-07-29 13:11:54 +00:00
|
|
|
manhattanOptions);
|
2015-04-22 07:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return updatedWaypoints || [ start, end ];
|
2015-07-13 08:37:43 +00:00
|
|
|
};
|
2015-07-29 13:11:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
function getAttachOrientation(attachedElement) {
|
|
|
|
|
|
|
|
var hostElement = attachedElement.host,
|
|
|
|
padding = -10;
|
|
|
|
|
|
|
|
return getOrientation(getMid(attachedElement), hostElement, padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getConnectionDocking(waypoints, idx, shape) {
|
|
|
|
var point = waypoints && waypoints[idx];
|
|
|
|
|
|
|
|
return point ? (point.original || point) : getMid(shape);
|
|
|
|
}
|