91 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-04-24 12:02:32 +02:00
import { Element } from './Element';
2020-04-17 15:43:34 +02:00
import { Edge } from './Edge';
2020-04-24 12:02:32 +02:00
import { Node } from './Node';
2020-04-27 18:57:23 +02:00
import { GraphStyles, NodeStyles } from './Styles';
2020-04-24 17:01:26 +02:00
import { Color } from './Color';
2020-04-17 15:43:34 +02:00
2020-04-27 18:57:23 +02:00
interface NodeOptions {
name: string;
label: string;
style: NodeStyles;
color: Color;
}
2020-04-24 12:02:32 +02:00
export class Graph extends Element {
2020-04-17 15:43:34 +02:00
public children: Array<Graph> = [];
2020-04-24 12:02:32 +02:00
public nodes: Array<Node> = [];
2020-04-17 15:43:34 +02:00
public is_root = false;
2020-04-24 12:02:32 +02:00
public edges: Array<Edge> = [];
2020-04-24 17:01:26 +02:00
public style?: GraphStyles;
public color?: Color;
2020-04-17 15:43:34 +02:00
// eslint-disable-next-line @typescript-eslint/naming-convention
2020-04-27 18:57:23 +02:00
public toString (): string {
2020-04-24 12:21:26 +02:00
const header = this.parent
? `subgraph cluster_${this.full_name}`
: `digraph ${this.full_name}`;
2020-04-24 17:01:26 +02:00
const attributes = [];
if (this.color)
attributes.push ({ name: 'color', value: this.color.toString () });
if (this.style)
attributes.push ({ name: 'style', value: this.style.toString () });
2020-04-27 18:57:23 +02:00
let attrs = `\n${attributes.map ((v) => `${v.name} = ${v.value}`)
.join ('\n')}\n`;
let children = `\n${this.children.map ((c) => c.toString ())
.join ('\n')}\n`;
let nodes = `\n${this.nodes.map ((c) => c.toString ())
.join ('\n')}\n`;
let edges = `\n${this.edges.map ((c) => c.toString ())
.join ('\n')}\n`;
2020-04-24 12:21:26 +02:00
2020-04-27 18:57:23 +02:00
if (attrs === '\n\n')
2020-04-24 17:01:26 +02:00
attrs = '';
2020-04-27 18:57:23 +02:00
if (children === '\n\n')
2020-04-24 12:21:26 +02:00
children = '';
2020-04-27 18:57:23 +02:00
if (nodes === '\n\n')
2020-04-24 12:21:26 +02:00
nodes = '';
2020-04-27 18:57:23 +02:00
if (edges === '\n\n')
2020-04-24 12:21:26 +02:00
edges = '';
2020-04-27 18:57:23 +02:00
const indented = `${attrs}${children}${nodes}${edges}`
.replace (/\n$/u, '')
.replace (/^/gmu, ' ')
.split ('\n')
.map ((v) => v.trimRight ())
.join ('\n');
return `${header} {${indented}\n}`
2020-04-24 12:02:32 +02:00
.replace (/^\s+$/gmu, '');
}
2020-04-27 18:57:23 +02:00
public add_node (constructor: ((n: Node) => void) | string): string {
const node = new Node ('unnamed', this.full_name);
2020-04-24 14:05:58 +02:00
if (typeof constructor === 'string') {
2020-04-27 18:57:23 +02:00
node.name = constructor;
node.label = constructor;
2020-04-24 14:05:58 +02:00
}
2020-04-27 18:57:23 +02:00
else { constructor (node); }
2020-04-24 12:02:32 +02:00
this.nodes.push (node);
2020-04-27 16:03:48 +02:00
return node.full_name;
2020-04-24 12:02:32 +02:00
}
2020-04-27 16:03:48 +02:00
public add_graph (constructor: ((g: Graph) => void) | string): string {
2020-04-24 14:05:58 +02:00
const graph = new Graph ('unnamed', this.full_name);
2020-04-27 18:57:23 +02:00
if (typeof constructor === 'string')
graph.name = constructor;
else
constructor (graph);
2020-04-24 14:05:58 +02:00
this.children.push (graph);
2020-04-27 16:03:48 +02:00
return graph.full_name;
2020-04-24 12:02:32 +02:00
}
public add_edge (origin: string, target: string): void {
2020-04-27 18:57:23 +02:00
this.edges.push (new Edge (origin, target));
2020-04-17 15:43:34 +02:00
}
}