页面模板
<div id="start" class="start" @mouseover="mouseover('start')"
@mouseleave="mouseleave('start')">
<div class="opacity" id="opacity-start">
</div>
开始</div>
初始化模板
{
isSource: true,
isTarget: true,
connector:["Bezier",{curviness: 63}],
overlays: [
['Arrow', { width: 8, length: 8, location: 1}]
],
endpoint: ['Dot', {radius: 4}],
paintStyle: {
fill: '#FDFDFD',
stroke:'#ccc',
strokeWidth: 0.5
},
maxConnections: 50,
hoverPaintStyle: {
outlineStroke: '#758CFE'
},
connectorStyle: {
outlineStroke: '#ccc',
strokeWidth: 0.2,
joinstyle: 'arrow',
stroke: '#ccc'
},
connectorHoverStyle: {
strokeWidth: 0.2
}
}
代码初始化
loadProcessFlow() {
this.selectNodes = []
this.nodeTree = {}
this.jsPlumb = jsPlumb.getInstance()
this.jsPlumb.ready(() => {
this.jsPlumb.draggable('start', {
containment: "flowWrap"
})
this.jsPlumb.draggable('end', {
containment: "flowWrap"
})
this.addpoint('start', 'Right')
this.addpoint('end', 'Left')
this.makePoint('start', 'makeSource')
this.makePoint('end', 'makeTarget')
this.jsPlumb.bind('dblclick', conn => {
const {sourceId, targetId} = conn
this.jsPlumb.deleteConnection(conn)
this.selectNodes = this.selectNodes
.filter(item => !(item.sourceId === sourceId && item.targetId === targetId))
if (this.nodeTree[sourceId].length === 1) {
delete this.nodeTree[sourceId]
} else {
this.nodeTree[sourceId] = this.nodeTree[sourceId]
.filter(item => item !== targetId)
}
})
this.jsPlumb.bind('beforeDrop', info => {
const {sourceId, targetId, connection: {endpoints}} = info
if (targetId === 'start' ||
sourceId === 'end' ||
sourceId === targetId ||
(sourceId === 'start' && targetId === 'end')) {
return false
}
const startNode = this.selectNodes.find(node => node.sourceId === 'start' && sourceId === 'start')
if (startNode) {
return false
}
const findIndex = this.selectNodes.findIndex(item =>
item.sourceId === sourceId && item.targetId === targetId
)
if (findIndex > -1) return false
if (endpoints) {
const anchorslength = endpoints[0].anchor.anchors.length
const firstEndPoint = endpoints[0].anchor.anchors[anchorslength-1].type
const secondEndPoint = info.dropEndpoint.anchor.anchors[0].type
if (firstEndPoint === 'Left' && secondEndPoint === 'Right') {
return false
}
if (firstEndPoint === 'Right' && secondEndPoint === 'Right') {
return false
}
if (firstEndPoint === 'Left' && secondEndPoint === 'Left') {
return false
}
}
const currentSourceId = this.nodeTree[sourceId];
if (currentSourceId && currentSourceId.length > 0) {
for (let sr = 0; sr < currentSourceId.length; sr++) {
if (!this.nodeTree[currentSourceId[sr]]) {
continue
}
if (this.nodeTree[currentSourceId[sr]].includes('end') &&
targetId === this.nodeTree[currentSourceId[sr]]) {
return false
}
}
}
let targetNodeCompare = this.selectNodes.find(node => node.targetId === targetId)
if (targetNodeCompare && (targetNodeCompare.sourceId === 'start' || sourceId === 'start')) {
return false
}
targetNodeCompare = this.selectNodes.find(node => node.sourceId === sourceId)
if (targetNodeCompare && targetId === 'end') {
return false
}
targetNodeCompare = this.selectNodes.find(node => node.sourceId === sourceId && node.targetId === 'end')
if (targetNodeCompare) {
return false
}
const sourceNodeCompare = this.selectNodes.find(node => node.targetId === targetId && node.sourceId !== sourceId)
if (sourceNodeCompare && sourceNodeCompare.targetId !== 'end') {
}
this.setSelectNodes(sourceId, targetId)
return true
})
this.getProcessInfo()
this.listenWindowResize()
})
},
拖拽节点连线
packageNode(processNodes = []) {
processNodes.forEach(item => {
const {sourceId, targetId, uuid, subMilestone, children = []} = item
this.nodeList.push(item)
this.$nextTick(() => {
this.jsPlumb.draggable(uuid, {
containment: "flowWrap",
})
this.addpoint(uuid, 'Left')
this.addpoint(uuid, 'Right')
this.makePoint(uuid, 'makeSource')
this.makePoint(uuid, 'makeTarget')
if (sourceId.indexOf(',') !== -1) {
let sourceIdSplit = sourceId.split(',')
sourceIdSplit.forEach(node => {
this.setSelectNodes(node, targetId)
this.jsPlumb.connect({
source: node,
target: targetId
}, {
anchor: node === 'start' ? ['Right', 'Left'] : ['Left', 'Right']
})
if (subMilestone === false) {
this.setSelectNodes(targetId, 'end')
this.jsPlumb.connect({
source: targetId,
target: "end"
}, {
anchor: ['Right', 'Left']
})
}
})
return
}
this.setSelectNodes(sourceId, targetId)
this.jsPlumb.connect({
source: sourceId,
target: targetId
}, {
anchor: sourceId === 'start' ? ['Right', 'Left'] : ['Left', 'Right']
})
if (subMilestone === false) {
this.setSelectNodes(targetId, 'end')
this.jsPlumb.connect({
source: targetId,
target: "end"
}, {
anchor: ['Right', 'Left']
})
}
})
})
},
onDragEnd(event) {
const {x, y} = event
if (x < (this.menuIscollapse ? 320 : 200)) return
if (y < 170) return
const uuid = `processNode${new Date().getTime()}`
this.nodeList.push({
uuid,
left: this.menuIscollapse ? x - 360 : x- 200,
top: y - 170
})
this.$nextTick(() => {
this.jsPlumb.draggable(uuid, {
containment: "flowWrap"
})
this.addpoint(uuid, 'Left')
this.addpoint(uuid, 'Right')
})
},
获取节点连线数据
const connectNodeData = this.jsPlumb.getConnections()