mermaid/src/diagrams/classDiagram/classRenderer.js

366 lines
9.9 KiB
JavaScript
Raw Normal View History

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