2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
collectLanes,
|
|
|
|
getLanesRoot
|
|
|
|
} from '../util/LaneUtil';
|
2015-10-15 21:50:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
is
|
|
|
|
} from '../../../util/ModelUtil';
|
2015-10-15 21:50:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
add as collectionAdd,
|
|
|
|
remove as collectionRemove
|
|
|
|
} from 'diagram-js/lib/util/Collections';
|
2015-10-15 21:50:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
asTRBL
|
|
|
|
} from 'diagram-js/lib/layout/LayoutUtil';
|
2015-10-15 21:50:15 +00:00
|
|
|
|
|
|
|
var FLOW_NODE_REFS_ATTR = 'flowNodeRef',
|
|
|
|
LANES_ATTR = 'lanes';
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
|
2015-10-15 21:50:15 +00:00
|
|
|
/**
|
|
|
|
* A handler that updates lane refs on changed elements
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function UpdateFlowNodeRefsHandler(elementRegistry) {
|
2015-10-15 21:50:15 +00:00
|
|
|
this._elementRegistry = elementRegistry;
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
UpdateFlowNodeRefsHandler.$inject = [
|
|
|
|
'elementRegistry'
|
|
|
|
];
|
2015-10-15 21:50:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
UpdateFlowNodeRefsHandler.prototype.computeUpdates = function(flowNodeShapes, laneShapes) {
|
|
|
|
|
|
|
|
var handledNodes = {};
|
|
|
|
|
|
|
|
var updates = [];
|
|
|
|
|
|
|
|
var participantCache = {};
|
|
|
|
|
|
|
|
var allFlowNodeShapes = [];
|
|
|
|
|
|
|
|
function isInLaneShape(element, laneShape) {
|
|
|
|
|
|
|
|
var laneTrbl = asTRBL(laneShape);
|
|
|
|
|
|
|
|
var elementMid = {
|
|
|
|
x: element.x + element.width / 2,
|
|
|
|
y: element.y + element.height / 2
|
|
|
|
};
|
|
|
|
|
|
|
|
return elementMid.x > laneTrbl.left &&
|
|
|
|
elementMid.x < laneTrbl.right &&
|
|
|
|
elementMid.y > laneTrbl.top &&
|
|
|
|
elementMid.y < laneTrbl.bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addFlowNodeShape(flowNodeShape) {
|
|
|
|
if (!handledNodes[flowNodeShape.id]) {
|
|
|
|
allFlowNodeShapes.push(flowNodeShape);
|
|
|
|
handledNodes[flowNodeShape.id] = flowNodeShape;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAllLaneShapes(flowNodeShape) {
|
|
|
|
|
|
|
|
var root = getLanesRoot(flowNodeShape);
|
|
|
|
|
|
|
|
if (!participantCache[root.id]) {
|
|
|
|
participantCache[root.id] = collectLanes(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
return participantCache[root.id];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewLanes(flowNodeShape) {
|
|
|
|
if (!flowNodeShape.parent) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var allLaneShapes = getAllLaneShapes(flowNodeShape);
|
|
|
|
|
|
|
|
return allLaneShapes.filter(function(l) {
|
|
|
|
return isInLaneShape(flowNodeShape, l);
|
|
|
|
}).map(function(shape) {
|
|
|
|
return shape.businessObject;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
laneShapes.forEach(function(laneShape) {
|
|
|
|
var root = getLanesRoot(laneShape);
|
|
|
|
|
|
|
|
if (!root || handledNodes[root.id]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var children = root.children.filter(function(c) {
|
|
|
|
return is(c, 'bpmn:FlowNode');
|
|
|
|
});
|
|
|
|
|
|
|
|
children.forEach(addFlowNodeShape);
|
|
|
|
|
|
|
|
handledNodes[root.id] = root;
|
|
|
|
});
|
|
|
|
|
|
|
|
flowNodeShapes.forEach(addFlowNodeShape);
|
|
|
|
|
|
|
|
|
|
|
|
allFlowNodeShapes.forEach(function(flowNodeShape) {
|
|
|
|
|
|
|
|
var flowNode = flowNodeShape.businessObject;
|
|
|
|
|
|
|
|
var lanes = flowNode.get(LANES_ATTR),
|
|
|
|
remove = lanes.slice(),
|
|
|
|
add = getNewLanes(flowNodeShape);
|
|
|
|
|
|
|
|
updates.push({ flowNode: flowNode, remove: remove, add: add });
|
|
|
|
});
|
|
|
|
|
|
|
|
laneShapes.forEach(function(laneShape) {
|
|
|
|
|
|
|
|
var lane = laneShape.businessObject;
|
|
|
|
|
|
|
|
// lane got removed XX-)
|
|
|
|
if (!laneShape.parent) {
|
|
|
|
lane.get(FLOW_NODE_REFS_ATTR).forEach(function(flowNode) {
|
|
|
|
updates.push({ flowNode: flowNode, remove: [ lane ], add: [] });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return updates;
|
|
|
|
};
|
|
|
|
|
|
|
|
UpdateFlowNodeRefsHandler.prototype.execute = function(context) {
|
|
|
|
|
|
|
|
var updates = context.updates;
|
|
|
|
|
|
|
|
if (!updates) {
|
|
|
|
updates = context.updates = this.computeUpdates(context.flowNodeShapes, context.laneShapes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updates.forEach(function(update) {
|
|
|
|
|
|
|
|
var flowNode = update.flowNode,
|
|
|
|
lanes = flowNode.get(LANES_ATTR);
|
|
|
|
|
|
|
|
// unwire old
|
|
|
|
update.remove.forEach(function(oldLane) {
|
2018-04-02 19:01:53 +00:00
|
|
|
collectionRemove(lanes, oldLane);
|
|
|
|
collectionRemove(oldLane.get(FLOW_NODE_REFS_ATTR), flowNode);
|
2015-10-15 21:50:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// wire new
|
|
|
|
update.add.forEach(function(newLane) {
|
2018-04-02 19:01:53 +00:00
|
|
|
collectionAdd(lanes, newLane);
|
|
|
|
collectionAdd(newLane.get(FLOW_NODE_REFS_ATTR), flowNode);
|
2015-10-15 21:50:15 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-03 16:09:43 +00:00
|
|
|
|
|
|
|
// TODO(nikku): return changed elements
|
|
|
|
// return [ ... ];
|
2015-10-15 21:50:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
UpdateFlowNodeRefsHandler.prototype.revert = function(context) {
|
|
|
|
|
|
|
|
var updates = context.updates;
|
|
|
|
|
|
|
|
updates.forEach(function(update) {
|
|
|
|
|
|
|
|
var flowNode = update.flowNode,
|
|
|
|
lanes = flowNode.get(LANES_ATTR);
|
|
|
|
|
|
|
|
// unwire new
|
|
|
|
update.add.forEach(function(newLane) {
|
2018-04-02 19:01:53 +00:00
|
|
|
collectionRemove(lanes, newLane);
|
|
|
|
collectionRemove(newLane.get(FLOW_NODE_REFS_ATTR), flowNode);
|
2015-10-15 21:50:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// wire old
|
|
|
|
update.remove.forEach(function(oldLane) {
|
2018-04-02 19:01:53 +00:00
|
|
|
collectionAdd(lanes, oldLane);
|
|
|
|
collectionAdd(oldLane.get(FLOW_NODE_REFS_ATTR), flowNode);
|
2015-10-15 21:50:15 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-03 16:09:43 +00:00
|
|
|
|
|
|
|
// TODO(nikku): return changed elements
|
|
|
|
// return [ ... ];
|
2015-10-15 21:50:15 +00:00
|
|
|
};
|