jsplumb设置锚点,保存并加载jsPlumb流程图,包括精确的锚点和连接

本文档介绍了一种使用jsPlumb创建流程图,并实现保存和加载功能的方法。通过将节点位置和连接信息转换为JSON格式进行存储,然后在加载时重新构建流程图。然而,原始实现丢失了锚点和连接的精确位置。解决方案是保存和加载连接的锚点详细信息,以确保恢复时的准确性。尽管此解决方案保存了锚点信息,但未保留端点形状和类型等详细信息。
摘要由CSDN通过智能技术生成

This is the jsFiddle of the flowchart editor I am building.

This is an example of what can be easily created with "Add Decision" + "Add Task", connecting and moving the elements.

f2bdab9fe9de2ad0006cff7ba234cfaa.png

Now for the hard part: I want to be able to save and load the exact flowchart. For this I got started based with a similar thread here at Stackoverflow.

For this I added the "Save" and "Load" buttons that export/import the flowchart to/from JSON (shown in a textform in the jsFiddle after save - same textform can be used for loading).

The save function:

function saveFlowchart(){

var nodes = []

$(".node").each(function (idx, elem) {

var $elem = $(elem);

var endpoints = jsPlumb.getEndpoints($elem.attr('id'));

console.log('endpoints of '+$elem.attr('id'));

console.log(endpoints);

nodes.push({

blockId: $elem.attr('id'),

nodetype: $elem.attr('data-nodetype'),

positionX: parseInt($elem.css("left"), 10),

positionY: parseInt($elem.css("top"), 10)

});

});

var connections = [];

$.each(jsPlumb.getConnections(), function (idx, connection) {

connections.push({

connectionId: connection.id,

pageSourceId: connection.sourceId,

pageTargetId: connection.targetId

});

});

var flowChart = {};

flowChart.nodes = nodes;

flowChart.connections = connections;

flowChart.numberOfElements = numberOfElements;

var flowChartJson = JSON.stringify(flowChart);

//console.log(flowChartJson);

$('#jsonOutput').val(flowChartJson);

}

The resulting JSON of the example above:

{"nodes":[{"blockId":"startpoint","nodetype":"startpoint","positionX":273,"positionY":8},{"blockId":"endpoint","nodetype":"endpoint","positionX":310,"positionY":385},{"blockId":"taskcontainer1","nodetype":"task","positionX":381,"positionY":208},{"blockId":"decisioncontainer2","nodetype":"decision","positionX":261,"positionY":103}],"connections":[{"connectionId":"con_18","pageSourceId":"decisioncontainer2","pageTargetId":"taskcontainer1"},{"connectionId":"con_25","pageSourceId":"taskcontainer1","pageTargetId":"endpoint"},{"connectionId":"con_32","pageSourceId":"decisioncontainer2","pageTargetId":"endpoint"},{"connectionId":"con_46","pageSourceId":"startpoint","pageTargetId":"decisioncontainer2"}],"numberOfElements":2}

With that I am able to save the position of the elements as well as part of the information of the connections.

Here the load function:

function loadFlowchart(){

var flowChartJson = $('#jsonOutput').val();

var flowChart = JSON.parse(flowChartJson);

var nodes = flowChart.nodes;

$.each(nodes, function( index, elem ) {

if(elem.nodetype === 'startpoint'){

repositionElement('startpoint', elem.positionX, elem.positionY);

}else if(elem.nodetype === 'endpoint'){

repositionElement('endpoint', elem.positionX, elem.positionY);

}else if(elem.nodetype === 'task'){

var id = addTask(elem.blockId);

repositionElement(id, elem.positionX, elem.positionY);

}else if(elem.nodetype === 'decision'){

var id = addDecision(elem.blockId);

repositionElement(id, elem.positionX, elem.positionY);

}else{

}

});

var connections = flowChart.connections;

$.each(connections, function( index, elem ) {

var connection1 = jsPlumb.connect({

source: elem.pageSourceId,

target: elem.pageTargetId,

anchors: ["BottomCenter", [0.75, 0, 0, -1]]

});

});

numberOfElements = flowChart.numberOfElements;

}

However, the exact position of the anchors and connections are lost. Same example again, the result after deleting the elements and then loading the exported JSON:

db67d2c4bc1da810be2bbdb2ae604537.png

This comes not as a big surprise as I have not yet stored the information. But I am stuck at this point.

My question is: which information regarding the anchors/connectors position do I need to store for the whole flowchart design and how I can extract it from (& load into it again) jsPlumb?

解决方案

See this JSFiddle for a solution.

You need to save the anchor details as follows. This conforms to the Anchor representation defined here. Note the double nesting to avoid the JQuery auto-flatten on the map.

$.each(jsPlumb.getConnections(), function (idx, connection) {

connections.push({

connectionId: connection.id,

pageSourceId: connection.sourceId,

pageTargetId: connection.targetId,

anchors: $.map(connection.endpoints, function(endpoint) {

return [[endpoint.anchor.x,

endpoint.anchor.y,

endpoint.anchor.orientation[0],

endpoint.anchor.orientation[1],

endpoint.anchor.offsets[0],

endpoint.anchor.offsets[1]]];

})

});

});

...and load them, as follows:

$.each(connections, function( index, elem ) {

var connection1 = jsPlumb.connect({

source: elem.pageSourceId,

target: elem.pageTargetId,

anchors: elem.anchors

});

});

Note that this solution does not preserve end-point details including the shape and type of end-points. It only preserves the anchor details, as you requested.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值