Skip to content

Commit

Permalink
chore: use instanceof checks for model elements
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed May 3, 2023
1 parent 709be3b commit 0722797
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 39 deletions.
6 changes: 2 additions & 4 deletions lib/features/auto-place/BpmnAutoPlaceUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
getConnectedDistance
} from 'diagram-js/lib/features/auto-place/AutoPlaceUtil';

import { isConnection } from 'diagram-js/lib/model';

/**
* @typedef {import('../../model/Types').Shape} Shape
*
Expand Down Expand Up @@ -169,8 +171,4 @@ export function getDataElementPosition(source, element) {
};

return findFreePosition(source, element, position, generateGetNextPosition(nextPositionDirection));
}

function isConnection(element) {
return !!element.waypoints;
}
8 changes: 7 additions & 1 deletion lib/features/copy-paste/BpmnCopyPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default function BpmnCopyPaste(bpmnFactory, eventBus, moddleCopy) {
di = descriptor.di;

// wire existing di + businessObject for external label
if (isLabel(descriptor)) {
if (isLabelLike(descriptor)) {
descriptor.businessObject = getBusinessObject(cache[ descriptor.labelTarget ]);
descriptor.di = getDi(cache[ descriptor.labelTarget ]);

Expand Down Expand Up @@ -191,3 +191,9 @@ BpmnCopyPaste.$inject = [
'eventBus',
'moddleCopy'
];

// helpers //////////

function isLabelLike(descriptor) {
return descriptor.labelTarget;
}
6 changes: 2 additions & 4 deletions lib/features/modeling/behavior/CreateParticipantBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {

import { asTRBL } from 'diagram-js/lib/layout/LayoutUtil';

import { isConnection } from 'diagram-js/lib/model';

/**
* @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
* @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
Expand Down Expand Up @@ -229,10 +231,6 @@ function getParticipantCreateConstraints(shape, childrenBBox) {
};
}

function isConnection(element) {
return !!element.waypoints;
}

function findParticipant(elements) {
return find(elements, function(element) {
return is(element, 'bpmn:Participant');
Expand Down
6 changes: 2 additions & 4 deletions lib/features/modeling/cmd/SetColorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
isLabel
} from '../../../util/LabelUtil';

import { isConnection } from 'diagram-js/lib/model';

/**
* @typedef {import('diagram-js/lib/command/CommandHandler').default} CommandHandler
*
Expand Down Expand Up @@ -144,10 +146,6 @@ function colorToHex(color) {
return /^#[0-9a-fA-F]{6}$/.test(context.fillStyle) ? context.fillStyle : null;
}

function isConnection(element) {
return !!element.waypoints;
}

/**
* Add legacy properties if required.
*
Expand Down
11 changes: 2 additions & 9 deletions lib/features/rules/BpmnRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
getBoundaryAttachment as isBoundaryAttachment
} from '../snapping/BpmnSnappingUtil';

import { isConnection } from 'diagram-js/lib/model';

/**
* @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
*
Expand Down Expand Up @@ -486,15 +488,6 @@ function isEventBasedTarget(element) {
);
}

/**
* @param {Element} element
*
* @return {boolean}
*/
function isConnection(element) {
return element.waypoints;
}

/**
* @param {Element} element
*
Expand Down
13 changes: 4 additions & 9 deletions lib/util/LabelUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {

import { is } from './ModelUtil';

import { isLabel } from 'diagram-js/lib/model';

export { isLabel } from 'diagram-js/lib/model';

/**
* @typedef {import('diagram-js/lib/util/Types').Point} Point
* @typedef {import('diagram-js/lib/util/Types').Rect} Rect
Expand Down Expand Up @@ -170,15 +174,6 @@ export function getExternalLabelBounds(di, element) {
}, size);
}

/**
* @param {Element} element
*
* @return {boolean}
*/
export function isLabel(element) {
return element && !!element.labelTarget;
}

/**
* @param {ModdleElement} semantic
*
Expand Down
8 changes: 3 additions & 5 deletions test/spec/features/copy-paste/BpmnCopyPasteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
is
} from 'lib/util/ModelUtil';

import { isRoot } from 'diagram-js/lib/model';

/**
* @typedef {import('../../../../lib/model/Types').Element} Element
*/
Expand Down Expand Up @@ -1015,7 +1017,7 @@ function integrationTest(elementIds) {
});

// (4) move all elements except root
modeling.moveElements(elementRegistry.filter(isRoot), { x: 50, y: -50 });
modeling.moveElements(elementRegistry.filter(element => !isRoot(element)), { x: 50, y: -50 });

// when
// (5) undo moving, pasting and removing
Expand Down Expand Up @@ -1057,10 +1059,6 @@ function integrationTest(elementIds) {
};
}

function isRoot(element) {
return !!element.parent;
}

function getPropertyForElements(elements, property) {
return map(elements, function(element) {
return element[ property ];
Expand Down
6 changes: 3 additions & 3 deletions test/spec/features/rules/BpmnRulesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ describe('features/modeling/rules - BpmnRules', function() {
// given
var task1 = elementFactory.createShape({ type: 'bpmn:Task' }),
task2 = elementFactory.createShape({ type: 'bpmn:Task' }),
sequenceFlow = elementFactory.createConnection({
messageFlow = elementFactory.createConnection({
type: 'bpmn:MessageFlow',
source: task1,
target: task2
});

// then
expectCanCreate([ task1, task2, sequenceFlow ], 'Process', false);
expectCanCreate([ task1, task2, messageFlow ], 'Process', false);
}));


Expand Down Expand Up @@ -2157,7 +2157,7 @@ describe('features/modeling/rules - BpmnRules', function() {
it('should ignore label elements', inject(function(elementFactory, rules) {

// given
var label = elementFactory.createShape({ type: 'bpmn:FlowNode', labelTarget: {} });
var label = elementFactory.createLabel({});

// when
var result = rules.allowed('connection.start', { source: label });
Expand Down
9 changes: 9 additions & 0 deletions test/spec/features/rules/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
map
} from 'min-dash';

/**
* @typedef {import('../../../../lib/model/Types').Parent} Parent
* @typedef {import('../../../../lib/model/Types').Shape} Shape
*/

export function expectCanConnect(source, target, rules) {

Expand Down Expand Up @@ -49,6 +53,11 @@ export function expectCanDrop(element, target, expectedResult) {
}


/**
* @param {Shape|Element[]} shape Shape or array of elements to create.
* @param {Parent} target
* @param {any} expectedResult
*/
export function expectCanCreate(shape, target, expectedResult) {

var result = getBpmnJS().invoke(function(rules) {
Expand Down

0 comments on commit 0722797

Please sign in to comment.