mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-21 00:09:51 +02:00
wip codeclimate refactoring request
This commit is contained in:
@@ -165,9 +165,31 @@ const drawEdge = function (elem, path, relation) {
|
|||||||
svgPath.attr('marker-end', 'url(' + url + '#' + getRelationType(relation.relation.type2) + 'End' + ')')
|
svgPath.attr('marker-end', 'url(' + url + '#' + getRelationType(relation.relation.type2) + 'End' + ')')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const writeLabel = function (x, y, title) {
|
||||||
|
const g = elem.append('g')
|
||||||
|
.attr('class', 'classLabel')
|
||||||
|
const label = g.append('text')
|
||||||
|
.attr('class', 'label')
|
||||||
|
.attr('x', x)
|
||||||
|
.attr('y', y)
|
||||||
|
.attr('fill', 'red')
|
||||||
|
.attr('text-anchor', 'middle')
|
||||||
|
.text(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)
|
||||||
|
}
|
||||||
|
|
||||||
const l = path.points.length
|
const l = path.points.length
|
||||||
// title
|
// title
|
||||||
{
|
if (typeof relation.title !== 'undefined') {
|
||||||
let x, y
|
let x, y
|
||||||
if ((l % 2) !== 0) {
|
if ((l % 2) !== 0) {
|
||||||
const p1 = path.points[Math.floor(l / 2)]
|
const p1 = path.points[Math.floor(l / 2)]
|
||||||
@@ -180,99 +202,38 @@ const drawEdge = function (elem, path, relation) {
|
|||||||
y = p.y
|
y = p.y
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof relation.title !== 'undefined') {
|
writeLabel(x, y, relation.title)
|
||||||
const g = elem.append('g')
|
|
||||||
.attr('class', 'classLabel')
|
|
||||||
const label = g.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalize = function (point, scale) {
|
const normalize = function (point, scale) {
|
||||||
var norm = Math.sqrt(point.x * point.x + point.y * point.y);
|
var norm = Math.sqrt(point.x * point.x + point.y * point.y)
|
||||||
if (norm != 0) { // as3 return 0,0 for a point of zero length
|
if (norm !== 0) { // as3 return 0,0 for a point of zero length
|
||||||
point.x = scale * point.x / norm;
|
point.x = scale * point.x / norm
|
||||||
point.y = scale * point.y / norm;
|
point.y = scale * point.y / norm
|
||||||
}
|
}
|
||||||
return point
|
return point
|
||||||
}
|
}
|
||||||
// relation title 1
|
// Relation title 1
|
||||||
{
|
if (relation.relationTitle1 !== 'none') {
|
||||||
const p = path.points[0]
|
const p = path.points[0]
|
||||||
const nextPoint = path.points[1]
|
const nextPoint = path.points[1]
|
||||||
|
|
||||||
let direction = {x: nextPoint.x - p.x, y: nextPoint.y - p.y}
|
let direction = {x: nextPoint.x - p.x, y: nextPoint.y - p.y}
|
||||||
normalize(direction, 10)
|
normalize(direction, 10)
|
||||||
const offsettedPoint = {x: p.x + direction.x, y: p.y + direction.y }
|
const offsettedPoint = {x: p.x + direction.x, y: p.y + direction.y}
|
||||||
|
|
||||||
if (relation.relationTitle1 !== "none") {
|
writeLabel(offsettedPoint.x, offsettedPoint.y, relation.relationTitle1)
|
||||||
const g = elem.append('g')
|
|
||||||
.attr('class', 'classLabel')
|
|
||||||
const label = g.append('text')
|
|
||||||
.attr('class', 'label')
|
|
||||||
.attr('x', offsettedPoint.x)
|
|
||||||
.attr('y', offsettedPoint.y)
|
|
||||||
.attr('fill', 'red')
|
|
||||||
.attr('text-anchor', 'middle')
|
|
||||||
.text(relation.relationTitle1)
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Relation title 2
|
||||||
// relation title 2
|
if (relation.relationTitle2 !== 'none') {
|
||||||
{
|
|
||||||
const p = path.points[l - 1]
|
const p = path.points[l - 1]
|
||||||
const previousPoint = path.points[l - 2]
|
const previousPoint = path.points[l - 2]
|
||||||
|
|
||||||
let direction = {x: previousPoint.x - p.x, y: previousPoint.y - p.y}
|
let direction = {x: previousPoint.x - p.x, y: previousPoint.y - p.y}
|
||||||
normalize(direction, 5)
|
normalize(direction, 10)
|
||||||
const offsettedPoint = {x: p.x + direction.x, y: p.y + direction.y }
|
const offsettedPoint = {x: p.x + direction.x, y: p.y + direction.y}
|
||||||
|
|
||||||
if (relation.relationTitle2 !== 'none') {
|
writeLabel(offsettedPoint.x, offsettedPoint.y, relation.relationTitle2)
|
||||||
const g = elem.append('g')
|
|
||||||
.attr('class', 'classLabel')
|
|
||||||
const label = g.append('text')
|
|
||||||
.attr('class', 'label')
|
|
||||||
.attr('x', offsettedPoint.x)
|
|
||||||
.attr('y', offsettedPoint.y)
|
|
||||||
.attr('fill', 'red')
|
|
||||||
.attr('text-anchor', 'middle')
|
|
||||||
.text(relation.relationTitle2)
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
edgeCount++
|
edgeCount++
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user