2020-03-11 19:52:57 +01:00
|
|
|
import intersectRect from './intersect/intersect-rect';
|
2021-02-06 15:56:05 +05:30
|
|
|
import { log } from '../logger';
|
2020-03-11 19:52:57 +01:00
|
|
|
import createLabel from './createLabel';
|
2020-05-27 21:47:07 +02:00
|
|
|
import { select } from 'd3';
|
|
|
|
import { getConfig } from '../config';
|
2021-06-03 20:47:24 +02:00
|
|
|
import { evaluate } from '../diagrams/common/common';
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
const rect = (parent, node) => {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.trace('Creating subgraph rect for ', node.id, node);
|
2020-04-11 17:16:01 +02:00
|
|
|
|
2020-03-11 19:52:57 +01:00
|
|
|
// Add outer g element
|
|
|
|
const shapeSvg = parent
|
|
|
|
.insert('g')
|
2020-10-15 17:37:16 +02:00
|
|
|
.attr('class', 'cluster' + (node.class ? ' ' + node.class : ''))
|
2020-03-11 19:52:57 +01:00
|
|
|
.attr('id', node.id);
|
|
|
|
|
|
|
|
// add the rect
|
|
|
|
const rect = shapeSvg.insert('rect', ':first-child');
|
|
|
|
|
|
|
|
// Create the label and insert it after the rect
|
|
|
|
const label = shapeSvg.insert('g').attr('class', 'cluster-label');
|
|
|
|
|
2020-05-27 21:47:07 +02:00
|
|
|
const text = label
|
|
|
|
.node()
|
|
|
|
.appendChild(createLabel(node.labelText, node.labelStyle, undefined, true));
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
// Get the size of the label
|
2020-05-27 21:47:07 +02:00
|
|
|
let bbox = text.getBBox();
|
|
|
|
|
2021-06-03 20:47:24 +02:00
|
|
|
if (evaluate(getConfig().flowchart.htmlLabels)) {
|
2020-05-27 21:47:07 +02:00
|
|
|
const div = text.children[0];
|
|
|
|
const dv = select(text);
|
|
|
|
bbox = div.getBoundingClientRect();
|
|
|
|
dv.attr('width', bbox.width);
|
|
|
|
dv.attr('height', bbox.height);
|
|
|
|
}
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
2021-05-16 10:44:34 +02:00
|
|
|
const width = node.width <= bbox.width + padding ? bbox.width + padding : node.width;
|
2021-05-16 08:21:28 +02:00
|
|
|
if (node.width <= bbox.width + padding) {
|
|
|
|
node.diff = (bbox.width - node.width) / 2;
|
|
|
|
} else {
|
|
|
|
node.diff = -node.padding / 2;
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:56:05 +05:30
|
|
|
log.trace('Data ', node, JSON.stringify(node));
|
2020-03-11 19:52:57 +01:00
|
|
|
// center the rect around its coordinate
|
|
|
|
rect
|
2020-11-17 23:18:11 +01:00
|
|
|
.attr('style', node.style)
|
2020-03-11 19:52:57 +01:00
|
|
|
.attr('rx', node.rx)
|
|
|
|
.attr('ry', node.ry)
|
2021-05-16 08:21:28 +02:00
|
|
|
.attr('x', node.x - width / 2)
|
2020-03-11 19:52:57 +01:00
|
|
|
.attr('y', node.y - node.height / 2 - halfPadding)
|
2021-05-16 08:21:28 +02:00
|
|
|
.attr('width', width)
|
2020-03-11 19:52:57 +01:00
|
|
|
.attr('height', node.height + padding);
|
|
|
|
|
|
|
|
// Center the label
|
2020-03-11 20:25:55 +01:00
|
|
|
label.attr(
|
|
|
|
'transform',
|
|
|
|
'translate(' +
|
|
|
|
(node.x - bbox.width / 2) +
|
|
|
|
', ' +
|
2020-11-17 23:18:11 +01:00
|
|
|
(node.y - node.height / 2 + node.padding / 3) +
|
2020-03-11 20:25:55 +01:00
|
|
|
')'
|
|
|
|
);
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.width = rectBox.width;
|
|
|
|
node.height = rectBox.height;
|
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
node.intersect = function (point) {
|
2020-03-11 19:52:57 +01:00
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
|
|
|
|
2020-03-29 14:20:49 +02:00
|
|
|
/**
|
|
|
|
* Non visiable cluster where the note is group with its
|
|
|
|
*/
|
|
|
|
const noteGroup = (parent, node) => {
|
|
|
|
// Add outer g element
|
2021-07-15 11:35:12 +02:00
|
|
|
const shapeSvg = parent.insert('g').attr('class', 'note-cluster').attr('id', node.id);
|
2020-03-29 14:20:49 +02:00
|
|
|
|
|
|
|
// add the rect
|
|
|
|
const rect = shapeSvg.insert('rect', ':first-child');
|
|
|
|
|
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
|
|
|
// center the rect around its coordinate
|
|
|
|
rect
|
|
|
|
.attr('rx', node.rx)
|
|
|
|
.attr('ry', node.ry)
|
|
|
|
.attr('x', node.x - node.width / 2 - halfPadding)
|
|
|
|
.attr('y', node.y - node.height / 2 - halfPadding)
|
|
|
|
.attr('width', node.width + padding)
|
|
|
|
.attr('height', node.height + padding)
|
|
|
|
.attr('fill', 'none');
|
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.width = rectBox.width;
|
|
|
|
node.height = rectBox.height;
|
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
node.intersect = function (point) {
|
2020-03-29 14:20:49 +02:00
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
2020-03-28 17:34:23 +01:00
|
|
|
const roundedWithTitle = (parent, node) => {
|
|
|
|
// Add outer g element
|
2021-07-15 11:35:12 +02:00
|
|
|
const shapeSvg = parent.insert('g').attr('class', node.classes).attr('id', node.id);
|
2020-03-28 17:34:23 +01:00
|
|
|
|
|
|
|
// add the rect
|
|
|
|
const rect = shapeSvg.insert('rect', ':first-child');
|
|
|
|
|
|
|
|
// Create the label and insert it after the rect
|
|
|
|
const label = shapeSvg.insert('g').attr('class', 'cluster-label');
|
|
|
|
const innerRect = shapeSvg.append('rect');
|
|
|
|
|
2020-05-27 21:47:07 +02:00
|
|
|
const text = label
|
|
|
|
.node()
|
|
|
|
.appendChild(createLabel(node.labelText, node.labelStyle, undefined, true));
|
2020-03-28 17:34:23 +01:00
|
|
|
|
|
|
|
// Get the size of the label
|
2020-05-27 21:47:07 +02:00
|
|
|
let bbox = text.getBBox();
|
2021-06-03 20:47:24 +02:00
|
|
|
if (evaluate(getConfig().flowchart.htmlLabels)) {
|
2020-05-27 21:47:07 +02:00
|
|
|
const div = text.children[0];
|
|
|
|
const dv = select(text);
|
|
|
|
bbox = div.getBoundingClientRect();
|
|
|
|
dv.attr('width', bbox.width);
|
|
|
|
dv.attr('height', bbox.height);
|
|
|
|
}
|
|
|
|
bbox = text.getBBox();
|
2020-03-28 17:34:23 +01:00
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
2021-05-16 10:44:34 +02:00
|
|
|
const width = node.width <= bbox.width + node.padding ? bbox.width + node.padding : node.width;
|
2021-05-15 18:52:06 +02:00
|
|
|
if (node.width <= bbox.width + node.padding) {
|
2021-06-06 11:37:23 +02:00
|
|
|
node.diff = (bbox.width + node.padding * 0 - node.width) / 2;
|
2021-05-17 18:49:53 +02:00
|
|
|
} else {
|
|
|
|
node.diff = -node.padding / 2;
|
2021-05-15 18:52:06 +02:00
|
|
|
}
|
2021-05-02 08:44:28 +02:00
|
|
|
|
2020-03-28 17:34:23 +01:00
|
|
|
// center the rect around its coordinate
|
|
|
|
rect
|
|
|
|
.attr('class', 'outer')
|
2021-05-02 08:44:28 +02:00
|
|
|
.attr('x', node.x - width / 2 - halfPadding)
|
2020-03-28 17:34:23 +01:00
|
|
|
.attr('y', node.y - node.height / 2 - halfPadding)
|
2021-05-02 08:44:28 +02:00
|
|
|
.attr('width', width + padding)
|
2020-03-28 17:34:23 +01:00
|
|
|
.attr('height', node.height + padding);
|
|
|
|
innerRect
|
|
|
|
.attr('class', 'inner')
|
2021-05-02 08:44:28 +02:00
|
|
|
.attr('x', node.x - width / 2 - halfPadding)
|
2020-03-28 17:34:23 +01:00
|
|
|
.attr('y', node.y - node.height / 2 - halfPadding + bbox.height - 1)
|
2021-05-02 08:44:28 +02:00
|
|
|
.attr('width', width + padding)
|
2020-03-28 17:34:23 +01:00
|
|
|
.attr('height', node.height + padding - bbox.height - 3);
|
|
|
|
|
|
|
|
// Center the label
|
|
|
|
label.attr(
|
|
|
|
'transform',
|
|
|
|
'translate(' +
|
|
|
|
(node.x - bbox.width / 2) +
|
|
|
|
', ' +
|
2021-06-03 20:47:24 +02:00
|
|
|
(node.y -
|
|
|
|
node.height / 2 -
|
|
|
|
node.padding / 3 +
|
|
|
|
(evaluate(getConfig().flowchart.htmlLabels) ? 5 : 3)) +
|
2020-03-28 17:34:23 +01:00
|
|
|
')'
|
|
|
|
);
|
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.height = rectBox.height;
|
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
node.intersect = function (point) {
|
2020-03-28 17:34:23 +01:00
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
|
|
|
|
2020-04-22 20:03:41 +02:00
|
|
|
const divider = (parent, node) => {
|
|
|
|
// Add outer g element
|
2021-07-15 11:35:12 +02:00
|
|
|
const shapeSvg = parent.insert('g').attr('class', node.classes).attr('id', node.id);
|
2020-04-22 20:03:41 +02:00
|
|
|
|
|
|
|
// add the rect
|
|
|
|
const rect = shapeSvg.insert('rect', ':first-child');
|
|
|
|
|
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
|
|
|
// center the rect around its coordinate
|
|
|
|
rect
|
|
|
|
.attr('class', 'divider')
|
|
|
|
.attr('x', node.x - node.width / 2 - halfPadding)
|
2020-04-25 14:18:23 +02:00
|
|
|
.attr('y', node.y - node.height / 2)
|
2020-04-22 20:03:41 +02:00
|
|
|
.attr('width', node.width + padding)
|
|
|
|
.attr('height', node.height + padding);
|
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.width = rectBox.width;
|
|
|
|
node.height = rectBox.height;
|
2021-06-06 11:37:23 +02:00
|
|
|
node.diff = -node.padding / 2;
|
2021-07-15 11:35:12 +02:00
|
|
|
node.intersect = function (point) {
|
2020-04-22 20:03:41 +02:00
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
|
|
|
|
|
|
|
const shapes = { rect, roundedWithTitle, noteGroup, divider };
|
2020-03-11 19:52:57 +01:00
|
|
|
|
2020-03-14 17:38:35 +01:00
|
|
|
let clusterElems = {};
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
export const insertCluster = (elem, node) => {
|
2021-02-06 15:56:05 +05:30
|
|
|
log.trace('Inserting cluster');
|
2020-04-11 17:16:01 +02:00
|
|
|
const shape = node.shape || 'rect';
|
|
|
|
clusterElems[node.id] = shapes[shape](elem, node);
|
2020-03-11 19:52:57 +01:00
|
|
|
};
|
|
|
|
export const getClusterTitleWidth = (elem, node) => {
|
2020-05-27 21:47:07 +02:00
|
|
|
const label = createLabel(node.labelText, node.labelStyle, undefined, true);
|
2020-03-11 19:52:57 +01:00
|
|
|
elem.node().appendChild(label);
|
|
|
|
const width = label.getBBox().width;
|
|
|
|
elem.node().removeChild(label);
|
|
|
|
return width;
|
|
|
|
};
|
|
|
|
|
2020-03-14 17:38:35 +01:00
|
|
|
export const clear = () => {
|
|
|
|
clusterElems = {};
|
|
|
|
};
|
|
|
|
|
2021-07-15 11:35:12 +02:00
|
|
|
export const positionCluster = (node) => {
|
2021-05-15 18:52:06 +02:00
|
|
|
log.info('Position cluster (' + node.id + ', ' + node.x + ', ' + node.y + ')');
|
2020-03-11 19:52:57 +01:00
|
|
|
const el = clusterElems[node.id];
|
2020-04-11 17:16:01 +02:00
|
|
|
|
2020-03-11 19:52:57 +01:00
|
|
|
el.attr('transform', 'translate(' + node.x + ', ' + node.y + ')');
|
|
|
|
};
|