fix(auto-resize): expand non-primary parents in multi select move

Closes #397
This commit is contained in:
pedesen 2015-10-22 18:19:06 +02:00
parent 4930fd72d4
commit 8704d5d5a0
2 changed files with 37 additions and 38 deletions

View File

@ -9,7 +9,8 @@ var pick = require('lodash/object/pick'),
assign = require('lodash/object/assign'), assign = require('lodash/object/assign'),
forEach = require('lodash/collection/forEach'), forEach = require('lodash/collection/forEach'),
values = require('lodash/object/values'), values = require('lodash/object/values'),
flatten = require('lodash/array/flatten'); flatten = require('lodash/array/flatten'),
groupBy = require('lodash/collection/groupBy');
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor'); var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
@ -20,7 +21,7 @@ var PADDING = { top: 2, bottom: 2, left: 15, right: 15 };
* An auto resize component that takes care of expanding parent participants * An auto resize component that takes care of expanding parent participants
* and lanes if elements are modeled close to an edge of the parent element. * and lanes if elements are modeled close to an edge of the parent element.
*/ */
function AutoResize(eventBus, canvas, modeling){ function AutoResize(eventBus, canvas, modeling, elementRegistry){
CommandInterceptor.call(this, eventBus); CommandInterceptor.call(this, eventBus);
@ -35,27 +36,15 @@ function AutoResize(eventBus, canvas, modeling){
this.postExecuted([ 'elements.move' ], function(event) { this.postExecuted([ 'elements.move' ], function(event) {
var context = event.context, var context = event.context,
elements = [], elements = flatten(values(context.closure.topLevel));
parent = context.parent || context.newParent,
oldParent = context.hints && context.hints.oldParent,
primaryShape = context.hints && context.hints.primaryShape;
forEach(flatten(values(context.closure.topLevel)), function(element) { var expandings = groupBy(elements, function(element){
/** return element.parent.id;
* exclude the element, when:
* - the primary selection does not change parent AND
* - the primary selection parent is the new parent AND
* - its parent is different from the new parent
*/
if (oldParent && oldParent === parent &&
primaryShape && primaryShape.parent === parent &&
element.parent !== parent) {
return;
}
elements.push(element);
}); });
expand(elements, parent); forEach(expandings, function(elements, parentId) {
expand(elements, parentId);
});
}); });
/** /**
@ -76,10 +65,10 @@ function AutoResize(eventBus, canvas, modeling){
*/ */
function isInbounds(bbox, target, padding) { function isInbounds(bbox, target, padding) {
return { return {
top: bbox.y < target.y + padding.top && bbox.y + bbox.height > target.y, top: bbox.y < target.y + padding.top,
bottom: bbox.y < target.y + target.height && bbox.y + bbox.height > target.y + target.height - padding.bottom, bottom: bbox.y + bbox.height > target.y + target.height - padding.bottom,
left: bbox.x < target.x + padding.left && bbox.x + bbox.width > target.x, left: bbox.x < target.x + padding.left,
right: bbox.x < target.x + target.width && bbox.x + bbox.width > target.x + target.width - padding.right, right: bbox.x + bbox.width > target.x + target.width - padding.right,
}; };
} }
@ -88,11 +77,15 @@ function AutoResize(eventBus, canvas, modeling){
* considering the position of the bounding box in relation to the parent's edge plus padding. * 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. * The amount to expand can be defined for each edge in the OFFSET object.
* *
* @param {Array<Shape>} [elements] * @param {Array<Shape>} elements
* @param {Shape} target * @param {Shape|String} target|targetId
*/ */
function expand(elements, target) { function expand(elements, target) {
if (typeof target === 'string') {
target = elementRegistry.get(target);
}
var bbox = getBoundingBox(elements), var bbox = getBoundingBox(elements),
canExpand = true; canExpand = true;
@ -149,7 +142,7 @@ function AutoResize(eventBus, canvas, modeling){
} }
} }
AutoResize.$inject = [ 'eventBus', 'canvas', 'modeling' ]; AutoResize.$inject = [ 'eventBus', 'canvas', 'modeling', 'elementRegistry' ];
inherits(AutoResize, CommandInterceptor); inherits(AutoResize, CommandInterceptor);

View File

@ -120,17 +120,6 @@ describe('features/auto-resize', function() {
})); }));
it('should not resize the parent if element is placed too far outside',
inject(function(modeling) {
// when
modeling.moveElements([ task ], { x: 300, y: 0 }, participant);
// then
expect(participant).to.have.bounds(originalBounds);
}));
it('should resize the parent if element and parent edge intersect', it('should resize the parent if element and parent edge intersect',
inject(function(modeling) { inject(function(modeling) {
@ -456,6 +445,23 @@ describe('features/auto-resize', function() {
})); }));
it('should expand non-primary parents',
inject(function(modeling) {
// given
var originalBounds = getBounds(subProcessShape_1);
// when
modeling.moveElements([ taskShape_1, taskShape_2 ],
{ x: 100, y: 0 }, rootShape, { primaryShape: taskShape_2 });
// then
var expectedBounds = assign(originalBounds, { width: 525 });
expect(subProcessShape_1).to.have.bounds(expectedBounds);
}));
it('should expand, if elements keep their parents (same original parent)', inject(function(modeling) { it('should expand, if elements keep their parents (same original parent)', inject(function(modeling) {
// given // given