mermaid/src/diagrams/class/classRenderer.js

248 lines
6.0 KiB
JavaScript
Raw Normal View History

import { select } from 'd3';
2019-10-21 14:34:04 +05:30
import dagre from 'dagre';
import graphlib from 'graphlib';
2021-02-06 15:56:05 +05:30
import { log } from '../../logger';
import classDb, { lookUpDomId } from './classDb';
import { parser } from './parser/classDiagram';
import svgDraw from './svgDraw';
import { configureSvgSize } from '../../utils';
2017-09-10 19:41:34 +08:00
parser.yy = classDb;
2017-04-11 22:14:25 +08:00
let idCache = {};
2020-04-02 09:50:27 -07:00
const padding = 20;
2017-04-11 22:14:25 +08:00
2017-09-14 20:12:54 +08:00
const conf = {
2017-04-11 22:14:25 +08:00
dividerMargin: 10,
padding: 5,
2017-04-22 22:25:07 +08:00
textHeight: 10
};
2015-10-30 10:47:25 +01:00
// Todo optimize
const getGraphId = function(label) {
const keys = Object.keys(idCache);
2015-11-15 15:06:24 +01:00
2017-09-14 20:12:54 +08:00
for (let i = 0; i < keys.length; i++) {
2017-04-11 22:14:25 +08:00
if (idCache[keys[i]].label === label) {
return keys[i];
2015-10-30 10:47:25 +01:00
}
2017-04-11 22:14:25 +08:00
}
2015-11-15 15:06:24 +01:00
return undefined;
};
2015-10-30 10:47:25 +01:00
/**
* Setup arrow head and define the marker. The result is appended to the svg.
*/
const insertMarkers = function(elem) {
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'extensionStart')
.attr('class', 'extension')
.attr('refX', 0)
.attr('refY', 7)
.attr('markerWidth', 190)
.attr('markerHeight', 240)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 1,7 L18,13 V 1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'extensionEnd')
.attr('refX', 19)
.attr('refY', 7)
.attr('markerWidth', 20)
.attr('markerHeight', 28)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 1,1 V 13 L18,7 Z'); // this is actual shape for arrowhead
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'compositionStart')
.attr('class', 'extension')
.attr('refX', 0)
.attr('refY', 7)
.attr('markerWidth', 190)
.attr('markerHeight', 240)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 18,7 L9,13 L1,7 L9,1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'compositionEnd')
.attr('refX', 19)
.attr('refY', 7)
.attr('markerWidth', 20)
.attr('markerHeight', 28)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 18,7 L9,13 L1,7 L9,1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'aggregationStart')
.attr('class', 'extension')
.attr('refX', 0)
.attr('refY', 7)
.attr('markerWidth', 190)
.attr('markerHeight', 240)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 18,7 L9,13 L1,7 L9,1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'aggregationEnd')
.attr('refX', 19)
.attr('refY', 7)
.attr('markerWidth', 20)
.attr('markerHeight', 28)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 18,7 L9,13 L1,7 L9,1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'dependencyStart')
.attr('class', 'extension')
.attr('refX', 0)
.attr('refY', 7)
.attr('markerWidth', 190)
.attr('markerHeight', 240)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 5,7 L9,13 L1,7 L9,1 Z');
2019-02-12 10:27:05 +02:00
elem
2019-06-14 01:22:46 +03:00
.append('defs')
.append('marker')
.attr('id', 'dependencyEnd')
.attr('refX', 19)
.attr('refY', 7)
.attr('markerWidth', 20)
.attr('markerHeight', 28)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 18,7 L9,13 L14,7 L9,1 Z');
};
2019-06-14 01:22:46 +03:00
export const setConf = function(cnf) {
const keys = Object.keys(cnf);
2019-02-12 10:27:05 +02:00
keys.forEach(function(key) {
conf[key] = cnf[key];
});
};
/**
* Draws a flowchart in the tag with id: id based on the graph definition in text.
* @param text
* @param id
*/
export const draw = function(text, id) {
idCache = {};
parser.yy.clear();
parser.parse(text);
2021-02-06 15:56:05 +05:30
log.info('Rendering diagram ' + text);
2015-10-30 10:47:25 +01:00
// Fetch the default direction, use TD if none was found
const diagram = select(`[id='${id}']`);
diagram.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink');
insertMarkers(diagram);
2017-04-16 23:48:36 +08:00
// Layout graph, Create a new directed graph
2018-03-06 16:04:40 +08:00
const g = new graphlib.Graph({
2017-04-11 22:14:25 +08:00
multigraph: true
});
2015-10-30 10:47:25 +01:00
2017-04-16 23:48:36 +08:00
// Set an object for the graph label
2017-04-11 22:14:25 +08:00
g.setGraph({
isMultiGraph: true
});
2015-10-30 10:47:25 +01:00
2017-04-16 23:48:36 +08:00
// Default to assigning a new object as a label for each new edge.
g.setDefaultEdgeLabel(function() {
return {};
});
2017-04-11 22:14:25 +08:00
const classes = classDb.getClasses();
const keys = Object.keys(classes);
2017-09-14 20:12:54 +08:00
for (let i = 0; i < keys.length; i++) {
const classDef = classes[keys[i]];
const node = svgDraw.drawClass(diagram, classDef, conf);
idCache[node.id] = node;
2017-04-16 23:48:36 +08:00
// Add nodes to the graph. The first argument is the node id. The second is
// metadata about the node. In this case we're going to add labels to each of
// our nodes.
g.setNode(node.id, node);
2021-02-06 15:56:05 +05:30
log.info('Org height: ' + node.height);
2017-04-11 22:14:25 +08:00
}
const relations = classDb.getRelations();
relations.forEach(function(relation) {
2021-02-06 15:56:05 +05:30
log.info(
'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)
);
2020-01-09 16:44:27 +01:00
g.setEdge(
getGraphId(relation.id1),
getGraphId(relation.id2),
{
relation: relation
},
relation.title || 'DEFAULT'
);
});
dagre.layout(g);
g.nodes().forEach(function(v) {
2019-06-14 01:22:46 +03:00
if (typeof v !== 'undefined' && typeof g.node(v) !== 'undefined') {
2021-02-06 15:56:05 +05:30
log.debug('Node ' + v + ': ' + JSON.stringify(g.node(v)));
select('#' + lookUpDomId(v)).attr(
2019-06-14 01:22:46 +03:00
'transform',
'translate(' +
2019-02-12 10:27:05 +02:00
(g.node(v).x - g.node(v).width / 2) +
2019-06-14 01:22:46 +03:00
',' +
2019-02-12 10:27:05 +02:00
(g.node(v).y - g.node(v).height / 2) +
2019-06-14 01:22:46 +03:00
' )'
);
2019-02-12 10:27:05 +02:00
}
});
g.edges().forEach(function(e) {
2019-06-14 01:22:46 +03:00
if (typeof e !== 'undefined' && typeof g.edge(e) !== 'undefined') {
2021-02-06 15:56:05 +05:30
log.debug('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(g.edge(e)));
svgDraw.drawEdge(diagram, g.edge(e), g.edge(e).relation, conf);
2017-04-11 22:14:25 +08:00
}
});
2015-10-30 10:47:25 +01:00
2020-04-02 09:50:27 -07:00
const svgBounds = diagram.node().getBBox();
const width = svgBounds.width + padding * 2;
const height = svgBounds.height + padding * 2;
configureSvgSize(diagram, height, width, conf.useMaxWidth);
// Ensure the viewBox includes the whole svgBounds area with extra space for padding
2020-04-02 09:50:27 -07:00
const vBox = `${svgBounds.x - padding} ${svgBounds.y - padding} ${width} ${height}`;
2021-02-06 15:56:05 +05:30
log.debug(`viewBox ${vBox}`);
2020-04-02 09:50:27 -07:00
diagram.attr('viewBox', vBox);
};
2017-09-10 22:03:10 +08:00
export default {
setConf,
draw
};