Files
workflow/app/view/component/flow/processFlow.html
T
2022-11-03 23:38:50 +08:00

798 lines
25 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template id="process-flow">
<div class="process-flow-container">
<v-card v-if="!readonly" class="flow-menu pa-4 mr-4" width="140px">
<div class="menu-item mb-3" v-for="item in menuList" draggable="true" @dragstart="drag(item)">
<i :class="item.icon"></i>
<div>{{item.name}}</div>
</div>
<v-divider></v-divider>
<v-btn block class="menu-item mt-3" :color="isConnect ? 'success' : 'gray'" @click="isConnect=true">
<i class="el-icon-bottom-right"></i>
<div>连线</div>
</v-btn>
<v-btn block class="menu-item" :color="!isConnect ? 'success' : 'gray'" @click="isConnect=false">
<i class="el-icon-rank"></i>
<div>选择</div>
</v-btn>
</v-card>
<v-card class="flow-detail pa-0">
<div id="flowContent" ref="flowContent" @drop="drop($event)" @dragover="allowDrop($event)" @dblclick="isConnect=false">
<div id="flow">
<div
v-show="auxiliaryLine.isShowXLine"
class="auxiliary-line-x"
:style="{
width: auxiliaryLinePos.width,
top: auxiliaryLinePos.y + 'px',
left: auxiliaryLinePos.offsetX + 'px'
}"
></div>
<div
v-show="auxiliaryLine.isShowYLine"
class="auxiliary-line-y"
:style="{
height: auxiliaryLinePos.height,
left: auxiliaryLinePos.x + 'px',
top: auxiliaryLinePos.offsetY + 'px'
}"
></div>
<flow-node v-for="node in data.nodeList" :current="currentNodeId" :key="node.id" :node="node" :id="node.id" :readonly="readonly" :isconnect="isConnect"
@delete-node="deleteNode" @change-node-site="changeNodeSite" @edit-node="editNode">
</flow-node>
</div>
</div>
</v-card>
<v-card v-show="!readonly" class="flow-edit pa-4 ml-4">
<template v-if="editType == 'node' || editType == 'line'">
<div class="mb-3">
<v-icon>mdi-application-edit-outline</v-icon>
<span style="transform: translateY(2px); display: inline-block; ">{{editType == 'node' ? '节点' : '连线'}}属性</span>
</div>
<div class="flow-edit-content">
<edit-node ref="nodeForm" v-if="editType == 'node'" :user="user" :group="group" :tpl="tpl"></edit-node>
<edit-line ref="lineForm" v-if="editType == 'line'" @line-save="lineLabelSave"></edit-line>
</div>
</template>
<template v-else>
<div class="mb-3">
<v-icon>mdi-application-edit-outline</v-icon>
<span style="transform: translateY(2px); display: inline-block; ">属性面板</span>
</div>
<div style="text-align: center;line-height: 100px; color: #999999">
请选择节点或连接线
</div>
</template>
</v-card>
</div>
</template>
<link href="/<$ ctx.app.config.appId $>/public/jsplumb/jsplumbtoolkit-defaults.css" rel="stylesheet">
<script src="/<$ ctx.app.config.appId $>/public/jsplumb/jsplumb.min.js"></script>
<script src="/<$ ctx.app.config.appId $>/public/panzoom/panzoom.min.js"></script>
<script type="module">
Vue.component("process-flow", {
template: '#process-flow',
vuetify: new Vuetify(),
name: 'process-flow',
props: {
data: {
type: Object,
default: () => {
return {
nodeList: [],
lineList: []
}
}
},
user: {
type: Array,
default: () => {
return []
}
},
group: {
type: Array,
default: () => {
return []
}
},
tpl: {
type: Array,
default: () => {
return []
}
},
currentNodeId: {
type: String,
default: ''
},
readonly: {
type: Boolean,
default: false
},
},
data: () => {
const connectorPaintStyle = {
lineWidth: 4,
strokeStyle: "#61B7CF",
joinstyle: "round",
outlineColor: "white",
outlineWidth: 2
};
// 鼠标悬浮在连接线上的样式
const connectorHoverStyle = {
lineWidth: 4,
strokeStyle: "#216477",
outlineWidth: 2,
outlineColor: "white"
};
return {
menuList: [{
type: 'start',
name: '起点',
icon: 'el-icon-help'
},
{
name: '终点',
type: 'end',
icon: 'el-icon-s-help'
},
{
name: '审批节点',
type: 'userTask',
icon: 'el-icon-user'
},
{
name: '处理节点',
type: 'receiveTask',
icon: 'el-icon-s-tools'
},
],
auxiliaryLine: { isShowXLine: false, isShowYLine: false }, //对齐辅助线是否显示
auxiliaryLinePos: {
width: "100%",
height: "100%",
offsetX: 0,
offsetY: 0,
x: 20,
y: 20
},
jsPlumb: null, // jsPlumb 实例
index: 1,
// 默认设置参数
jsplumbSetting: {
// 动态锚点、位置自适应
Anchors: ['Top', 'TopCenter', 'TopRight', 'TopLeft', 'Right', 'RightMiddle', 'Bottom', 'BottomCenter',
'BottomRight', 'BottomLeft', 'Left', 'LeftMiddle'
],
Container: 'flow',
// 连线的样式 StateMachine、Flowchart,有四种默认类型:Bezier(贝塞尔曲线),Straight(直线),Flowchart(流程图),State machine(状态机)
Connector: 'Straight',
//这个是鼠标拉出来的线的属性
ConnectionOverlays: [
["Label", {
label: "同意-连线文本",
id: "label-1",
cssClass: "csslabel"
}]
],
// 鼠标不能拖动删除线
ConnectionsDetachable: false,
// 删除线的时候节点不删除
DeleteEndpointsOnDetach: false,
// 连线的端点
// Endpoint: ["Dot", {radius: 5}],
Endpoint: ["Rectangle", {
height: 10,
width: 10
}],
// 线端点的样式
EndpointStyle: {
fill: 'rgba(255,255,255,0)',
outlineWidth: 1
},
LogEnabled: true, //是否打开jsPlumb的内部日志记录
// 绘制线
PaintStyle: {
stroke: '#409eff',
strokeWidth: 2
},
// 绘制箭头
Overlays: [
['Arrow', {
width: 8,
length: 8,
location: 1
}],
],
RenderMode: "svg"
},
// jsplumb连接参数
jsplumbConnectOptions: {
isSource: true,
isTarget: true,
// 动态锚点、提供了4个方向 Continuous、AutoDefault
anchor: "Continuous",
},
jsplumbSourceOptions: {
filter: ".flow-node-drag", //触发连线的区域
/*"span"表示标签,".className"表示类,"#id"表示元素id*/
filterExclude: false,
anchor: "Continuous",
allowLoopback: false
},
jsplumbTargetOptions: {
filter: ".flow-node-drag",
/*"span"表示标签,".className"表示类,"#id"表示元素id*/
filterExclude: false,
anchor: "Continuous",
allowLoopback: false
},
// 是否加载完毕
loadEasyFlowFinish: false,
// 数据
currentItem: '', //临时存添加的元素
isConnect: false, //判断是否连接
timer: null, //定时器,判断单双击,
currentConnect: '', //当前的连接线
currentLine: '', //当前连接线数据
editType: 'flow', //编辑的类型
connectorPaintStyle,
// 鼠标悬浮在连接线上的样式
connectorHoverStyle,
hollowCircle: {
endpoint: ["Dot", { radius: 8 }], //端点的形状
connectorStyle: connectorPaintStyle,//连接线的颜色,大小样式
connectorHoverStyle: connectorHoverStyle,
paintStyle: {
strokeStyle: "#1e8151",
fillStyle: "transparent",
radius: 2,
lineWidth: 2
}, //端点的颜色样式
//anchor: "AutoDefault",
isSource: true, //是否可以拖动(作为连线起点)
connector: ["Flowchart", { stub: [40, 60], gap: 10, cornerRadius: 5, alwaysRespectStubs: true }], //连接线的样式种类有[Bezier],[Flowchart],[StateMachine ],[Straight ]
isTarget: true, //是否可以放置(连线终点)
maxConnections: -1, // 设置连接点最多可以连接几条线
connectorOverlays: [["Arrow", { width: 10, length: 10, location: 1 }]]
},
lineTypeList: [
{text: '同意', value: 'agree'},
{text: '拒绝', value: 'deny'},
]
};
},
watch: {
data: {
// 每个属性值发生变化就会调用这个函数
handler(newVal, oldVal) {
this.saveData(newVal)
},
// 深度监听 属性的变化
deep: true
}
},
created() { },
mounted() {
this.jsPlumb = jsPlumb.getInstance();
this.$nextTick(() => {
this.init();
});
},
methods: {
init() {
const _this = this
if (this.jsPlumb) {
_this.jsPlumb.setSuspendDrawing(false, true);
}
this.jsPlumb.ready(function () {
// 导入默认配置
_this.jsPlumb.importDefaults(_this.jsplumbSetting)
// 会使整个jsPlumb立即重绘。
_this.jsPlumb.setSuspendDrawing(false, true);
// 初始化节点
_this.loadEasyFlow()
// 单点连接线(编辑label),
_this.jsPlumb.bind('click', function (conn, originalEvent) {
if (_this.readonly) {
// 只读模式禁止切换
return
}
//clearTimeout(this.timer);
//this.timer = setTimeout(function () { // 这里采用执行自定义事件的方式
// console.log("click", conn);
// _this.editLine(conn);
//}, 300); // 延迟300ms执行单击事件,区分双击事件
// console.log("click", conn);
_this.editLine(conn);
// console.log(conn.getOverlay("label-1"));
//conn.getOverlay("label-1").setLabel('大肥肚'); //初始化label
})
// 双击连接线(删除),
_this.jsPlumb.bind('dblclick', function (conn, originalEvent) {
if (_this.readonly) {
// 只读模式禁止删除
return
}
clearTimeout(this.timer);
console.log("dblclick", conn)
_this.jsPlumb.deleteConnection(conn)
})
// 连线
_this.jsPlumb.bind("connection", function (evt) {
console.log('connection', evt)
let from = evt.source.id
let to = evt.target.id
if (_this.loadEasyFlowFinish) {
_this.data.lineList.push({
from: from,
to: to,
label: '连线名称',
type: 'agree',
id: _this.getUUID('line'),
Remark: '',
endpoint: 'Dot' // 线的类型
})
};
setTimeout(function () {
_this.editLine(evt.connection, true);
}, 100);
})
// 删除连线
_this.jsPlumb.bind("connectionDetached", function (evt) {
console.log('connectionDetached', evt)
_this.deleteLine(evt.sourceId, evt.targetId)
})
// 改变线的连接节点
_this.jsPlumb.bind("connectionMoved", function (evt) {
console.log('connectionMoved', evt)
_this.changeLine(evt.originalSourceId, evt.originalTargetId)
})
// 单击endpoint
// jsPlumb.bind("endpointClick", function (evt) {
// console.log('endpointClick', evt)
// })
//
// // 双击endpoint
// jsPlumb.bind("endpointDblClick", function (evt) {
// console.log('endpointDblClick', evt)
// })
// contextmenu 右键
_this.jsPlumb.bind("contextmenu", function (evt) {
console.log('contextmenu', evt)
})
// beforeDrop
_this.jsPlumb.bind("beforeDrop", function (evt) {
console.log('beforeDrop', evt)
let from = evt.sourceId
let to = evt.targetId
if (from === to) {
window.vtoast.fail('不能连接自己');
return false
}
if (_this.hasLine(from, to)) {
window.vtoast.fail({ message: '不能重复连线' });
return false
}
// if (_this.hashOppositeLine(from, to)) {
// window.vtoast.fail('不能回环哦');
// return false
// }
return true
})
// beforeDetach
_this.jsPlumb.bind("beforeDetach", function (evt) {
console.log('beforeDetach', evt)
})
})
this.initPanZoom();
},
// 加载流程图
loadEasyFlow() {
// 初始化节点
for (var i = 0; i < this.data.nodeList.length; i++) {
let node = this.data.nodeList[i]
// 设置源点,可以拖出线连接其他节点
this.jsPlumb.makeSource(node.id, this.jsplumbSourceOptions)
// // 设置目标点,其他源点拖出的线可以连接该节点
this.jsPlumb.makeTarget(node.id, this.jsplumbTargetOptions)
// 设置可拖拽
// jsPlumb.draggable(node.id, {
// containment: 'parent',
// grid: [10, 10]
// })
this.jsPlumb.draggable(node.id, {
containment: 'parent'
})
// jsPlumb.draggable(node.id)
}
// 初始化连线
for (var i = 0; i < this.data.lineList.length; i++) {
let line = this.data.lineList[i]
let connection = this.jsPlumb.connect({
source: line.from,
target: line.to,
endpoint: 'Dot' // 线的类型
}, this.jsplumbConnectOptions);
if (connection && connection.getOverlay("label-1")) {
connection.getOverlay("label-1").setLabel(line.type + '-' +line.label); //初始化label
}
}
this.$nextTick(function () {
this.loadEasyFlowFinish = true
})
},
// 添加新的节点
addNode(temp) {
console.log('添加节点', temp)
this.data.nodeList.push(temp);
this.$nextTick(function () {
this.jsPlumb.makeSource(temp.id, this.jsplumbSourceOptions)
this.jsPlumb.makeTarget(temp.id, this.jsplumbTargetOptions)
this.jsPlumb.draggable(temp.id, {
containment: 'parent'
})
})
},
// 删除线
deleteLine(from, to) {
this.data.lineList = this.data.lineList.filter(function (line) {
return line.from !== from && line.to !== to
})
},
// 改变连线
changeLine(oldFrom, oldTo) {
this.deleteLine(oldFrom, oldTo)
},
// 改变节点的位置
changeNodeSite(data) {
for (var i = 0; i < this.data.nodeList.length; i++) {
let node = this.data.nodeList[i]
if (node.id === data.nodeId) {
this.data.nodeList[i].left = data.left
this.data.nodeList[i].top = data.top
}
}
},
//删除节点
async deleteNode(nodeId) {
// this.data.nodeList = this.data.nodeList.filter(function(node) {
// if (node.id === nodeId) {
// node.show = false
// }
// return true
// })
this.data.nodeList.forEach((item, index) => {
if (item.id === nodeId) {
this.data.nodeList.splice(index, 1);
}
})
this.$nextTick(function () {
console.log('删除' + nodeId)
this.jsPlumb.removeAllEndpoints(nodeId);
})
return true
},
//编辑节点
editNode(node) {
if (this.readonly) {
// 只读模式、禁止切换节点
return
}
//console.log('编辑节点', nodeId)
this.editType = 'node';
this.currentNodeId = node.id
this.changeLineState("");
this.$nextTick(function () {
this.$refs.nodeForm.init(this.data, node)
})
},
editLine(conn, add = false) {
const _this = this;
this.currentNodeId = ''
_this.currentConnect = conn;
_this.data.lineList.forEach(function (item, index) {
if (item.from == conn.sourceId && item.to == conn.targetId) {
// 获取上一个节点
const preNode = _this.data.nodeList.find(e => e.id === item.from);
const lineTypeList = preNode.lineTypeList ? preNode.lineTypeList.split(',') : []
// 如果不是开始节点 并且类型不在允许的操作内
if (!lineTypeList.includes(item.type) ) {
// 1 - 删除画布连线 | 2 - 清空line的操作属性
// _this.jsPlumb.deleteConnection(conn)
// 默认修改类型为第一个
if (!preNode.id.includes('start-')) {
if (add) {
item.type = lineTypeList[0] || '';
} else {
item.type = '';
}
} else {
item.type = lineTypeList[0] || '';
}
// item.type = lineTypeList[0] || '';
}
_this.currentLine = item;
_this.editType = 'line';
_this.changeLineState("line", item);
_this.$nextTick(function () {
_this.$refs.lineForm.init(item, lineTypeList)
})
return;
}
});
},
//更改连线状态
changeLineState(nodeId, val) {
let lines = this.jsPlumb.getAllConnections();
if (!nodeId) {
lines.forEach((line) => {
line.canvas.classList.remove("active");
});
return;
} else if (nodeId === "line") {
lines.forEach((line) => {
if (line.targetId === val.to && line.sourceId === val.from) {
line.canvas.classList.add("active");
} else {
line.canvas.classList.remove("active");
}
});
}
},
//删除线
delLine(conn) {
this.jsPlumb.deleteConnection(conn)
},
// 是否具有该线
hasLine(from, to) {
return !!this.data.lineList.find(line => line.from === from && line.to === to)
},
// 是否含有相反的线
hashOppositeLine(from, to) {
return this.hasLine(to, from)
},
lineLabelSave(line) {
this.currentConnect.getOverlay("label-1").setLabel(line.type + '-' +line.label);
//this.$set(this.currentLine, 'label', line.label);
},
drag(item) {
this.currentItem = item;
},
drop(event) {
//event.preventDefault();
var _obj = this.$refs.flowContent;
var temp = {
id: this.getUUID(this.currentItem.type),
label: this.currentItem.name,
top: event.offsetY + 'px',
left: event.offsetX + 'px',
type: this.currentItem.type,
assignType: 'person',
assignValue: ['<$ ctx.userInfo.userId $>'],
lineTypeList: this.currentItem.type === 'start' ? '同意' : this.currentItem.type === 'end' ? '' : '同意,拒绝',
}
this.addNode(temp);
this.editNode(temp);
},
allowDrop(event) {
event.preventDefault()
},
getUUID(prefix) {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 10; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
var uuid = s.join("");
return prefix + '-' + uuid;
},
saveData() {
//console.log(this.jsPlumb)
//console.log(this.jsPlumb.Defaults)
//console.log('线', this.jsPlumb.getConnections())
console.log(this.data);
this.$emit('change-structure', this.data)
},
initPanZoom() {
const mainContainer = this.jsPlumb.getContainer();
const mainContainerWrap = mainContainer.parentNode;
const pan = panzoom(mainContainer, {
smoothScroll: false,
bounds: true,
// autocenter: true,
zoomDoubleClickSpeed: 1,
minZoom: 0.5,
maxZoom: 2,
//设置滚动缩放的组合键,默认不需要组合键
beforeWheel: (e) => {
// console.log(e);
// let shouldIgnore = !e.ctrlKey
// return shouldIgnore
},
beforeMouseDown: function (e) {
// allow mouse-down panning only if altKey is down. Otherwise - ignore
var shouldIgnore = e.ctrlKey;
return shouldIgnore;
}
});
this.jsPlumb.mainContainerWrap = mainContainerWrap;
this.jsPlumb.pan = pan;
// 缩放时设置jsPlumb的缩放比率
pan.on("zoom", (e) => {
const { x, y, scale } = e.getTransform();
this.jsPlumb.setZoom(scale);
//根据缩放比例,缩放对齐辅助线长度和位置
this.auxiliaryLinePos.width = (1 / scale) * 100 + "%";
this.auxiliaryLinePos.height = (1 / scale) * 100 + "%";
this.auxiliaryLinePos.offsetX = -(x / scale);
this.auxiliaryLinePos.offsetY = -(y / scale);
});
pan.on("panend", (e) => {
const { x, y, scale } = e.getTransform();
this.auxiliaryLinePos.width = (1 / scale) * 100 + "%";
this.auxiliaryLinePos.height = (1 / scale) * 100 + "%";
this.auxiliaryLinePos.offsetX = -(x / scale);
this.auxiliaryLinePos.offsetY = -(y / scale);
});
// 平移时设置鼠标样式
mainContainerWrap.style.cursor = "grab";
mainContainerWrap.addEventListener("mousedown", function wrapMousedown() {
this.style.cursor = "grabbing";
mainContainerWrap.addEventListener("mouseout", function wrapMouseout() {
this.style.cursor = "grab";
});
});
mainContainerWrap.addEventListener("mouseup", function wrapMouseup() {
this.style.cursor = "grab";
});
}
}
})
</script>
<style>
.process-flow-container {
display: flex;
width: 100%;
height: calc(100vh - 260px);
color: #606266;
}
.flow-detail {
flex: 1;
border: 1px solid #EBEEF5;
padding: 12px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
word-break: break-all;
border-radius: 4px;
position: relative;
}
#flowContent {
width: 100%;
height: 100%;
position: relative;
background-image: url("/<$ ctx.app.config.appId $>/public/images/point.png");
background-repeat: repeat;
overflow: hidden;
cursor: grab;
}
#flowContent #flow {
position: relative;
width: 100%;
height: 100%;
}
#flowContent #flow .auxiliary-line-x {
position: absolute;
border: 0.5px dashed #2ab1e8;
z-index: 9999;
}
#flowContent #flow .auxiliary-line-y {
position: absolute;
border: 0.5px dashed #2ab1e8;
z-index: 9999;
}
.item {
position: absolute;
width: 60px;
height: 90px;
border: 1px solid #007AFF;
}
.flow-menu {
width: 100px;
border: 1px solid #EBEEF5;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
word-break: break-all;
border-radius: 4px;
}
.menu-item {
text-align: center;
margin-bottom: 8px;
cursor: pointer;
font-size: 14px;
color: #606266;
background: #f5f8fa;
padding: 5px 0;
border-radius: 5px;
}
.menu-item:hover {
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.menu-item>i {
font-size: 30px;
}
.flow-edit {
width: 300px;
padding: 0 10px;
max-height: 100%;
min-height: 100%;
overflow-x: hidden;
overflow-y: auto;
}
.flow-edit-content {
min-height: 100%;
color: #606266;
line-height: 1.4;
text-align: justify;
font-size: 14px;
padding: 12px;
word-break: break-all;
border-radius: 4px;
}
.csslabel {
color: #434343;
font-weight: 400;
z-index: 10;
font-size: 12px;
color: #409eff;
background: #fff;
}
.csslabel .label-text {
background-color: white;
}
.jtk-connector.active {
z-index: 9999;
}
.jtk-connector.active path {
stroke: #150042;
stroke-width: 1.5;
animation: ring;
animation-duration: 3s;
animation-timing-function: linear;
animation-iteration-count: infinite;
stroke-dasharray: 5;
}
</style>