Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of link labels #687

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/drawflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ export default class Drawflow {

}

addConnection(id_output, id_input, output_class, input_class) {
addConnection(id_output, id_input, output_class, input_class, labels) {
var nodeOneModule = this.getModuleFromNodeId(id_output);
var nodeTwoModule = this.getModuleFromNodeId(id_input);
if(nodeOneModule === nodeTwoModule) {
Expand Down Expand Up @@ -725,6 +725,10 @@ export default class Drawflow {
connection.classList.add(output_class);
connection.classList.add(input_class);
connection.appendChild(path);
if (labels) {
var connectionLabel = this.buildConnectionLabels(labels, id_input, id_output)
connection.appendChild(connectionLabel);
}
this.precanvas.appendChild(connection);
this.updateConnectionNodes('node-'+id_output);
this.updateConnectionNodes('node-'+id_input);
Expand All @@ -735,6 +739,35 @@ export default class Drawflow {
}
}

buildConnectionLabels(labels, id_input, id_output) {
var foreignObject = document.createElementNS('http://www.w3.org/2000/svg', "foreignObject");
if (!labels || labels.length == 0) {
return foreignObject;
}
foreignObject.setAttributeNS(null, 'width', '100%');
foreignObject.setAttributeNS(null, 'height', '100%');
foreignObject.style['overflow'] = 'visible';
var div = document.createElement('div');
labels.forEach((labelHtml) => {
var template = document.createElement('template');
labelHtml = labelHtml.trim();
template.innerHTML = labelHtml;
div.appendChild(template.content.firstChild);
})
div.classList.add("connection-label-container");
div.setAttribute('node_in', id_input);
div.setAttribute('node_out', id_output);
foreignObject.appendChild(div);
return foreignObject;
}

updateConnectionLabels(labelFOContainer, start_pos_x, start_pos_y, end_pos_x, end_pos_y) {
var xMid = start_pos_x + (end_pos_x - start_pos_x) / 2
var yMid = start_pos_y + (end_pos_y - start_pos_y) / 2
labelFOContainer.setAttributeNS(null, 'x', xMid);
labelFOContainer.setAttributeNS(null, 'y', yMid);
}

updateConnectionNodes(id) {

// Aquí nos quedamos;
Expand All @@ -745,6 +778,7 @@ export default class Drawflow {
const precanvas = this.precanvas;
const curvature = this.curvature;
const createCurvature = this.createCurvature;
const updateConnectionLabels = this.updateConnectionLabels;
const reroute_curvature = this.reroute_curvature;
const reroute_curvature_start_end = this.reroute_curvature_start_end;
const reroute_fix_curvature = this.reroute_fix_curvature;
Expand Down Expand Up @@ -780,6 +814,9 @@ export default class Drawflow {

const lineCurve = createCurvature(line_x, line_y, x, y, curvature, 'openclose');
elemsOut[item].children[0].setAttributeNS(null, 'd', lineCurve );
if (elemsOut[item].children[1]) {
updateConnectionLabels(elemsOut[item].children[1], line_x, line_y, x, y);
}
} else {
const points = elemsOut[item].querySelectorAll('.point');
let linecurve = '';
Expand Down Expand Up @@ -924,6 +961,9 @@ export default class Drawflow {

const lineCurve = createCurvature(line_x, line_y, x, y, curvature, 'openclose');
elems[item].children[0].setAttributeNS(null, 'd', lineCurve );
if (elems[item].children[1]) {
updateConnectionLabels(elems[item].children[1], line_x, line_y, x, y);
}

} else {
const points = elems[item].querySelectorAll('.point');
Expand Down Expand Up @@ -1310,6 +1350,7 @@ export default class Drawflow {
}

addNodeImport (dataNode, precanvas) {
const buildConnectionLabels = this.buildConnectionLabels;
const parent = document.createElement('div');
parent.classList.add("parent-node");

Expand Down Expand Up @@ -1346,6 +1387,11 @@ export default class Drawflow {
connection.classList.add(input_item);

connection.appendChild(path);
var labels = dataNode.inputs[input_item].connections[output_item].labels
if (labels) {
var connectionLabel = buildConnectionLabels(labels, dataNode.id, dataNode.inputs[input_item].connections[output_item].node)
connection.appendChild(connectionLabel);
}
precanvas.appendChild(connection);

});
Expand Down