nope/resources/ui/graph/helpers/generateNetworkAtElement.tsx

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-29 18:20:42 +00:00
import { UndoRedoGraph } from '../graph';
2020-10-25 20:14:51 +00:00
export function generateNetworkAtElement<N, E>(networkRenderElement:any, options, maxStackSize = 100){
/** Defeine the Network */
const network = new UndoRedoGraph<N,E>(networkRenderElement, options, maxStackSize);
/**
* Define a Function, which reacts to resizing the Elements. This Function
* will resize the Network and adapt its postion.
* @param width width of the Network
* @param height height of the Network
*/
const resize = (width: number, height: number) => {
if (width > 0 && height > 0) {
/**
* Extract the currently used Postion and Scale, to
* reset this after resizing the Network
*/
const position = network.visNetwork.getViewPosition();
const currentScale = network.visNetwork.getScale();
// Update the Network Size
network.visNetwork.setSize(width, height);
// Perform a redraw off the Network
network.visNetwork.redraw();
network.visNetwork.moveTo({
position,
scale: currentScale,
animation: false
});
}
}
/**
* Return the Network and a Resize Function
*/
return {
network,
resize
}
}