mermaid/src/diagrams/class/classRenderer.js

445 lines
11 KiB
JavaScript
Raw Normal View History

2019-06-14 08:01:51 +03:00
import * as d3 from 'd3'
import dagre from 'dagre-layout'
import graphlib from 'graphlibrary'
import { logger } from '../../logger'
import classDb from './classDb'
import { parser } from './parser/classDiagram'
2017-09-10 19:41:34 +08:00
2019-06-14 01:22:46 +03:00
parser.yy = classDb
2017-04-11 22:14:25 +08:00
2019-06-14 01:22:46 +03:00
const idCache = {}
2017-04-11 22:14:25 +08:00
2019-06-14 01:22:46 +03:00
let classCnt = 0
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-06-14 01:22:46 +03:00
}
2015-10-30 10:47:25 +01:00
// Todo optimize
2019-06-14 01:22:46 +03: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-06-14 01:22:46 +03: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-06-14 01:22:46 +03: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.
*/
2019-06-14 01:22:46 +03: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')
.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')
}
let edgeCount = 0
let total = 0
const drawEdge = function (elem, path, relation) {
const getRelationType = function (type) {
2017-04-11 22:14:25 +08:00
switch (type) {
2017-09-10 21:23:04 +08:00
case classDb.relationType.AGGREGATION:
2019-06-14 01:22:46 +03:00
return 'aggregation'
2017-09-10 21:23:04 +08:00
case classDb.relationType.EXTENSION:
2019-06-14 01:22:46 +03:00
return 'extension'
2017-09-10 21:23:04 +08:00
case classDb.relationType.COMPOSITION:
2019-06-14 01:22:46 +03:00
return 'composition'
2017-09-10 21:23:04 +08:00
case classDb.relationType.DEPENDENCY:
2019-06-14 01:22:46 +03:00
return 'dependency'
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
}
2019-02-12 10:27:05 +02:00
2019-06-14 07:37:16 +03:00
path.points = path.points.filter(p => !Number.isNaN(p.y))
2017-04-11 22:14:25 +08:00
2017-04-16 23:48:36 +08:00
// The data for our line
2019-06-14 01:22:46 +03:00
const lineData = path.points
2017-04-11 22:14:25 +08:00
2017-04-16 23:48:36 +08:00
// This is the accessor function we talked about above
2019-02-12 10:27:05 +02:00
const lineFunction = d3
.line()
2019-06-14 01:22:46 +03:00
.x(function (d) {
return d.x
2017-04-16 23:48:36 +08:00
})
2019-06-14 01:22:46 +03:00
.y(function (d) {
return d.y
2017-04-16 23:48:36 +08:00
})
2019-06-14 01:22:46 +03:00
.curve(d3.curveBasis)
2019-02-12 10:27:05 +02:00
const svgPath = elem
2019-06-14 01:22:46 +03:00
.append('path')
.attr('d', lineFunction(lineData))
.attr('id', 'edge' + edgeCount)
.attr('class', 'relation')
let url = ''
2017-04-11 22:14:25 +08:00
if (conf.arrowMarkerAbsolute) {
2019-02-12 10:27:05 +02:00
url =
window.location.protocol +
2019-06-14 01:22:46 +03:00
'//' +
2019-02-12 10:27:05 +02:00
window.location.host +
window.location.pathname +
2019-06-14 01:22:46 +03:00
window.location.search
url = url.replace(/\(/g, '\\(')
url = url.replace(/\)/g, '\\)')
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
if (relation.relation.type1 !== 'none') {
2019-02-12 10:27:05 +02:00
svgPath.attr(
2019-06-14 01:22:46 +03:00
'marker-start',
'url(' +
2019-02-12 10:27:05 +02:00
url +
2019-06-14 01:22:46 +03:00
'#' +
2019-02-12 10:27:05 +02:00
getRelationType(relation.relation.type1) +
2019-06-14 01:22:46 +03:00
'Start' +
')'
)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
if (relation.relation.type2 !== 'none') {
2019-02-12 10:27:05 +02:00
svgPath.attr(
2019-06-14 01:22:46 +03:00
'marker-end',
'url(' +
2019-02-12 10:27:05 +02:00
url +
2019-06-14 01:22:46 +03:00
'#' +
2019-02-12 10:27:05 +02:00
getRelationType(relation.relation.type2) +
2019-06-14 01:22:46 +03:00
'End' +
')'
)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
let x, y
const l = path.points.length
2019-02-12 10:27:05 +02:00
if (l % 2 !== 0 && l > 1) {
2019-06-14 01:22:46 +03:00
const p1 = path.points[Math.floor(l / 2)]
const p2 = path.points[Math.ceil(l / 2)]
x = (p1.x + p2.x) / 2
y = (p1.y + p2.y) / 2
2017-04-11 22:14:25 +08:00
} else {
2019-06-14 01:22:46 +03:00
const p = path.points[Math.floor(l / 2)]
x = p.x
y = p.y
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
if (typeof relation.title !== 'undefined') {
const g = elem.append('g').attr('class', 'classLabel')
2019-02-12 10:27:05 +02:00
const label = g
2019-06-14 01:22:46 +03:00
.append('text')
.attr('class', 'label')
.attr('x', x)
.attr('y', y)
.attr('fill', 'red')
.attr('text-anchor', 'middle')
.text(relation.title)
window.label = label
const bounds = label.node().getBBox()
g.insert('rect', ':first-child')
.attr('class', 'box')
.attr('x', bounds.x - conf.padding / 2)
.attr('y', bounds.y - conf.padding / 2)
.attr('width', bounds.width + conf.padding)
.attr('height', bounds.height + conf.padding)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
edgeCount++
}
2019-06-14 01:22:46 +03:00
const drawClass = function (elem, classDef) {
logger.info('Rendering class ' + classDef)
2019-06-14 01:22:46 +03:00
const addTspan = function (textEl, txt, isFirst) {
2019-02-12 10:27:05 +02:00
const tSpan = textEl
2019-06-14 01:22:46 +03:00
.append('tspan')
.attr('x', conf.padding)
.text(txt)
2017-04-11 22:14:25 +08:00
if (!isFirst) {
2019-06-14 01:22:46 +03:00
tSpan.attr('dy', conf.textHeight)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
}
2015-10-30 10:47:25 +01:00
2019-06-14 01:22:46 +03:00
const id = 'classId' + (classCnt % total)
2017-09-14 20:12:54 +08:00
const classInfo = {
2017-04-11 22:14:25 +08:00
id: id,
label: classDef.id,
width: 0,
height: 0
2019-06-14 01:22:46 +03:00
}
2019-02-12 10:27:05 +02:00
const g = elem
2019-06-14 01:22:46 +03:00
.append('g')
.attr('id', id)
.attr('class', 'classGroup')
2019-02-12 10:27:05 +02:00
const title = g
2019-06-14 01:22:46 +03:00
.append('text')
.attr('x', conf.padding)
.attr('y', conf.textHeight + conf.padding)
.text(classDef.id)
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
const titleHeight = title.node().getBBox().height
2019-02-12 10:27:05 +02:00
const membersLine = g
2019-06-14 01:22:46 +03:00
.append('line') // text label for the x axis
.attr('x1', 0)
.attr('y1', conf.padding + titleHeight + conf.dividerMargin / 2)
.attr('y2', conf.padding + titleHeight + conf.dividerMargin / 2)
2019-02-12 10:27:05 +02:00
const members = g
2019-06-14 01:22:46 +03:00
.append('text') // text label for the x axis
.attr('x', conf.padding)
.attr('y', titleHeight + conf.dividerMargin + conf.textHeight)
.attr('fill', 'white')
.attr('class', 'classText')
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
let isFirst = true
classDef.members.forEach(function (member) {
addTspan(members, member, isFirst)
isFirst = false
})
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
const membersBox = members.node().getBBox()
2019-02-12 10:27:05 +02:00
const methodsLine = g
2019-06-14 01:22:46 +03:00
.append('line') // text label for the x axis
.attr('x1', 0)
2019-02-12 10:27:05 +02:00
.attr(
2019-06-14 01:22:46 +03:00
'y1',
2019-02-12 10:27:05 +02:00
conf.padding + titleHeight + conf.dividerMargin + membersBox.height
)
.attr(
2019-06-14 01:22:46 +03:00
'y2',
2019-02-12 10:27:05 +02:00
conf.padding + titleHeight + conf.dividerMargin + membersBox.height
2019-06-14 01:22:46 +03:00
)
2019-02-12 10:27:05 +02:00
const methods = g
2019-06-14 01:22:46 +03:00
.append('text') // text label for the x axis
.attr('x', conf.padding)
2019-02-12 10:27:05 +02:00
.attr(
2019-06-14 01:22:46 +03:00
'y',
2019-02-12 10:27:05 +02:00
titleHeight + 2 * conf.dividerMargin + membersBox.height + conf.textHeight
)
2019-06-14 01:22:46 +03:00
.attr('fill', 'white')
.attr('class', 'classText')
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
isFirst = true
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
classDef.methods.forEach(function (method) {
addTspan(methods, method, isFirst)
isFirst = false
})
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
const classBox = g.node().getBBox()
g.insert('rect', ':first-child')
.attr('x', 0)
.attr('y', 0)
.attr('width', classBox.width + 2 * conf.padding)
.attr('height', classBox.height + conf.padding + 0.5 * conf.dividerMargin)
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
membersLine.attr('x2', classBox.width + 2 * conf.padding)
methodsLine.attr('x2', classBox.width + 2 * conf.padding)
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
classInfo.width = classBox.width + 2 * conf.padding
classInfo.height = classBox.height + conf.padding + 0.5 * conf.dividerMargin
2019-02-12 10:27:05 +02:00
2019-06-14 01:22:46 +03:00
idCache[id] = classInfo
classCnt++
return classInfo
}
2019-02-12 10:27:05 +02:00
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
2019-06-14 01:22:46 +03: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
*/
2019-06-14 01:22:46 +03:00
export const draw = function (text, id) {
parser.yy.clear()
parser.parse(text)
2019-06-14 01:22:46 +03:00
logger.info('Rendering diagram ' + text)
2015-10-30 10:47:25 +01:00
2017-04-16 23:48:36 +08:00
/// / Fetch the default direction, use TD if none was found
2019-06-14 01:22:46 +03:00
const diagram = d3.select(`[id='${id}']`)
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
2019-06-14 01:22:46 +03: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-06-14 01:22:46 +03: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-06-14 01:22:46 +03:00
g.setDefaultEdgeLabel(function () {
return {}
})
2017-04-11 22:14:25 +08:00
2019-06-14 01:22:46 +03:00
const classes = classDb.getClasses()
const keys = Object.keys(classes)
total = keys.length
2017-09-14 20:12:54 +08:00
for (let i = 0; i < keys.length; i++) {
2019-06-14 01:22:46 +03:00
const classDef = classes[keys[i]]
const node = drawClass(diagram, classDef)
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-06-14 01:22:46 +03:00
g.setNode(node.id, node)
logger.info('Org height: ' + node.height)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
const relations = classDb.getRelations()
relations.forEach(function (relation) {
2019-02-12 10:27:05 +02:00
logger.info(
2019-06-14 01:22:46 +03:00
'tjoho' +
2019-02-12 10:27:05 +02:00
getGraphId(relation.id1) +
getGraphId(relation.id2) +
JSON.stringify(relation)
2019-06-14 01:22:46 +03:00
)
2019-02-12 10:27:05 +02:00
g.setEdge(getGraphId(relation.id1), getGraphId(relation.id2), {
relation: relation
2019-06-14 01:22:46 +03:00
})
})
dagre.layout(g)
g.nodes().forEach(function (v) {
if (typeof v !== 'undefined' && typeof g.node(v) !== 'undefined') {
logger.debug('Node ' + v + ': ' + JSON.stringify(g.node(v)))
d3.select('#' + v).attr(
'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
}
2019-06-14 01:22:46 +03:00
})
g.edges().forEach(function (e) {
if (typeof e !== 'undefined' && typeof g.edge(e) !== 'undefined') {
2019-02-12 10:27:05 +02:00
logger.debug(
2019-06-14 01:22:46 +03:00
'Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(g.edge(e))
)
drawEdge(diagram, g.edge(e), g.edge(e).relation)
2017-04-11 22:14:25 +08:00
}
2019-06-14 01:22:46 +03:00
})
2015-10-30 10:47:25 +01:00
2019-06-14 01:22:46 +03:00
diagram.attr('height', '100%')
diagram.attr('width', '100%')
2019-02-12 10:27:05 +02:00
diagram.attr(
2019-06-14 01:22:46 +03:00
'viewBox',
'0 0 ' + (g.graph().width + 20) + ' ' + (g.graph().height + 20)
)
}
2017-09-10 22:03:10 +08:00
export default {
setConf,
draw
2019-06-14 01:22:46 +03:00
}