vue2中使用jsplumb完成流程图

前言

之前的几个demo都是在vue3中写的,虽然可以直接拿去复用。

但是根据有些看客反馈,想用一个vue2版本的,毕竟很多人开发功能的时间都不是特别富裕。大多时候还是用现成的demo更好一些。

这里我就写一个简易版本的demo,可以实现绘制,并且删除连接线和节点等功能,篇幅也不大,适合应急的朋友,哈哈

代码

1.剥离公共的配置  config.js

export const readyConfig = {Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}
export const sourceConfig = {filter: '.pointNode',filterExclude: false,allowLoopback: true,maxConnections: -1,Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}
export const targetConfig = {filter: '.pointNode',filterExclude: false,allowLoopback: true,maxConnections: -1,Container: 'plumbBox',anchor: ['Bottom', 'Top', 'Left', 'Right'],connector: 'Straight',endpoint: 'Blank',PaintStyle: { stroke: '#8b8c8d', strokeWidth: 2, outlineStroke: 'transparent', outlineWidth: 10 },Overlays: [['Arrow', { width: 10, length: 5, location: 0.5, direction: 1 }]],endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 }
}

其实这些配置都大差不差,之所以分别罗列,是让大家搞明白,画布层级的配置,起点节点的配置以及终点节点的配置都是可以单独去配置,具体配置项可以看我之前的文档。

2.使用简易数据 data.js

export const leftMenuList = [{name: "节点1",id: "app1",},{name: "节点2",id: "app2",},{name: "节点3",id: "app3",},{name: "节点4",id: "app4",},
]

 这里的数据其实是左侧的数据

3.组件部分

