78 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-05-17 17:17:39 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of graphviz-builder which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
2020-05-06 20:24:37 +02:00
import { NodeStyles } from '../enums/Styles';
2023-03-25 14:12:10 +01:00
import { NodeShapes } from '../enums/Shapes';
2020-04-24 12:02:32 +02:00
import { Element } from './Element';
2020-04-24 17:01:26 +02:00
import { Color } from './Color';
2020-04-24 12:02:32 +02:00
export class Node extends Element {
public label?: string;
public is_table = false;
public table_contents?: Array<Array<string>>;
2020-04-24 17:01:26 +02:00
public style?: NodeStyles;
public color?: Color;
2023-03-25 14:12:10 +01:00
public shape?: NodeShapes;
2020-04-24 12:02:32 +02:00
2020-04-27 18:57:23 +02:00
public constructor (name: string, parent: string, label?: string) {
2020-04-24 12:02:32 +02:00
super (name, parent);
this.label = label;
}
private get serialized_table (): string {
if (typeof this.table_contents === 'undefined')
throw new Error ('table contents are undefined');
const mapped_columns = this.table_contents
.map ((val) => `<td>${val.join ('</td><td>')}</td>`);
2020-04-28 11:26:21 +02:00
2020-04-24 12:02:32 +02:00
return `<table>\n <tr>${
mapped_columns.join ('</tr>\n <tr>')
}</tr>\n</table>`;
2020-04-17 15:43:34 +02:00
}
// eslint-disable-next-line @typescript-eslint/naming-convention
public toString (): string {
2020-04-25 17:34:05 +02:00
const attributes = [];
2020-04-28 11:26:21 +02:00
2020-04-27 18:57:23 +02:00
if (this.label || this.is_table) {
attributes.push ({
name: 'label',
value: this.is_table
? this.serialized_table
: this.label
});
}
2020-04-25 17:34:05 +02:00
if (this.style)
attributes.push ({ name: 'style', value: this.style.toString () });
2023-03-25 14:12:10 +01:00
if (this.shape)
attributes.push ({ name: 'shape', value: this.shape.toString () });
2020-04-25 17:34:05 +02:00
if (this.color)
attributes.push ({ name: 'color', value: this.color.toString () });
2020-04-27 18:57:23 +02:00
const attrs = attributes.map ((v) => {
const d = (/\n/u).test (v.value as string)
? [
'<',
'>'
]
: [
'"',
'"'
];
2020-04-28 11:26:21 +02:00
2020-04-27 18:57:23 +02:00
return `${v.name}=${d[0]}${v.value}${d[1]}`;
})
.join (', ');
if (attributes.length > 0)
return `${this.full_name} [${attrs}]`;
2020-04-25 17:34:05 +02:00
2020-04-24 12:02:32 +02:00
return `${this.full_name}`;
2020-04-17 15:43:34 +02:00
}
}