1 import { reduce } from 'min-dash';
3 import inherits from 'inherits';
5 import { is } from 'bpmn-js/lib/util/ModelUtil';
7 import RuleProvider from 'diagram-js/lib/features/rules/RuleProvider';
9 var HIGH_PRIORITY = 1500;
11 function isCustom(element) {
12 return element && /^custom:/.test(element.type);
16 * Specific rules for custom elements
18 export default function CustomRules(eventBus) {
19 RuleProvider.call(this, eventBus);
22 inherits(CustomRules, RuleProvider);
24 CustomRules.$inject = ['eventBus'];
26 CustomRules.prototype.init = function() {
28 * Can shape be created on target container?
30 function canCreate(shape, target) {
31 // only judge about custom elements
32 if (!isCustom(shape)) {
36 // allow creation on processes
38 is(target, 'bpmn:Process') ||
39 is(target, 'bpmn:Participant') ||
40 is(target, 'bpmn:Collaboration')
45 * Can source and target be connected?
47 function canConnect(source, target) {
48 // only judge about custom elements
49 if (!isCustom(source) && !isCustom(target)) {
53 // allow connection between custom shape and task
54 if (isCustom(source)) {
55 if (is(target, 'bpmn:Task')) {
56 return { type: 'custom:connection' };
60 } else if (isCustom(target)) {
61 if (is(source, 'bpmn:Task')) {
62 return { type: 'custom:connection' };
69 this.addRule('elements.move', HIGH_PRIORITY, function(context) {
70 var target = context.target,
71 shapes = context.shapes;
75 // do not allow mixed movements of custom / BPMN shapes
76 // if any shape cannot be moved, the group cannot be moved, too
80 if (type === undefined) {
84 if (type !== isCustom(s) || result === false) {
88 return canCreate(s, target);
93 // reject, if we have at least one
94 // custom element that cannot be moved
98 this.addRule('shape.create', HIGH_PRIORITY, function(context) {
99 var target = context.target,
100 shape = context.shape;
102 return canCreate(shape, target);
105 this.addRule('shape.resize', HIGH_PRIORITY, function(context) {
106 var shape = context.shape;
108 if (isCustom(shape)) {
109 // cannot resize custom elements
114 this.addRule('connection.create', HIGH_PRIORITY, function(context) {
115 var source = context.source,
116 target = context.target;
118 return canConnect(source, target);
121 this.addRule('connection.reconnectStart', HIGH_PRIORITY, function(context) {
122 var connection = context.connection,
123 source = context.hover || context.source,
124 target = connection.target;
126 return canConnect(source, target, connection);
129 this.addRule('connection.reconnectEnd', HIGH_PRIORITY, function(context) {
130 var connection = context.connection,
131 source = connection.source,
132 target = context.hover || context.target;
134 return canConnect(source, target, connection);