Skip to content

Commit

Permalink
feat(dia.Element): add getPortGroupNames() (#2629)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumilingus authored Jul 11, 2024
1 parent db07082 commit b1b7a45
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
3 changes: 1 addition & 2 deletions examples/libavoid/src/avoid-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ export class AvoidRouter {
// specific sides of the element.

// Add pins to each port of the element.
const portGroups = Object.keys(element.prop('ports/groups'));
portGroups.forEach((group) => {
element.getPortGroupNames().forEach((group) => {
const portsPositions = element.getPortsPositions(group);
const { width, height } = element.size();
const rect = new g.Rect(0, 0, width, height);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<pre class="docs-method-signature"><code>element.getPortGroupNames()</code></pre>

Returns an array of port group names of the element.
It returns all port group names defined on the element regardless of whether the port group has any ports or not.

4 changes: 4 additions & 0 deletions packages/joint-core/src/dia/ports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ export const elementPortPrototype = {
}));
},

getPortGroupNames: function() {
return Object.keys(this._portSettingsData.groups);
},

/**
* @param {string} groupName
* @returns {Object<portId, {x: number, y: number, angle: number}>}
Expand Down
31 changes: 31 additions & 0 deletions packages/joint-core/test/jointjs/elementPorts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,37 @@ QUnit.module('element ports', function() {
});
});

QUnit.module('getPortGroupNames', function() {

QUnit.test('return group names of all ports', function(assert) {

const shape = new joint.shapes.standard.Rectangle();

assert.deepEqual(shape.getPortGroupNames(), [], 'no groups');

shape.set({
ports: {
groups: {
a: { position: 'left' },
b: { position: 'right' }
},
items: [
{ id: 'one', group: 'a' },
{ id: 'two', group: 'b' },
{ id: 'three', group: 'b' },
{ id: 'four', group: 'b' }
]
}
});

assert.deepEqual(shape.getPortGroupNames(), ['a', 'b'], 'group with ports');

shape.prop('ports/groups/c', { position: 'top' });

assert.deepEqual(shape.getPortGroupNames(), ['a', 'b', 'c'], 'group without ports');
});
});

QUnit.module('portProp', function() {

QUnit.test('set port properties', function(assert) {
Expand Down
2 changes: 2 additions & 0 deletions packages/joint-core/types/joint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ export namespace dia {

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

getPortGroupNames(): string[];

portProp(portId: string, path: dia.Path): any;

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

0 comments on commit b1b7a45

Please sign in to comment.