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

New: Add removal functions #96

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 45 additions & 9 deletions src/models/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { IGraphStyle } from './style';
import { ImageHandler } from '../services/images';
import { getEdgeOffsets } from './topology';
import { IEntityState, EntityState } from '../utils/entity.utils';
import { dedupArrays } from '../utils/array.utils';

export interface IGraphData<N extends INodeBase, E extends IEdgeBase> {
nodes: N[];
edges: E[];
}

export interface IGraphObjectsIds {
nodeIds: any[];
edgeIds: any[];
}

export type IEdgeFilter<N extends INodeBase, E extends IEdgeBase> = (edge: IEdge<N, E>) => boolean;

export type INodeFilter<N extends INodeBase, E extends IEdgeBase> = (node: INode<N, E>) => boolean;
Expand All @@ -33,7 +39,10 @@ export interface IGraph<N extends INodeBase, E extends IEdgeBase> {
setup(data: Partial<IGraphData<N, E>>): void;
clearPositions(): void;
merge(data: Partial<IGraphData<N, E>>): void;
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>): void;
remove(data: Partial<IGraphObjectsIds>): void;
removeAll(): void;
removeAllNodes(): void;
removeAllEdges(): void;
isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean;
getBoundingBox(): IRectangle;
getNearestNode(point: IPosition): INode<N, E> | undefined;
Expand All @@ -47,7 +56,7 @@ export interface IGraphSettings<N extends INodeBase, E extends IEdgeBase> {
onLoadedImages?: () => void;
onSetupData?: (data: Partial<IGraphData<N, E>>) => void;
onMergeData?: (data: Partial<IGraphData<N, E>>) => void;
onRemoveData?: (data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) => void;
onRemoveData?: (data: Partial<IGraphObjectsIds>) => void;
}

export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N, E> {
Expand Down Expand Up @@ -263,18 +272,41 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._settings?.onMergeData?.(data);
}

// TODO(dlozic): Add delete all mechanic.
remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) {
remove(data: Partial<IGraphObjectsIds>) {
const nodeIds = data.nodeIds ?? [];
const edgeIds = data.edgeIds ?? [];

this._removeNodes(nodeIds);
this._removeEdges(edgeIds);
const removedNodesData = this._removeNodes(nodeIds);
const removedEdgesData = this._removeEdges(edgeIds);

this._applyEdgeOffsets();
this._applyStyle();

this._settings?.onRemoveData?.(data);
if (this._settings && this._settings.onRemoveData) {
const removedData: IGraphObjectsIds = {
nodeIds: dedupArrays(removedNodesData.nodeIds, removedEdgesData.nodeIds),
edgeIds: dedupArrays(removedNodesData.edgeIds, removedEdgesData.edgeIds),
};

this._settings.onRemoveData(removedData);
}
}

removeAll() {
const nodeIds = this._nodes.getAll().map((node) => node.id);
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ nodeIds, edgeIds });
}

removeAllEdges() {
const edgeIds = this._edges.getAll().map((edge) => edge.id);

this.remove({ edgeIds });
}

removeAllNodes() {
this.removeAll();
}

isEqual<T extends INodeBase, K extends IEdgeBase>(graph: Graph<T, K>): boolean {
Expand Down Expand Up @@ -474,7 +506,7 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
this._edges.removeMany(removedEdgeIds);
}

private _removeNodes(nodeIds: any[]) {
private _removeNodes(nodeIds: any[]): IGraphObjectsIds {
const removedNodeIds: any[] = [];
const removedEdgeIds: any[] = [];

Expand All @@ -496,9 +528,11 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
}
this._nodes.removeMany(removedNodeIds);
this._edges.removeMany(removedEdgeIds);

return { nodeIds: removedNodeIds, edgeIds: removedEdgeIds };
}

private _removeEdges(edgeIds: any[]) {
private _removeEdges(edgeIds: any[]): IGraphObjectsIds {
const removedEdgeIds: any[] = [];

for (let i = 0; i < edgeIds.length; i++) {
Expand All @@ -512,6 +546,8 @@ export class Graph<N extends INodeBase, E extends IEdgeBase> implements IGraph<N
removedEdgeIds.push(edge.id);
}
this._edges.removeMany(removedEdgeIds);

return { nodeIds: [], edgeIds: removedEdgeIds };
}

private _applyEdgeOffsets() {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/array.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const copyArray = <T>(array: Array<T>): Array<T> => {
}
return newArray;
};

export const dedupArrays = <T>(...arrays: T[][]): T[] => {
const combinedArray = arrays.reduce((acc, curr) => acc.concat(curr), []);
return Array.from(new Set(combinedArray));
};
Loading