2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getBpmnJS
|
|
|
|
} from 'test/TestHelper';
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
map,
|
|
|
|
forEach
|
|
|
|
} from 'min-dash';
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
function sign(x) {
|
2015-09-10 15:45:14 +00:00
|
|
|
x = +x; // convert to a number
|
|
|
|
if (x === 0 || isNaN(x)) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
return x > 0 ? 1 : -1;
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 15:45:14 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function move(elementIds, delta, targetId, isAttach) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
if (typeof elementIds === 'string') {
|
|
|
|
elementIds = [ elementIds ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof delta !== 'object') {
|
|
|
|
isAttach = targetId;
|
|
|
|
targetId = delta;
|
|
|
|
delta = { x: 0, y: 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof targetId !== 'string') {
|
|
|
|
isAttach = targetId;
|
|
|
|
targetId = null;
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
return getBpmnJS().invoke(function(canvas, elementRegistry, modeling) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
function getElement(id) {
|
|
|
|
|
|
|
|
var element = elementRegistry.get(id);
|
2015-09-10 15:45:14 +00:00
|
|
|
|
2015-08-19 14:27:07 +00:00
|
|
|
expect(element).to.exist;
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
var elements = map(elementIds, getElement),
|
2015-09-10 15:45:14 +00:00
|
|
|
target;
|
|
|
|
|
|
|
|
if (targetId === 'Root') {
|
|
|
|
target = canvas.getRootElement();
|
|
|
|
} else {
|
|
|
|
target = targetId && getElement(targetId);
|
|
|
|
}
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2017-12-20 09:36:07 +00:00
|
|
|
var hints = isAttach ? { attach: true } : {};
|
|
|
|
|
|
|
|
return modeling.moveElements(elements, delta, target, hints);
|
2015-08-19 14:27:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function add(attrs, position, target, isAttach) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
return getBpmnJS().invoke(function(canvas, elementRegistry, modeling) {
|
2016-01-04 12:22:04 +00:00
|
|
|
|
|
|
|
function getElement(id) {
|
|
|
|
|
|
|
|
var element = elementRegistry.get(id);
|
|
|
|
|
|
|
|
expect(element).to.exist;
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target) {
|
|
|
|
target = canvas.getRootElement();
|
|
|
|
} else
|
|
|
|
if (typeof target === 'string') {
|
|
|
|
target = getElement(target);
|
|
|
|
}
|
|
|
|
|
2017-12-20 09:36:07 +00:00
|
|
|
return modeling.createShape(attrs, position, target, { attach: isAttach });
|
2016-01-04 12:22:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function connect(source, target) {
|
2017-11-13 10:11:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
return getBpmnJS().invoke(function(canvas, elementRegistry, modeling) {
|
2017-11-13 10:11:07 +00:00
|
|
|
|
|
|
|
function getElement(id) {
|
|
|
|
|
|
|
|
var element = elementRegistry.get(id);
|
|
|
|
|
|
|
|
expect(element).to.exist;
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof target === 'string') {
|
|
|
|
target = getElement(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof source === 'string') {
|
|
|
|
source = getElement(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return modeling.connect(source, target);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function attach(attrs, position, target) {
|
2016-01-04 12:22:04 +00:00
|
|
|
return add(attrs, position, target, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-19 14:27:07 +00:00
|
|
|
function getAncestors(element) {
|
|
|
|
var ancestors = [];
|
|
|
|
|
|
|
|
while (element) {
|
|
|
|
ancestors.push(element);
|
|
|
|
|
|
|
|
element = element.parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ancestors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
function compareZOrder(a, b) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
var elementA,
|
|
|
|
elementB;
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
getBpmnJS().invoke(function(elementRegistry) {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
function getElement(id) {
|
|
|
|
|
|
|
|
var element = elementRegistry.get(id);
|
|
|
|
expect(element).to.exist;
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2017-11-13 10:11:07 +00:00
|
|
|
if (typeof a === 'string') {
|
|
|
|
a = getElement(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof b === 'string') {
|
|
|
|
b = getElement(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
elementA = a;
|
|
|
|
elementB = b;
|
2015-08-19 14:27:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var aAncestors = getAncestors(elementA),
|
|
|
|
bAncestors = getAncestors(elementB);
|
|
|
|
|
|
|
|
var sharedRoot = aAncestors.reduce(function(result, aAncestor, aParentIndex) {
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bParentIndex = bAncestors.indexOf(aAncestor);
|
|
|
|
|
|
|
|
if (bParentIndex !== -1) {
|
|
|
|
return {
|
|
|
|
a: aAncestors[aParentIndex - 1],
|
|
|
|
b: bAncestors[bParentIndex - 1],
|
|
|
|
parent: aAncestor
|
|
|
|
};
|
|
|
|
}
|
2015-09-10 15:45:14 +00:00
|
|
|
}, false);
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
// b contained in a
|
|
|
|
if (!sharedRoot.a) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// a contained in b
|
|
|
|
if (!sharedRoot.b) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-10 15:45:14 +00:00
|
|
|
var aIndex = sharedRoot.parent.children.indexOf(sharedRoot.a),
|
|
|
|
bIndex = sharedRoot.parent.children.indexOf(sharedRoot.b);
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
return sign(aIndex - bIndex);
|
2015-08-19 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function expectZOrder() {
|
2015-08-19 14:27:07 +00:00
|
|
|
|
|
|
|
var elements = Array.prototype.slice.call(arguments);
|
|
|
|
|
|
|
|
var next;
|
|
|
|
|
|
|
|
forEach(elements, function(e, idx) {
|
|
|
|
|
2015-09-10 15:45:14 +00:00
|
|
|
next = elements[idx + 1];
|
2015-08-19 14:27:07 +00:00
|
|
|
|
2018-12-04 10:48:36 +00:00
|
|
|
if (next && compareZOrder(e, next) !== -1) {
|
|
|
|
throw new Error(
|
|
|
|
'expected <element#' + next + '> to be in front of <element#' + e + '>'
|
|
|
|
);
|
2015-08-19 14:27:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|