<template><div class="flowBox"><div class="leftMenu"><h4 @click="checkInfo">左侧菜单</h4><draggable @start="start" @end="end" :sort="false"><divv-for="(item, index) in leftMenuList":key="item.id"@mousedown="(el) => downNode(el, item)"class="leftNode">{{ item.name }}</div><h4>操作提示</h4><hr /><p>左侧拖拽至右侧画布</p><p>右键节点是删除节点,左键线条是删除线条</p></draggable></div><div class="plumbBox" id="plumbBox"><divv-for="(item, index) in dataInfo":key="item.id":id="item.id":style="nodeStyle(item)"@click.right="deleteNode($event, item)">{{ item.name }}<div class="pointNode"></div></div></div></div>
</template>
<script>
import { jsPlumb } from "jsplumb";
import { readyConfig, sourceConfig, targetConfig } from "./config";
import { leftMenuList } from "./data";
import { cloneDeep } from "lodash";
import draggable from "vuedraggable";
export default {name: "flowChart",components: { draggable },data() {return {//节点列表leftMenuList: leftMenuList,dataInfo: [],//plumb实例PlumbInit: null,//关系列表renderList: [],//画布信息(大小和位置)canvasInfo: null,//鼠标位置精准判定nodePositionDiff: null,//选中的左侧列表节点ativeNodeItem: null,//线条的信息deleteLineInfo: null,};},mounted() {this.jsPlumbInit();this.readyPlumbDataFun("once");},methods: {//初始化jsPlumbInit() {this.PlumbInit = jsPlumb.getInstance();this.PlumbInit.importDefaults(readyConfig);},//组织渲染用的数据readyPlumbDataFun(flag) {this.renderList = [];this.PlumbInit.deleteEveryConnection();this.PlumbInit.deleteEveryEndpoint();this.$nextTick(() => {this.dataInfo = cloneDeep(this.dataInfo);//根据数据创建关联信息(连线关系)this.dataInfo.forEach((item) => {if (item.to && item.to.length > 0) {item.to.forEach((item1) => {let nodeConfig = Object.assign({source: item.id,target: item1,},readyConfig);this.renderList.push(nodeConfig);});}this.makeNodeConfig(item);});this.readyPlumbNodeFun(flag);});},//配置节点的具体信息makeNodeConfig(item) {this.PlumbInit.setSourceEnabled(item.id, true);this.PlumbInit.setTargetEnabled(item.id, true);this.PlumbInit.makeSource(item.id, sourceConfig);this.PlumbInit.makeTarget(item.id, targetConfig);this.PlumbInit.setDraggable(item.id, true);this.PlumbInit.draggable(item.id, {containment: "parent",stop: function (el) {item.left = el.pos[0];item.top = el.pos[1];},});},//渲染页面关系readyPlumbNodeFun(flag) {this.PlumbInit.ready(() => {this.renderList.forEach((item) => {this.PlumbInit.connect(item);});if (flag !== "once") {return;}//连线事件this.PlumbInit.bind("connection", (info) => {const sourceNode = this.dataInfo.find((item) => item.id === info.sourceId);if (sourceNode.to.includes(info.targetId)) {return false;}console.log("调用了几次");sourceNode.to.push(info.targetId);// this.$nextTick(() => {//   this.readyPlumbDataFun()// })return true;});//点击线条this.PlumbInit.bind("click", (con) => {this.deleteLineInfo = {source: con.sourceId,target: con.targetId,};this.deleteLine(this.deleteLineInfo);});});},//plumbNode的样式nodeStyle(item) {return {position: "absolute",left: item.left + "px",top: item.top + "px",width: "200px",height: "40px",lineHeight: "40px",textAlign: "center",borderLeft: "2px solid blue",borderRadius: "4%",cursor: "pointer",boxShadow: "#eee 3px 3px 3px 3px",};},//拖动开始start() {},//拖动结束end(e) {this.refreshCanvas();// 判断位置this.judgePosition(this.ativeNodeItem,this.canvasInfo,e.originalEvent.x,e.originalEvent.y);},//添加节点addNode(positionInfo, nodeInfo) {if (this.dataInfo.find((item) => item.id === nodeInfo.id)) {this.$message.error("该节点已经存在");return;}this.dataInfo.push({name: nodeInfo.name,id: nodeInfo.id,left: positionInfo.left,top: positionInfo.top,to: [],});this.$nextTick(() => {this.readyPlumbDataFun();});},//删除节点deleteNode($event, nodeInfo) {$event.returnValue = false;let index = this.dataInfo.findIndex((item) => item.id === nodeInfo.id);this.dataInfo.splice(index, 1);this.readyPlumbDataFun();},//删除线deleteLine(deleteLineInfo) {let dataInfo = cloneDeep(this.dataInfo);let node = dataInfo.find((val) => val.id === deleteLineInfo.source);let index = node.to.findIndex((val) => val === deleteLineInfo.target);node.to.splice(index, 1);this.dataInfo = null;this.dataInfo = dataInfo;this.readyPlumbDataFun();},checkInfo() {console.log(this.dataInfo, "dataInfo");console.log(this.renderList, "渲染关系");},//获取画布信息refreshCanvas() {this.canvasInfo = document.querySelector("#plumbBox").getBoundingClientRect();},//判断节点拖拽位置judgePosition(dragNodeInfo, plumbBoxPositionInfo, x, y) {if (x - this.nodePositionDiff.leftDiff < plumbBoxPositionInfo.left ||x + 200 - this.nodePositionDiff.leftDiff > plumbBoxPositionInfo.right ||y - this.nodePositionDiff.topDiff < plumbBoxPositionInfo.top ||y + 40 - this.nodePositionDiff.topDiff > plumbBoxPositionInfo.bottom) {this.$message.error("节点不能拖拽至画布之外");} else {const positionInfo = {top: y - plumbBoxPositionInfo.top - this.nodePositionDiff.topDiff,left: x - plumbBoxPositionInfo.left - this.nodePositionDiff.leftDiff,};this.addNode(positionInfo, dragNodeInfo);}},//鼠标抬起时,距离判定downNode(el, nodeItem) {this.ativeNodeItem = nodeItem;const mousedownPositionInfo = { x: el.clientX, y: el.clientY };// 被拖拽节点初始的位置信息const moveBoxBeforePosition = {x: el.target.getBoundingClientRect().x,y: el.target.getBoundingClientRect().y,};this.nodePositionDiff = {leftDiff: mousedownPositionInfo.x - moveBoxBeforePosition.x,topDiff: mousedownPositionInfo.y - moveBoxBeforePosition.y,};console.log(this.nodePositionDiff, "位置判定");},},
};
</script>
<style scoped>
.flowBox {display: flex;height: 100%;
}.leftMenu {width: 300px;/* height: 100%; */border: 1px solid #1a1919;
}h4 {margin-top: 10px;margin-left: 110px;
}.leftNode {width: 200px;height: 40px;line-height: 40px;text-align: center;margin: 30px;border: dashed 1px #362c2c;cursor: move;
}.plumbBox {flex: 1;/* height: 100%; */position: relative;
}
.pointNode {border-radius: 50%;width: 10px;height: 10px;background: royalblue;position: absolute;bottom: -5px;left: 95px;
}
</style>

各个函数的注释都写好了,操作方法也在其中,最主要的是根据插件暴露的api去领悟其中的每一个方法。

效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/259324.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

每天五分钟计算机视觉:稠密连接网络(DenseNet)

本文重点 在前面的课程中我们学习了残差网络ResNet,而DenseNet可以看成是ResNet的后续,我们看一下图就可以看出二者的主要区别了。 特点 DenseNet是一种卷积神经网络,它的特点是每一层都直接连接到所有后续层。这意味着,每一层都接收来自前一层的输出,并将其作为输入传递…

流程画布开发技术方案归档(G6)

&#x1f3a8; 在理想的最美好世界中&#xff0c;一切都是为最美好的目的而设。 —— 伏尔泰 如果可以实现记得点赞分享&#xff0c;谢谢老铁&#xff5e; 一、技术选型 •从可维护性和可拓展性出发 •基本满足 1&#xff1a;链接: https://github.com/hukaibaihu/vue-org…

C++相关闲碎记录(5)

1、容器提供的类型 2、Array Array大小固定&#xff0c;只允许替换元素的值&#xff0c;不能增加或者移除元素改变大小。Array是一种有序集合&#xff0c;支持随机访问。 std::array<int, 4> x; //elements of x have undefined value std::array<int, 5> x {…

12v转48v升压电源芯片:参数、特点及应用领域

12v转48v升压电源芯片&#xff1a;参数、特点及应用领域 随着电子设备的日益普及&#xff0c;电源芯片的需求也在不断增长。今天我们将介绍一款具有广泛应用前景的升压电源芯片——12v转48v升压电源芯片。该芯片具有宽输入电压范围、高效率、固定工作频率等特点&#xff0c;适…

python 使用 watchdog 实现类似 Linux 中 tail -f 的功能

一、代码实现 import logging import os import threading import timefrom watchdog.events import FileSystemEventHandler from watchdog.observers import Observerlogger logging.getLogger(__name__)class LogWatcher(FileSystemEventHandler):def __init__(self, log_…

设计模式——单例模式(Singleton Pattern)

概述 单例模式确保一个类只有一个实例&#xff0c;而且自行实例化并向整个系统提供整个实例&#xff0c;这个类称为单例类&#xff0c;它提供全局访问的方法。单例模式是一种对象创建型模式。单例模式有三个要点&#xff1a;一是某个类只能有一个实例&#xff1b;二是它必须自行…

2023年8月14日 Go生态洞察:向后兼容性、Go 1.21与Go 2

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

python3.5安装教程及环境配置,python3.7.2安装与配置

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;python3.5安装教程及环境配置&#xff0c;python3.7.2安装与配置&#xff0c;现在让我们一起来看看吧&#xff01; python 从爬虫开始&#xff08;一&#xff09; Python 简介 首先简介一下Python和爬虫的关系与概念&am…

Qt12.8

使用手动连接&#xff0c;将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中&#xff0c;在自定义的槽函数中调用关闭函数 将登录按钮使用qt5版本的连接到自定义的槽函数中&#xff0c;在槽函数中判断ui界面上输入的账号是否为"admin"&#xff0c;密码是否为…

智能优化算法应用:基于食肉植物算法无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于食肉植物算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于食肉植物算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.食肉植物算法4.实验参数设定5.算法结果6.参考…

9.关于Java的程序设计-基于Springboot的家政平台管理系统设计与实现

摘要 随着社会的进步和生活水平的提高&#xff0c;家政服务作为一种重要的生活服务方式逐渐受到人们的关注。本研究基于Spring Boot框架&#xff0c;设计并实现了一种家政平台管理系统&#xff0c;旨在提供一个便捷高效的家政服务管理解决方案。系统涵盖了用户注册登录、家政服…

论文精读 MOG 埃里克·格里姆森

Adaptive background mixture models for real-time tracking 用于实时跟踪的自适应背景混合模型 1999年的MOG&#xff0c;作者是麻省理工学院前校长——埃里克格里姆森&#xff08;W. Eric L. Grimson&#xff09;。 目录 摘要 结论 1 介绍 1.1 以往的工作和现在的缺点 …