Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to select nodes in network area diagram viewer #90

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion demo/src/diagram-viewers/add-diagrams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
OnBusCallbackType,
OnFeederCallbackType,
OnNextVoltageCallbackType,
OnMoveNodeCallbackType,
OnSelectNodeCallbackType,
} from '../../../src';

export const addNadToDemo = () => {
Expand All @@ -38,6 +40,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand All @@ -58,6 +61,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
false
);

Expand All @@ -78,6 +82,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand All @@ -100,6 +105,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand All @@ -120,6 +126,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand All @@ -140,6 +147,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand All @@ -160,6 +168,7 @@ export const addNadToDemo = () => {
1000,
1200,
handleNodeMove,
handleNodeSelect,
true
);

Expand Down Expand Up @@ -307,7 +316,14 @@ const handleTogglePopover: HandleTogglePopoverType = (
}
};

const handleNodeMove = (equipmentId, nodeId, x, y, xOrig, yOrig) => {
const handleNodeMove: OnMoveNodeCallbackType = (
equipmentId,
nodeId,
x,
y,
xOrig,
yOrig
) => {
const msg =
'Node ' +
nodeId +
Expand All @@ -324,3 +340,22 @@ const handleNodeMove = (equipmentId, nodeId, x, y, xOrig, yOrig) => {
']';
console.log(msg);
};

const handleNodeSelect: OnSelectNodeCallbackType = (
equipmentId,
nodeId,
x,
y
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
) => {
const msg =
'Node ' +
nodeId +
' equipment ' +
equipmentId +
' position [' +
x +
', ' +
y +
'] selected';
console.log(msg);
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Test network-area-diagram-viewer', () => {
0,
0,
null,
null,
false
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export type OnMoveNodeCallbackType = (
yOrig: number
) => void;

export type OnSelectNodeCallbackType = (
equipmentId: string,
nodeId: string,
x: number,
y: number
) => void;

export class NetworkAreaDiagramViewer {
container: HTMLElement;
svgContent: string;
Expand All @@ -37,7 +44,8 @@ export class NetworkAreaDiagramViewer {
initialPosition: Point = new Point(0, 0);
svgParameters: SvgParameters;
edgeAngles: Map<string, number> = new Map<string, number>();
onNodeCallback: OnMoveNodeCallbackType | null;
onMoveNodeCallback: OnMoveNodeCallbackType | null;
onSelectNodeCallback: OnSelectNodeCallbackType | null;

constructor(
container: HTMLElement,
Expand All @@ -46,7 +54,8 @@ export class NetworkAreaDiagramViewer {
minHeight: number,
maxWidth: number,
maxHeight: number,
onNodeCallback: OnMoveNodeCallbackType | null,
onMoveNodeCallback: OnMoveNodeCallbackType | null,
onSelectNodeCallback: OnSelectNodeCallbackType | null,
enableNodeMoving: boolean
) {
this.container = container;
Expand All @@ -57,7 +66,8 @@ export class NetworkAreaDiagramViewer {
this.originalHeight = 0;
this.init(minWidth, minHeight, maxWidth, maxHeight, enableNodeMoving);
this.svgParameters = this.getSvgParameters();
this.onNodeCallback = onNodeCallback;
this.onMoveNodeCallback = onMoveNodeCallback;
this.onSelectNodeCallback = onSelectNodeCallback;
}

public setWidth(width: number): void {
Expand Down Expand Up @@ -272,27 +282,41 @@ export class NetworkAreaDiagramViewer {
if (svg != null) {
svg.style.cursor = 'grabbing';
}
this.initialPosition = DiagramUtils.getPosition(this.selectedElement); // used for the offset
this.edgeAngles = new Map<string, number>();
if (!(event as MouseEvent).shiftKey) {
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
this.initialPosition = DiagramUtils.getPosition(
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
this.selectedElement
); // used for the offset
this.edgeAngles = new Map<string, number>();
}
}

private drag(event: Event) {
if (this.selectedElement) {
event.preventDefault();
this.ctm = this.svgDraw?.node.getScreenCTM();
const mousePosition = this.getMousePosition(event as MouseEvent);
this.updateGraph(mousePosition);
this.initialPosition = DiagramUtils.getPosition(
this.selectedElement
);
if (!(event as MouseEvent).shiftKey) {
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
this.ctm = this.svgDraw?.node.getScreenCTM();
const mousePosition = this.getMousePosition(
event as MouseEvent
);
this.updateGraph(mousePosition);
this.initialPosition = DiagramUtils.getPosition(
this.selectedElement
);
}
}
}

private endDrag(event: Event) {
if (this.selectedElement) {
const mousePosition = this.getMousePosition(event as MouseEvent);
this.updateGraph(mousePosition);
this.updateMetadataCallCallback(mousePosition);
if (!(event as MouseEvent).shiftKey) {
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
const mousePosition = this.getMousePosition(
event as MouseEvent
);
this.updateGraph(mousePosition);
this.updateMetadataCallCallback(mousePosition);
} else {
this.callSelectNodeCallback();
}
const svg: HTMLElement = <HTMLElement>(
this.svgDraw?.node.firstElementChild?.parentElement
);
Expand Down Expand Up @@ -1276,9 +1300,9 @@ export class NetworkAreaDiagramViewer {
// update node position in metadata
node.setAttribute('x', xNew);
node.setAttribute('y', yNew);
// call the callback, if defined
if (this.onNodeCallback != null) {
this.onNodeCallback(
// call the move node callback, if defined
if (this.onMoveNodeCallback != null) {
this.onMoveNodeCallback(
node.getAttribute('equipmentid') ?? '',
node.getAttribute('svgid') ?? '',
+xNew,
Expand All @@ -1289,4 +1313,25 @@ export class NetworkAreaDiagramViewer {
}
}
}

private callSelectNodeCallback() {
// call the select node callback, if defined
if (this.onSelectNodeCallback != null) {
// get selected node from metadata
const node: SVGGraphicsElement | null =
this.container.querySelector(
'nad\\:node[svgid="' + this.selectedElement?.id + '"]'
);
if (node != null) {
const x = node.getAttribute('x') ?? '0';
const y = node.getAttribute('y') ?? '0';
this.onSelectNodeCallback(
node.getAttribute('equipmentid') ?? '',
node.getAttribute('svgid') ?? '',
+x,
+y
);
}
}
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/

export { NetworkAreaDiagramViewer } from './components/network-area-diagram-viewer/network-area-diagram-viewer';
export type {
OnMoveNodeCallbackType,
OnSelectNodeCallbackType,
} from './components/network-area-diagram-viewer/network-area-diagram-viewer';
export { SingleLineDiagramViewer } from './components/single-line-diagram-viewer/single-line-diagram-viewer';
export type {
HandleTogglePopoverType,
Expand Down
Loading