2015-08-25 09:16:10 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
|
2015-09-08 10:01:06 +00:00
|
|
|
var is = require('../../util/ModelUtil').is,
|
|
|
|
getBoundingBox = require('diagram-js/lib/util/Elements').getBBox;
|
2015-08-25 09:16:10 +00:00
|
|
|
|
|
|
|
var pick = require('lodash/object/pick'),
|
2015-09-08 10:01:06 +00:00
|
|
|
assign = require('lodash/object/assign'),
|
|
|
|
forEach = require('lodash/collection/forEach'),
|
|
|
|
values = require('lodash/object/values'),
|
2015-10-22 16:19:06 +00:00
|
|
|
flatten = require('lodash/array/flatten'),
|
|
|
|
groupBy = require('lodash/collection/groupBy');
|
2015-08-25 09:16:10 +00:00
|
|
|
|
|
|
|
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
|
|
|
|
|
2015-09-02 08:37:51 +00:00
|
|
|
var OFFSET = { top: 60, bottom: 60, left: 100, right: 100 };
|
|
|
|
var PADDING = { top: 2, bottom: 2, left: 15, right: 15 };
|
2015-08-25 09:16:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An auto resize component that takes care of expanding parent participants
|
2015-09-08 10:01:06 +00:00
|
|
|
* and lanes if elements are modeled close to an edge of the parent element.
|
2015-08-25 09:16:10 +00:00
|
|
|
*/
|
2015-10-22 16:19:06 +00:00
|
|
|
function AutoResize(eventBus, canvas, modeling, elementRegistry){
|
2015-08-25 09:16:10 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2015-09-08 10:01:06 +00:00
|
|
|
this.postExecuted([ 'shape.create' ], function(event) {
|
2016-05-03 12:38:30 +00:00
|
|
|
|
2015-08-25 09:16:10 +00:00
|
|
|
var context = event.context,
|
2016-05-03 12:38:30 +00:00
|
|
|
hints = context.hints,
|
2015-08-25 09:16:10 +00:00
|
|
|
shape = context.shape,
|
|
|
|
parent = context.parent || context.newParent;
|
|
|
|
|
2016-05-03 12:38:30 +00:00
|
|
|
if (hints && hints.root === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-23 13:00:05 +00:00
|
|
|
expand([ shape ], parent);
|
2015-09-08 10:01:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.postExecuted([ 'elements.move' ], function(event) {
|
|
|
|
|
|
|
|
var context = event.context,
|
2015-10-22 16:19:06 +00:00
|
|
|
elements = flatten(values(context.closure.topLevel));
|
|
|
|
|
2016-05-03 12:38:30 +00:00
|
|
|
var expandings = groupBy(elements, function(element) {
|
2015-10-22 16:19:06 +00:00
|
|
|
return element.parent.id;
|
2015-09-23 12:58:37 +00:00
|
|
|
});
|
2015-09-08 10:01:06 +00:00
|
|
|
|
2015-10-22 16:19:06 +00:00
|
|
|
forEach(expandings, function(elements, parentId) {
|
|
|
|
expand(elements, parentId);
|
|
|
|
});
|
2015-08-25 09:16:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object which indicates near which bounding edge(s)
|
2015-09-08 10:01:06 +00:00
|
|
|
* of a target a bounding box is located.
|
2015-08-25 09:16:10 +00:00
|
|
|
*
|
2015-09-08 10:01:06 +00:00
|
|
|
* @param {Object} bbox bounding box object with x, y, width and height properties
|
2015-08-25 09:16:10 +00:00
|
|
|
* @param {Shape} target
|
|
|
|
* @param {Number} padding
|
|
|
|
*
|
|
|
|
* @return {Object} {top, bottom, left, right}
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
2015-09-08 10:01:06 +00:00
|
|
|
* // If the bounding box is near the bottom left corner of a target the return object is:
|
2015-08-25 09:16:10 +00:00
|
|
|
* { top: false, bottom: true, left: true, right: false }
|
|
|
|
*
|
|
|
|
*/
|
2015-09-08 10:01:06 +00:00
|
|
|
function isInbounds(bbox, target, padding) {
|
2015-08-25 09:16:10 +00:00
|
|
|
return {
|
2015-10-22 16:19:06 +00:00
|
|
|
top: bbox.y < target.y + padding.top,
|
|
|
|
bottom: bbox.y + bbox.height > target.y + target.height - padding.bottom,
|
|
|
|
left: bbox.x < target.x + padding.left,
|
|
|
|
right: bbox.x + bbox.width > target.x + target.width - padding.right,
|
2015-08-25 09:16:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-08 10:01:06 +00:00
|
|
|
* Expand the target shape if the bounding box of the moved elements is near or on an edge,
|
|
|
|
* considering the position of the bounding box in relation to the parent's edge plus padding.
|
|
|
|
* The amount to expand can be defined for each edge in the OFFSET object.
|
2015-08-25 09:16:10 +00:00
|
|
|
*
|
2015-10-22 16:19:06 +00:00
|
|
|
* @param {Array<Shape>} elements
|
|
|
|
* @param {Shape|String} target|targetId
|
2015-08-25 09:16:10 +00:00
|
|
|
*/
|
2015-09-08 10:01:06 +00:00
|
|
|
function expand(elements, target) {
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-10-22 16:19:06 +00:00
|
|
|
if (typeof target === 'string') {
|
|
|
|
target = elementRegistry.get(target);
|
|
|
|
}
|
|
|
|
|
2015-09-08 10:01:06 +00:00
|
|
|
var bbox = getBoundingBox(elements),
|
|
|
|
canExpand = true;
|
2015-09-01 14:20:57 +00:00
|
|
|
|
2015-09-14 13:14:12 +00:00
|
|
|
if (!is(target, 'bpmn:Participant') && !is(target, 'bpmn:Lane') && !(is(target, 'bpmn:SubProcess'))) {
|
2015-08-31 13:35:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-09-08 10:01:06 +00:00
|
|
|
forEach(elements, function(element) {
|
|
|
|
|
|
|
|
if (is(element, 'bpmn:Lane') || element.labelTarget) {
|
|
|
|
canExpand = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!canExpand) {
|
2015-08-31 13:35:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-09-08 10:01:06 +00:00
|
|
|
var inbounds = isInbounds(bbox, target, PADDING);
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-08-31 13:35:32 +00:00
|
|
|
var newBounds = pick(target, [ 'x', 'y', 'width', 'height' ]);
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-08-31 13:35:32 +00:00
|
|
|
if (inbounds.top) {
|
2015-09-08 10:01:06 +00:00
|
|
|
var topPosition = bbox.y - OFFSET.top;
|
2015-09-23 13:00:05 +00:00
|
|
|
assign(newBounds, { y: topPosition, height: newBounds.height + newBounds.y - topPosition });
|
2015-08-31 13:35:32 +00:00
|
|
|
}
|
2015-08-25 09:16:10 +00:00
|
|
|
|
2015-08-31 13:35:32 +00:00
|
|
|
if (inbounds.bottom) {
|
2015-09-23 13:00:05 +00:00
|
|
|
assign(newBounds, { height: bbox.y + bbox.height + OFFSET.bottom - newBounds.y });
|
2015-08-25 09:16:10 +00:00
|
|
|
}
|
2015-08-31 13:35:32 +00:00
|
|
|
|
|
|
|
if (inbounds.left) {
|
2015-09-08 10:01:06 +00:00
|
|
|
var leftPosition = bbox.x - OFFSET.left;
|
2015-09-23 13:00:05 +00:00
|
|
|
assign(newBounds, { x: leftPosition, width: newBounds.width + newBounds.x - leftPosition });
|
2015-08-31 13:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inbounds.right) {
|
2015-09-23 13:00:05 +00:00
|
|
|
assign(newBounds, { width: bbox.x + bbox.width + OFFSET.right - newBounds.x });
|
2015-08-31 13:35:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 08:49:18 +00:00
|
|
|
if (is(target, 'bpmn:Participant')) {
|
|
|
|
modeling.resizeLane(target, newBounds);
|
|
|
|
} else {
|
|
|
|
modeling.resizeShape(target, newBounds);
|
|
|
|
}
|
2015-09-15 12:47:16 +00:00
|
|
|
|
|
|
|
var parent = target.parent;
|
|
|
|
|
|
|
|
// recursively expand parent elements
|
|
|
|
if (parent) {
|
2015-09-23 13:00:05 +00:00
|
|
|
expand([ target ], parent);
|
2015-09-15 12:47:16 +00:00
|
|
|
}
|
2015-08-25 09:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:19:06 +00:00
|
|
|
AutoResize.$inject = [ 'eventBus', 'canvas', 'modeling', 'elementRegistry' ];
|
2015-08-25 09:16:10 +00:00
|
|
|
|
|
|
|
inherits(AutoResize, CommandInterceptor);
|
|
|
|
|
2016-05-03 12:38:30 +00:00
|
|
|
module.exports = AutoResize;
|