nope/resources/ui/graph/addons/cluster.selected.ts
2020-10-29 19:20:42 +01:00

85 lines
2.6 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-03-12 11:56:55
* @modify date 2020-10-29 12:07:07
* @desc [description]
*/
import { IBaseEdgeOptions } from '../interfaces/IBaseEdgeOptions';
import { IBaseNodeOptions } from '../interfaces/IBaseNodeOptions';
import { IUndoRedoGraph } from '../interfaces/IGraph';
import { ITemplate } from '../interfaces/ITemplate';
/**
*
* @param component Component, which will be used
* @param selectFirstForTemplate
*/
/**
*
* @param component Component containing the Network and the openPromt Function
* @param selectFirstForTemplate Flag, which will enabling, to look for Node
* @param clusterEdgeProperties Properties for the Edges, that will be added
*/
export function clusterSelected(network: IUndoRedoGraph<any,any>, selectFirstForTemplate = true, clusterEdgeProperties = {
color: 'red',
dashes: true,
label: 'cluster',
arrows: {
to: {enabled: false},
middle: {enabled: false},
from: {enabled: false}
},
}){
const nodes = network.network.getSelectedNodes();
let added = false;
/**
* Function to Clusterize the Elements
* @param clusterNodeProperties Cluster Properties for the Node
*/
const cluster = (template: ITemplate<IBaseNodeOptions,IBaseEdgeOptions> = undefined) => {
network.network.cluster({
joinCondition(nodeOptions) {
return nodes.includes(nodeOptions.id);
},
clusterNodeProperties: template.nodes[0],
clusterEdgeProperties
});
/** Save the Network */
network.save();
added = true;
}
/** Based on the Selection, try to */
if (selectFirstForTemplate && nodes.length > 0){
/** Extract the Node */
const node = network.data.nodes.get(nodes[0]);
/** Only if a Template is proveded, generate the node */
if (node.clusterTemplate && typeof node.clusterTemplate === 'function'){
cluster(node.clusterTemplate(node));
} else if (node.clusterTemplate && typeof node.clusterTemplate === 'object'){
cluster(node.clusterTemplate);
}
added = selectFirstForTemplate ? true: added;
}
/** Only if the Element hasnt been added, open up the Prompt */
if (!added){
// openEditInterface<ClusterEditorComponent<IBaseNodeOptions,IBaseEdgeOptions>>(
// ClusterEditorComponent as any,
// {},
// 'Change Cluster-Settings',
// (data) => {
// cluster(data.template);
// },
// );
}
}