2015-08-19 14:27:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
|
|
|
|
var OrderingProvider = require('diagram-js/lib/features/ordering/OrderingProvider');
|
|
|
|
|
2015-10-09 23:34:46 +00:00
|
|
|
var isAny = require('../modeling/util/ModelingUtil').isAny;
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
var findIndex = require('lodash/array/findIndex');
|
|
|
|
|
|
|
|
var find = require('lodash/collection/find');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* a simple ordering provider that makes sure:
|
|
|
|
*
|
|
|
|
* (1) elements are ordered by a {level} property
|
|
|
|
* (2) elements with {alwaysOnTop} are always added to the root
|
|
|
|
*/
|
2016-02-25 16:40:56 +00:00
|
|
|
function BpmnOrderingProvider(eventBus, translate) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
OrderingProvider.call(this, eventBus);
|
|
|
|
|
|
|
|
var orders = [
|
2015-09-10 15:45:14 +00:00
|
|
|
{ type: 'bpmn:SubProcess', order: { level: 6 } },
|
2015-08-21 12:21:56 +00:00
|
|
|
{
|
|
|
|
type: 'bpmn:SequenceFlow',
|
|
|
|
order: {
|
2017-11-13 10:11:07 +00:00
|
|
|
level: 3,
|
2015-08-21 12:21:56 +00:00
|
|
|
containers: [
|
|
|
|
'bpmn:Participant',
|
|
|
|
'bpmn:FlowElementsContainer'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2016-12-06 13:49:45 +00:00
|
|
|
// handle DataAssociation(s) like message flows and render them always on top
|
2017-11-13 10:11:07 +00:00
|
|
|
{
|
|
|
|
type: 'bpmn:DataAssociation',
|
|
|
|
order: {
|
|
|
|
level: 9,
|
|
|
|
containers: [
|
|
|
|
'bpmn:Collaboration',
|
|
|
|
'bpmn:Process'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'bpmn:MessageFlow', order: {
|
|
|
|
level: 9,
|
|
|
|
containers: [ 'bpmn:Collaboration' ]
|
|
|
|
}
|
|
|
|
},
|
2015-08-21 12:21:56 +00:00
|
|
|
{
|
|
|
|
type: 'bpmn:Association',
|
|
|
|
order: {
|
|
|
|
level: 6,
|
|
|
|
containers: [
|
|
|
|
'bpmn:Participant',
|
|
|
|
'bpmn:FlowElementsContainer',
|
|
|
|
'bpmn:Collaboration'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2015-09-10 15:45:14 +00:00
|
|
|
{ type: 'bpmn:BoundaryEvent', order: { level: 8 } },
|
2017-11-13 10:11:07 +00:00
|
|
|
{ type: 'bpmn:FlowElement', order: { level: 5 } },
|
2015-08-19 14:27:07 +00:00
|
|
|
{ type: 'bpmn:Participant', order: { level: -2 } },
|
|
|
|
{ type: 'bpmn:Lane', order: { level: -1 } }
|
|
|
|
];
|
|
|
|
|
|
|
|
function computeOrder(element) {
|
2016-08-16 12:39:37 +00:00
|
|
|
if (element.labelTarget) {
|
|
|
|
return { level: 10 };
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:27:07 +00:00
|
|
|
var entry = find(orders, function(o) {
|
2016-03-16 10:26:41 +00:00
|
|
|
return isAny(element, [ o.type ]);
|
2015-08-19 14:27:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return entry && entry.order || { level: 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOrder(element) {
|
|
|
|
|
|
|
|
var order = element.order;
|
|
|
|
|
|
|
|
if (!order) {
|
|
|
|
element.order = order = computeOrder(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
2015-08-21 12:21:56 +00:00
|
|
|
function findActualParent(element, newParent, containers) {
|
|
|
|
|
|
|
|
var actualParent = newParent;
|
|
|
|
|
|
|
|
while (actualParent) {
|
|
|
|
|
|
|
|
if (isAny(actualParent, containers)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
actualParent = actualParent.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!actualParent) {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw new Error(translate('no parent for {element} in {parent}', {
|
2016-06-07 06:46:45 +00:00
|
|
|
element: element.id,
|
|
|
|
parent: newParent.id
|
|
|
|
}));
|
2015-08-21 12:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return actualParent;
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:27:07 +00:00
|
|
|
this.getOrdering = function(element, newParent) {
|
|
|
|
|
|
|
|
var elementOrder = getOrder(element);
|
|
|
|
|
|
|
|
|
2015-08-21 12:21:56 +00:00
|
|
|
if (elementOrder.containers) {
|
|
|
|
newParent = findActualParent(element, newParent, elementOrder.containers);
|
2015-08-19 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var currentIndex = newParent.children.indexOf(element);
|
|
|
|
|
|
|
|
var insertIndex = findIndex(newParent.children, function(child) {
|
2016-01-04 12:22:04 +00:00
|
|
|
|
|
|
|
// do not compare with labels, they are created
|
|
|
|
// in the wrong order (right after elements) during import and
|
|
|
|
// mess up the positioning.
|
|
|
|
if (!element.labelTarget && child.labelTarget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:27:07 +00:00
|
|
|
return elementOrder.level < getOrder(child).level;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// if the element is already in the child list at
|
|
|
|
// a smaller index, we need to adjust the inser index.
|
|
|
|
// this takes into account that the element is being removed
|
|
|
|
// before being re-inserted
|
|
|
|
if (insertIndex !== -1) {
|
|
|
|
if (currentIndex !== -1 && currentIndex < insertIndex) {
|
|
|
|
insertIndex -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
index: insertIndex,
|
|
|
|
parent: newParent
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-25 16:40:56 +00:00
|
|
|
BpmnOrderingProvider.$inject = [ 'eventBus', 'translate' ];
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
inherits(BpmnOrderingProvider, OrderingProvider);
|
|
|
|
|
2015-09-10 15:45:14 +00:00
|
|
|
module.exports = BpmnOrderingProvider;
|