2020-05-11 07:10:04 +01:00
|
|
|
import { select } from 'd3';
|
2019-10-21 14:34:04 +05:30
|
|
|
import dagre from 'dagre';
|
|
|
|
import graphlib from 'graphlib';
|
2019-09-12 12:58:32 -07:00
|
|
|
import { logger } from '../../logger';
|
2020-01-08 20:48:57 +01:00
|
|
|
import classDb, { lookUpDomId } from './classDb';
|
2019-09-12 12:58:32 -07:00
|
|
|
import { parser } from './parser/classDiagram';
|
2020-02-03 16:04:54 -08:00
|
|
|
import svgDraw from './svgDraw';
|
2017-09-10 19:41:34 +08:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
parser.yy = classDb;
|
2017-04-11 22:14:25 +08:00
|
|
|
|
2019-11-01 13:38:23 +09: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
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2015-10-28 08:12:47 +01:00
|
|
|
|
2015-10-30 10:47:25 +01:00
|
|
|
// Todo optimize
|
2019-09-12 12:58:32 -07:00
|
|
|
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) {
|
2019-09-12 12:58:32 -07:00
|
|
|
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
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
return undefined;
|
|
|
|
};
|
2015-10-30 10:47:25 +01:00
|
|
|
|
2015-11-04 20:38:14 +01:00
|
|
|
/**
|
|
|
|
* Setup arrow head and define the marker. The result is appended to the svg.
|
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.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')
|
2019-09-12 12:58:32 -07:00
|
|
|
.attr('d', 'M 18,7 L9,13 L14,7 L9,1 Z');
|
|
|
|
};
|
2019-06-14 01:22:46 +03:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
export const setConf = function(cnf) {
|
|
|
|
const keys = Object.keys(cnf);
|
2019-02-12 10:27:05 +02:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
keys.forEach(function(key) {
|
|
|
|
conf[key] = cnf[key];
|
|
|
|
});
|
|
|
|
};
|
2019-12-30 17:24:19 -08:00
|
|
|
|
2015-10-28 08:12:47 +01:00
|
|
|
/**
|
|
|
|
* Draws a flowchart in the tag with id: id based on the graph definition in text.
|
|
|
|
* @param text
|
|
|
|
* @param id
|
|
|
|
*/
|
2019-09-12 12:58:32 -07:00
|
|
|
export const draw = function(text, id) {
|
2019-11-01 13:38:23 +09:00
|
|
|
idCache = {};
|
2019-09-12 12:58:32 -07:00
|
|
|
parser.yy.clear();
|
|
|
|
parser.parse(text);
|
2015-10-29 07:49:08 +01:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
logger.info('Rendering diagram ' + text);
|
2015-10-30 10:47:25 +01:00
|
|
|
|
2020-02-03 16:04:54 -08:00
|
|
|
// Fetch the default direction, use TD if none was found
|
2020-05-11 07:10:04 +01:00
|
|
|
const diagram = select(`[id='${id}']`);
|
2019-09-12 12:58:32 -07:00
|
|
|
insertMarkers(diagram);
|
2015-10-29 07:49:08 +01:00
|
|
|
|
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
|
2019-09-12 12:58:32 -07:00
|
|
|
});
|
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
|
2019-09-12 12:58:32 -07:00
|
|
|
});
|
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.
|
2019-09-12 12:58:32 -07:00
|
|
|
g.setDefaultEdgeLabel(function() {
|
|
|
|
return {};
|
|
|
|
});
|
2017-04-11 22:14:25 +08:00
|
|
|
|
2019-09-12 12:58:32 -07: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++) {
|
2019-09-12 12:58:32 -07:00
|
|
|
const classDef = classes[keys[i]];
|
2020-02-03 16:04:54 -08:00
|
|
|
const node = svgDraw.drawClass(diagram, classDef, conf);
|
|
|
|
idCache[node.id] = node;
|
2019-12-30 17:24:19 -08:00
|
|
|
|
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.
|
2019-09-12 12:58:32 -07:00
|
|
|
g.setNode(node.id, node);
|
2019-12-30 17:24:19 -08:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
logger.info('Org height: ' + node.height);
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
const relations = classDb.getRelations();
|
|
|
|
relations.forEach(function(relation) {
|
2019-02-12 10:27:05 +02:00
|
|
|
logger.info(
|
2019-09-12 12:58:32 -07:00
|
|
|
'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'
|
|
|
|
);
|
2019-09-12 12:58:32 -07:00
|
|
|
});
|
2020-02-03 16:04:54 -08:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
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') {
|
2019-09-12 12:58:32 -07:00
|
|
|
logger.debug('Node ' + v + ': ' + JSON.stringify(g.node(v)));
|
2020-05-11 07:10:04 +01:00
|
|
|
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-09-12 12:58:32 -07:00
|
|
|
);
|
2019-02-12 10:27:05 +02:00
|
|
|
}
|
2019-09-12 12:58:32 -07:00
|
|
|
});
|
2020-02-03 16:04:54 -08:00
|
|
|
|
2019-09-12 12:58:32 -07:00
|
|
|
g.edges().forEach(function(e) {
|
2019-06-14 01:22:46 +03:00
|
|
|
if (typeof e !== 'undefined' && typeof g.edge(e) !== 'undefined') {
|
2019-09-12 12:58:32 -07:00
|
|
|
logger.debug('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(g.edge(e)));
|
2020-02-03 16:04:54 -08:00
|
|
|
svgDraw.drawEdge(diagram, g.edge(e), g.edge(e).relation, conf);
|
2017-04-11 22:14:25 +08:00
|
|
|
}
|
2019-09-12 12:58:32 -07: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;
|
|
|
|
|
2020-04-26 06:10:18 -07:00
|
|
|
if (conf.useMaxWidth) {
|
|
|
|
diagram.attr('width', '100%');
|
|
|
|
diagram.attr('style', `max-width: ${width}px;`);
|
|
|
|
} else {
|
|
|
|
diagram.attr('height', height);
|
|
|
|
diagram.attr('width', width);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}`;
|
2020-04-26 06:10:18 -07:00
|
|
|
logger.debug(`viewBox ${vBox}`);
|
2020-04-02 09:50:27 -07:00
|
|
|
diagram.attr('viewBox', vBox);
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|
2017-09-10 22:03:10 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
setConf,
|
|
|
|
draw
|
2019-09-12 12:58:32 -07:00
|
|
|
};
|