Skip to content

Commit b1b7a45

Browse files
authored
feat(dia.Element): add getPortGroupNames() (#2629)
1 parent db07082 commit b1b7a45

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

examples/libavoid/src/avoid-router.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ export class AvoidRouter {
186186
// specific sides of the element.
187187

188188
// Add pins to each port of the element.
189-
const portGroups = Object.keys(element.prop('ports/groups'));
190-
portGroups.forEach((group) => {
189+
element.getPortGroupNames().forEach((group) => {
191190
const portsPositions = element.getPortsPositions(group);
192191
const { width, height } = element.size();
193192
const rect = new g.Rect(0, 0, width, height);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<pre class="docs-method-signature"><code>element.getPortGroupNames()</code></pre>
2+
3+
Returns an array of port group names of the element.
4+
It returns all port group names defined on the element regardless of whether the port group has any ports or not.
5+

packages/joint-core/src/dia/ports.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ export const elementPortPrototype = {
280280
}));
281281
},
282282

283+
getPortGroupNames: function() {
284+
return Object.keys(this._portSettingsData.groups);
285+
},
286+
283287
/**
284288
* @param {string} groupName
285289
* @returns {Object<portId, {x: number, y: number, angle: number}>}

packages/joint-core/test/jointjs/elementPorts.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,37 @@ QUnit.module('element ports', function() {
20192019
});
20202020
});
20212021

2022+
QUnit.module('getPortGroupNames', function() {
2023+
2024+
QUnit.test('return group names of all ports', function(assert) {
2025+
2026+
const shape = new joint.shapes.standard.Rectangle();
2027+
2028+
assert.deepEqual(shape.getPortGroupNames(), [], 'no groups');
2029+
2030+
shape.set({
2031+
ports: {
2032+
groups: {
2033+
a: { position: 'left' },
2034+
b: { position: 'right' }
2035+
},
2036+
items: [
2037+
{ id: 'one', group: 'a' },
2038+
{ id: 'two', group: 'b' },
2039+
{ id: 'three', group: 'b' },
2040+
{ id: 'four', group: 'b' }
2041+
]
2042+
}
2043+
});
2044+
2045+
assert.deepEqual(shape.getPortGroupNames(), ['a', 'b'], 'group with ports');
2046+
2047+
shape.prop('ports/groups/c', { position: 'top' });
2048+
2049+
assert.deepEqual(shape.getPortGroupNames(), ['a', 'b', 'c'], 'group without ports');
2050+
});
2051+
});
2052+
20222053
QUnit.module('portProp', function() {
20232054

20242055
QUnit.test('set port properties', function(assert) {

packages/joint-core/types/joint.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ export namespace dia {
559559

560560
getPortIndex(port: string | Element.Port): number;
561561

562+
getPortGroupNames(): string[];
563+
562564
portProp(portId: string, path: dia.Path): any;
563565

564566
portProp(portId: string, path: dia.Path, value?: any, opt?: S): Element;

0 commit comments

Comments
 (0)