nope/resources/ui/graph/defaults/default.add-edge-method.ts
2020-10-25 21:14:51 +01:00

56 lines
2.4 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2019-05-10 13:02:31
* @modify date 2020-07-18 07:07:16
* @desc [description]
*/
import { v4 as generateID } from 'uuid';
import { IGraphToolComponent } from '../interfaces/IGraphTool';
import { IBaseNodeOptions } from '../../../@zema/ZISS-Network/type/IBaseNodeOptions';
import { IBaseEdgeOptions } from '../../../@zema/ZISS-Network/type/IBaseEdgeOptions';
import { IMinProvidedDataSet } from '../../gui-components-basic-layout/types/interfaces';
/**
* Function, which will generate the Default Add Edge Callback
* @param network The Network
* @param customAddCallback A Custom Callback, which could be provided
*/
export function generateAddFunction<N extends IBaseNodeOptions, E extends IBaseEdgeOptions, D extends IMinProvidedDataSet, C extends IGraphToolComponent<N, E, D>>(component: C, customAddCallback?: (edgeData: IBaseEdgeOptions, callback: (edge: IBaseEdgeOptions) => void) => void, allowSelfConnection = true, allowDoubleConnections = true){
return (edgeData: IBaseEdgeOptions, callback: (edge: IBaseEdgeOptions) => void) => {
/** If not Connected to itself everything should be fine */
if (allowSelfConnection || edgeData.from !== edgeData.to) {
let addEdge = true;
/**
* Search for Edges, whether they already have been
* added.
*/
component.network.data.edges.forEach((edge) => {
if (edge.from === edgeData.from && edge.to === edgeData.to) {
addEdge = false;
}
});
/** If the Edge can be added ==> Add it */
if (allowDoubleConnections || addEdge) {
/** Make shure, a new ID is provided */
edgeData.id = generateID();
/** Call the custom callback (if provided), otherwise just add it. */
if (typeof customAddCallback === 'function'){
customAddCallback(edgeData, callback)
} else {
callback(edgeData);
}
} else if (!allowDoubleConnections) {
component.network.showMessage('warning','Double Connections not allowed','You can not connect these elements again',5000);
}
} else {
component.network.showMessage('warning','Self-Connections forbidden','You can not connect elements to itself',5000);
}
};
}