2020-03-11 19:52:57 +01:00
|
|
|
import intersectRect from './intersect/intersect-rect';
|
2020-04-11 17:16:01 +02:00
|
|
|
import { logger as log } from '../logger'; // eslint-disable-line
|
2020-03-11 19:52:57 +01:00
|
|
|
import createLabel from './createLabel';
|
|
|
|
|
|
|
|
const rect = (parent, node) => {
|
2020-04-16 19:44:11 +02:00
|
|
|
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')
|
|
|
|
.attr('class', 'cluster')
|
|
|
|
.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');
|
|
|
|
|
|
|
|
const text = label.node().appendChild(createLabel(node.labelText, node.labelStyle));
|
|
|
|
|
|
|
|
// Get the size of the label
|
|
|
|
const bbox = text.getBBox();
|
|
|
|
|
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
2020-04-16 19:44:11 +02:00
|
|
|
log.trace('Data ', node, JSON.stringify(node));
|
2020-03-11 19:52:57 +01:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Center the label
|
2020-03-11 20:25:55 +01:00
|
|
|
label.attr(
|
|
|
|
'transform',
|
|
|
|
'translate(' +
|
|
|
|
(node.x - bbox.width / 2) +
|
|
|
|
', ' +
|
|
|
|
(node.y - node.height / 2 - node.padding / 3 + 3) +
|
|
|
|
')'
|
|
|
|
);
|
2020-03-11 19:52:57 +01:00
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.width = rectBox.width;
|
|
|
|
node.height = rectBox.height;
|
|
|
|
|
|
|
|
node.intersect = function(point) {
|
|
|
|
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
|
|
|
|
const shapeSvg = parent
|
|
|
|
.insert('g')
|
|
|
|
.attr('class', 'note-cluster')
|
|
|
|
.attr('id', node.id);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
node.intersect = function(point) {
|
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
2020-03-28 17:34:23 +01:00
|
|
|
const roundedWithTitle = (parent, node) => {
|
|
|
|
// Add outer g element
|
|
|
|
const shapeSvg = parent
|
|
|
|
.insert('g')
|
2020-03-29 14:20:49 +02:00
|
|
|
.attr('class', node.classes)
|
2020-03-28 17:34:23 +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');
|
|
|
|
const innerRect = shapeSvg.append('rect');
|
|
|
|
|
|
|
|
const text = label.node().appendChild(createLabel(node.labelText, node.labelStyle));
|
|
|
|
|
|
|
|
// Get the size of the label
|
|
|
|
const bbox = text.getBBox();
|
|
|
|
|
|
|
|
const padding = 0 * node.padding;
|
|
|
|
const halfPadding = padding / 2;
|
|
|
|
|
|
|
|
// center the rect around its coordinate
|
|
|
|
rect
|
|
|
|
.attr('class', 'outer')
|
|
|
|
.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);
|
|
|
|
innerRect
|
|
|
|
.attr('class', 'inner')
|
|
|
|
.attr('x', node.x - node.width / 2 - halfPadding)
|
|
|
|
.attr('y', node.y - node.height / 2 - halfPadding + bbox.height - 1)
|
|
|
|
.attr('width', node.width + padding)
|
|
|
|
.attr('height', node.height + padding - bbox.height - 3);
|
|
|
|
|
|
|
|
// Center the label
|
|
|
|
label.attr(
|
|
|
|
'transform',
|
|
|
|
'translate(' +
|
|
|
|
(node.x - bbox.width / 2) +
|
|
|
|
', ' +
|
|
|
|
(node.y - node.height / 2 - node.padding / 3 + 3) +
|
|
|
|
')'
|
|
|
|
);
|
|
|
|
|
|
|
|
const rectBox = rect.node().getBBox();
|
|
|
|
node.width = rectBox.width;
|
|
|
|
node.height = rectBox.height;
|
|
|
|
|
|
|
|
node.intersect = function(point) {
|
|
|
|
return intersectRect(node, point);
|
|
|
|
};
|
|
|
|
|
|
|
|
return shapeSvg;
|
|
|
|
};
|
|
|
|
|
2020-03-29 14:20:49 +02:00
|
|
|
const shapes = { rect, roundedWithTitle, noteGroup };
|
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) => {
|
2020-04-16 19:44:11 +02:00
|
|
|
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) => {
|
|
|
|
const label = createLabel(node.labelText, node.labelStyle);
|
|
|
|
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 = {};
|
|
|
|
};
|
|
|
|
|
2020-03-11 19:52:57 +01:00
|
|
|
export const positionCluster = node => {
|
2020-04-11 17:16:01 +02:00
|
|
|
log.info('Position cluster');
|
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 + ')');
|
|
|
|
};
|