-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/ui/src/components/Visualization/Custom/target-anchor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Node, Point, Rect } from '@patternfly/react-topology'; | ||
import { TargetAnchor } from './target-anchor'; | ||
|
||
describe('TargetAnchor', () => { | ||
/** | ||
* ______ | ||
* | | height: 50 | ||
* |____| | ||
* width: 50 | ||
* | ||
* (*) reference point | ||
* (x: 100, y: 100) | ||
*/ | ||
it('should return the closest point of a rectangle to a reference point below', () => { | ||
const element = { | ||
getBounds: () => new Rect(0, 0, 50, 50), | ||
} as Node; | ||
const targetAnchor = new TargetAnchor(element); | ||
|
||
const reference = new Point(100, 100); | ||
const result = targetAnchor.getLocation(reference); | ||
|
||
expect(result).toEqual({ x: 50, y: 50 }); | ||
}); | ||
|
||
/** | ||
* | ||
* (*) reference point | ||
* (x: 25, y: 25) | ||
* ______ | ||
* | | height: 50 | ||
* |____| | ||
* width: 50 | ||
* top left corner: (0, 100) | ||
*/ | ||
it('should return the closest point of a rectangle to a reference point above', () => { | ||
const reference = new Point(25, 25); | ||
|
||
const element = { | ||
getBounds: () => new Rect(0, 100, 50, 50), | ||
} as Node; | ||
const targetAnchor = new TargetAnchor(element); | ||
|
||
const result = targetAnchor.getLocation(reference); | ||
|
||
expect(result).toEqual({ x: 25, y: 100 }); | ||
}); | ||
|
||
it('should delegate to the super class to get the reference point', () => { | ||
const superSpy = jest.spyOn(TargetAnchor.prototype, 'getReferencePoint'); | ||
const element = { | ||
getBounds: () => new Rect(0, 0, 50, 50), | ||
} as Node; | ||
|
||
const targetAnchor = new TargetAnchor(element); | ||
targetAnchor.getReferencePoint(); | ||
|
||
expect(superSpy).toHaveBeenCalled(); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/ui/src/components/Visualization/Custom/target-anchor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { AbstractAnchor, Point, Rect } from '@patternfly/react-topology'; | ||
|
||
export class TargetAnchor extends AbstractAnchor { | ||
getLocation(reference: Point): Point { | ||
return this.closestPointOnRectangle(this.owner.getBounds(), reference); | ||
} | ||
|
||
getReferencePoint(): Point { | ||
return super.getReferencePoint(); | ||
} | ||
|
||
private closestPointOnRectangle(rect: Rect, point: Point): Point { | ||
// Deconstruct the rectangle and point parameters | ||
const { x: rx, y: ry, width, height } = rect; | ||
const { x: px, y: py } = point; | ||
|
||
// Calculate the projections on the edges | ||
// For left edge | ||
const leftX = rx; | ||
const leftY = Math.max(ry, Math.min(py, ry + height)); | ||
|
||
// For right edge | ||
const rightX = rx + width; | ||
const rightY = Math.max(ry, Math.min(py, ry + height)); | ||
|
||
// For top edge | ||
const topX = Math.max(rx, Math.min(px, rx + width)); | ||
const topY = ry; | ||
|
||
// For bottom edge | ||
const bottomX = Math.max(rx, Math.min(px, rx + width)); | ||
const bottomY = ry + height; | ||
|
||
// Calculate distances to each edge projection | ||
const distances = [ | ||
{ x: leftX, y: leftY, dist: Math.hypot(px - leftX, py - leftY) }, | ||
{ x: rightX, y: rightY, dist: Math.hypot(px - rightX, py - rightY) }, | ||
{ x: topX, y: topY, dist: Math.hypot(px - topX, py - topY) }, | ||
{ x: bottomX, y: bottomY, dist: Math.hypot(px - bottomX, py - bottomY) }, | ||
]; | ||
|
||
// Find the minimum distance | ||
const closestPoint = distances.reduce( | ||
(minPoint, currentPoint) => (currentPoint.dist < minPoint.dist ? currentPoint : minPoint), | ||
{ x: 0, y: 0, dist: 0 }, | ||
); | ||
|
||
// Return the closest point | ||
return new Point(closestPoint.x, closestPoint.y); | ||
} | ||
} |