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 4123430 commit 9a2f5fb
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 134 deletions.
9 changes: 2 additions & 7 deletions lib/features/bendpoints/BendpointSnapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { setSnapped } from '../snapping/SnapUtil';

import { getClosestPointOnConnection } from './BendpointUtil';

import { isConnection } from '../../model';

/**
* @typedef {import('../../core/EventBus').default} EventBus
*/
Expand Down Expand Up @@ -245,10 +247,3 @@ export default function BendpointSnapping(eventBus) {


BendpointSnapping.$inject = [ 'eventBus' ];


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

function isConnection(element) {
return element && !!element.waypoints;
}
25 changes: 15 additions & 10 deletions lib/features/copy-paste/CopyPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {

import { eachElement } from '../../util/Elements';

import {
isConnection,
isLabel
} from '../../model';

/**
* @typedef {import('../../core/Types').ElementLike} Element
* @typedef {import('../../core/Types').ShapeLike} Shape
Expand Down Expand Up @@ -314,7 +319,7 @@ CopyPaste.prototype._createElements = function(tree) {

var element;

if (isConnection(attrs)) {
if (isConnectionLike(attrs)) {
attrs.source = cache[ descriptor.source ];
attrs.target = cache[ descriptor.target ];

Expand All @@ -325,7 +330,7 @@ CopyPaste.prototype._createElements = function(tree) {
return;
}

if (isLabel(attrs)) {
if (isLabelLike(attrs)) {
attrs.labelTarget = cache[ attrs.labelTarget ];

element = cache[ descriptor.id ] = self.createLabel(attrs);
Expand Down Expand Up @@ -580,14 +585,6 @@ function isAttacher(element) {
return !!element.host;
}

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

function isLabel(element) {
return !!element.labelTarget;
}

function copyWaypoints(element) {
return map(element.waypoints, function(waypoint) {

Expand All @@ -614,3 +611,11 @@ function removeElement(element, elements) {

return elements.splice(index, 1);
}

function isConnectionLike(element) {
return element.waypoints;
}

function isLabelLike(element) {
return element.labelTarget;
}
17 changes: 7 additions & 10 deletions lib/features/create/Create.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {

import { getBBox } from '../../util/Elements';

import {
isConnection,
isLabel
} from '../../model';

/**
* @typedef {import('../../core/Types').ElementLike} Element
* @typedef {import('../../core/Types').ShapeLike} Shape
Expand Down Expand Up @@ -367,14 +372,6 @@ function ensureConstraints(event) {
}
}

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

function isSingleShape(elements) {
return elements && elements.length === 1 && !isConnection(elements[0]);
}

function isLabel(element) {
return !!element.labelTarget;
}
return elements && elements.length === 1 && !isConnection(elements[ 0 ]);
}
17 changes: 6 additions & 11 deletions lib/features/modeling/cmd/CreateElementsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
getParents
} from '../../../util/Elements';

import {
isConnection,
isLabel
} from '../../../model';

/**
* @typedef {import('../Modeling').default} Modeling
*/
Expand Down Expand Up @@ -127,14 +132,4 @@ CreateElementsHandler.prototype.preExecute = function(context) {
});

context.elements = values(cache);
};

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

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

function isLabel(element) {
return !!element.labelTarget;
}
};
6 changes: 2 additions & 4 deletions lib/features/modeling/cmd/DeleteShapeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {

import { saveClear } from '../../../util/Removal';

import { isConnection } from '../../../model';

/**
* @typedef {import('../../../core/Canvas').default} Canvas
* @typedef {import('../Modeling').default} Modeling
Expand Down Expand Up @@ -95,7 +97,3 @@ DeleteShapeHandler.prototype.revert = function(context) {

return shape;
};

function isConnection(element) {
return element.waypoints;
}
9 changes: 2 additions & 7 deletions lib/features/move/MovePreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {

import { translate } from '../../util/SvgTransformUtil';

import { isConnection } from '../../model';

/**
* @typedef {import('../../model/Types').Element} Element
*
Expand Down Expand Up @@ -252,10 +254,3 @@ function removeEdges(elements) {
function haveDifferentParents(elements) {
return size(groupBy(elements, function(e) { return e.parent && e.parent.id; })) !== 1;
}

/**
* Checks if an element is a connection.
*/
function isConnection(element) {
return element.waypoints;
}
6 changes: 2 additions & 4 deletions lib/features/resize/ResizeHandles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {

import { getReferencePoint } from './Resize';

import { isConnection } from '../../model';

/**
* @typedef {import('../../model/Types').Element} Element
*
Expand Down Expand Up @@ -210,8 +212,4 @@ function getHandleOffset(direction) {
}

return offset;
}

function isConnection(element) {
return !!element.waypoints;
}
13 changes: 5 additions & 8 deletions lib/features/snapping/CreateMoveSnapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
isNumber
} from 'min-dash';

import {
isConnection,
isLabel
} from '../../model';

/**
* @typedef {import('../../core/ElementRegistry').default} ElementRegistry
* @typedef {import('../../core/EventBus').default} EventBus
Expand Down Expand Up @@ -200,14 +205,6 @@ CreateMoveSnapping.prototype.getSnapTargets = function(shape, target) {

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

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

function isHidden(element) {
return !!element.hidden;
}

function isLabel(element) {
return !!element.labelTarget;
}
13 changes: 5 additions & 8 deletions lib/features/snapping/ResizeSnapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {

import { forEach } from 'min-dash';

import {
isConnection,
isLabel
} from '../../model';

/**
* @typedef {import('../../core/EventBus').default} EventBus
* @typedef {import('./Snapping').default} Snapping
Expand Down Expand Up @@ -157,18 +162,10 @@ function isAttached(element, host) {
return element.host === host;
}

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

function isHidden(element) {
return !!element.hidden;
}

function isLabel(element) {
return !!element.labelTarget;
}

function isHorizontal(direction) {
return direction === 'n' || direction === 's';
}
Expand Down
13 changes: 5 additions & 8 deletions lib/features/space-tool/SpaceTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import { set as setCursor } from '../../util/Cursor';

import { selfAndAllChildren } from '../../util/Elements';

import {
isConnection,
isLabel
} from '../../model';

/**
* @typedef {import('../../core/Types').ShapeLike} Shape
*
Expand Down Expand Up @@ -619,12 +624,4 @@ function includes(array, item) {

function isAttacher(element) {
return !!element.host;
}

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

function isLabel(element) {
return !!element.labelTarget;
}
12 changes: 2 additions & 10 deletions lib/features/space-tool/SpaceToolPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
translate
} from '../../util/SvgTransformUtil';

import { isConnection } from '../../model';

/**
* @typedef {import('../../core/Canvas').default} Canvas
* @typedef {import('../../core/ElementRegistry').default} ElementRegistry
Expand Down Expand Up @@ -301,13 +303,3 @@ SpaceToolPreview.$inject = [
'styles',
'previewSupport'
];


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

/**
* Checks if an element is a connection.
*/
function isConnection(element) {
return element.waypoints;
}
6 changes: 2 additions & 4 deletions lib/layout/LayoutUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

import intersectPaths from 'path-intersection';

import { isConnection } from '../model';

/**
* @typedef {import('../core/Types').ElementLike} Element
* @typedef {import('../core/Types').ConnectionLike} Connection
Expand Down Expand Up @@ -302,8 +304,4 @@ export function filterRedundantWaypoints(waypoints) {

function distance(a, b) {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}

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

0 comments on commit 9a2f5fb

Please sign in to comment.