2018-04-02 19:01:53 +00:00
|
|
|
import { forEach } from 'min-dash';
|
2015-09-08 12:38:50 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2015-09-08 12:38:50 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import RuleProvider from 'diagram-js/lib/features/rules/RuleProvider';
|
2015-09-08 12:38:50 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
var HIGH_PRIORITY = 1500;
|
2015-09-08 12:38:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specific rules for custom elements
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function CustomRules(eventBus) {
|
2015-09-08 12:38:50 +00:00
|
|
|
RuleProvider.call(this, eventBus);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(CustomRules, RuleProvider);
|
|
|
|
|
|
|
|
CustomRules.$inject = [ 'eventBus' ];
|
|
|
|
|
|
|
|
|
|
|
|
CustomRules.prototype.init = function() {
|
|
|
|
|
|
|
|
this.addRule('connection.create', HIGH_PRIORITY, function(context) {
|
|
|
|
var source = context.source,
|
|
|
|
target = context.target;
|
|
|
|
|
|
|
|
return canConnect(source, target);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('connection.reconnectStart', HIGH_PRIORITY, function(context) {
|
|
|
|
|
|
|
|
var connection = context.connection,
|
|
|
|
source = context.hover || context.source,
|
|
|
|
target = connection.target;
|
|
|
|
|
|
|
|
return canConnect(source, target, connection);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('connection.reconnectEnd', HIGH_PRIORITY, function(context) {
|
|
|
|
|
|
|
|
var connection = context.connection,
|
|
|
|
source = connection.source,
|
|
|
|
target = context.hover || context.target;
|
|
|
|
|
|
|
|
return canConnect(source, target, connection);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('connection.updateWaypoints', HIGH_PRIORITY, function(context) {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2015-09-08 12:38:50 +00:00
|
|
|
// OK! but visually ignore
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('elements.move', HIGH_PRIORITY, function(context) {
|
|
|
|
|
|
|
|
var target = context.target,
|
|
|
|
shapes = context.shapes,
|
|
|
|
position = context.position;
|
|
|
|
|
|
|
|
return canMove(shapes, target, position);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('shape.create', HIGH_PRIORITY, function(context) {
|
|
|
|
var target = context.target,
|
|
|
|
shape = context.shape,
|
|
|
|
position = context.position;
|
|
|
|
|
|
|
|
return canCreate(shape, target, position);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addRule('shape.resize', HIGH_PRIORITY, function(context) {
|
|
|
|
var shape = context.shape;
|
|
|
|
|
|
|
|
if (isCustom(shape)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function canConnect(source, target) {
|
2019-05-17 11:11:17 +00:00
|
|
|
return !isCustom(source) && !isCustom(target);
|
2015-09-08 12:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function canCreate(shape, target) {
|
|
|
|
if (isType(target, 'custom:triangle')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isType(target, 'custom:circle')) {
|
|
|
|
if (isType(shape, 'custom:triangle')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCustom(shape)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function canMove(shapes, target, position) {
|
|
|
|
var result;
|
|
|
|
|
|
|
|
forEach(shapes, function(shape) {
|
|
|
|
if (isType(shape, 'custom:triangle') && isType(target, 'custom:circle')) {
|
|
|
|
result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCustom(target)) {
|
|
|
|
result = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCustom(shape)) {
|
|
|
|
result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-04-02 19:01:53 +00:00
|
|
|
|
|
|
|
function isType(element, type) {
|
|
|
|
var patt = new RegExp(type, 'i');
|
|
|
|
|
|
|
|
return element && patt.test(element.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isCustom(element) {
|
|
|
|
return element && /^custom:/.test(element.type);
|
|
|
|
}
|