methods.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import panzoom from "panzoom";
  2. import { GenNonDuplicateID } from "@/util/util";
  3. const methods = {
  4. init() {
  5. this.jsPlumb.ready(() => {
  6. // 导入默认配置
  7. this.jsPlumb.importDefaults(this.jsplumbSetting);
  8. //完成连线前的校验
  9. this.jsPlumb.bind("beforeDrop", evt => {
  10. let res = () => { } //此处可以添加是否创建连接的校验, 返回 false 则不添加;
  11. return res
  12. })
  13. // 连线创建成功后,维护本地数据
  14. this.jsPlumb.bind("connection", evt => {
  15. this.addLine(evt)
  16. });
  17. //连线双击删除事件
  18. this.jsPlumb.bind("dblclick",(conn, originalEvent) => {
  19. this.confirmDelLine(conn)
  20. })
  21. //断开连线后,维护本地数据
  22. this.jsPlumb.bind("connectionDetached", evt => {
  23. this.deleLine(evt)
  24. })
  25. this.loadEasyFlow();
  26. // 会使整个jsPlumb立即重绘。
  27. this.jsPlumb.setSuspendDrawing(false, true);
  28. });
  29. this.initPanZoom();
  30. },
  31. // 加载流程图
  32. loadEasyFlow() {
  33. // 初始化节点
  34. for (let i = 0; i < this.data.nodeList.length; i++) {
  35. let node = this.data.nodeList[i];
  36. // 设置源点,可以拖出线连接其他节点
  37. this.jsPlumb.makeSource(node.id, this.jsplumbSourceOptions);
  38. // // 设置目标点,其他源点拖出的线可以连接该节点
  39. this.jsPlumb.makeTarget(node.id, this.jsplumbTargetOptions);
  40. // this.jsPlumb.draggable(node.id);
  41. this.draggableNode(node.id)
  42. }
  43. // 初始化连线
  44. this.jsPlumb.unbind("connection"); //取消连接事件
  45. for (let i = 0; i < this.data.lineList.length; i++) {
  46. let line = this.data.lineList[i];
  47. this.jsPlumb.connect(
  48. {
  49. source: line.from,
  50. target: line.to
  51. },
  52. this.jsplumbConnectOptions
  53. );
  54. }
  55. this.jsPlumb.bind("connection", evt => {
  56. let from = evt.source.id;
  57. let to = evt.target.id;
  58. this.data.lineList.push({
  59. from: from,
  60. to: to,
  61. label: "连线名称",
  62. id: GenNonDuplicateID(8),
  63. Remark: ""
  64. });
  65. });
  66. },
  67. draggableNode(nodeId) {
  68. this.jsPlumb.draggable(nodeId, {
  69. grid: this.commonGrid,
  70. drag: (params) => {
  71. this.alignForLine(nodeId, params.pos)
  72. },
  73. start: () => {
  74. },
  75. stop: (params) => {
  76. this.auxiliaryLine.isShowXLine = false
  77. this.auxiliaryLine.isShowYLine = false
  78. this.changeNodePosition(nodeId, params.pos)
  79. }
  80. })
  81. },
  82. //移动节点时,动态显示对齐线
  83. alignForLine(nodeId, position) {
  84. let showXLine = false, showYLine = false
  85. this.data.nodeList.some(el => {
  86. if(el.id !== nodeId && el.left == position[0]+'px') {
  87. this.auxiliaryLinePos.x = position[0] + 60;
  88. showYLine = true
  89. }
  90. if(el.id !== nodeId && el.top == position[1]+'px') {
  91. this.auxiliaryLinePos.y = position[1] + 20;
  92. showXLine = true
  93. }
  94. })
  95. this.auxiliaryLine.isShowYLine = showYLine
  96. this.auxiliaryLine.isShowXLine = showXLine
  97. },
  98. changeNodePosition(nodeId, pos) {
  99. this.data.nodeList.some(v => {
  100. if(nodeId == v.id) {
  101. v.left = pos[0] +'px'
  102. v.top = pos[1] + 'px'
  103. return true
  104. }else {
  105. return false
  106. }
  107. })
  108. },
  109. drag(ele, item) {
  110. this.currentItem = item;
  111. },
  112. drop(event) {
  113. const containerRect = this.jsPlumb.getContainer().getBoundingClientRect();
  114. const scale = this.getScale();
  115. let left = (event.pageX - containerRect.left -60) / scale;
  116. let top = (event.pageY - containerRect.top -20) / scale;
  117. var temp = {
  118. ...this.currentItem,
  119. id: GenNonDuplicateID(8),
  120. top: (Math.round(top/20))*20 + "px",
  121. left: (Math.round(left/20))*20 + "px"
  122. };
  123. this.addNode(temp);
  124. },
  125. addLine(line) {
  126. let from = line.source.id;
  127. let to = line.target.id;
  128. this.data.lineList.push({
  129. from: from,
  130. to: to,
  131. label: "连线名称",
  132. id: GenNonDuplicateID(8),
  133. Remark: ""
  134. });
  135. },
  136. confirmDelLine(line) {
  137. this.$confirm(this.$t('askDeleteData'), {
  138. confirmButtonText: '确认删除该连线?',
  139. cancelButtonText: '取消',
  140. type: 'warning',
  141. }).then(() => {
  142. this.jsPlumb.deleteConnection(line)
  143. })
  144. },
  145. deleLine(line) {
  146. this.data.lineList.forEach((item, index) => {
  147. if(item.from === line.sourceId && item.to === line.targetId) {
  148. this.data.lineList.splice(index, 1)
  149. }
  150. })
  151. },
  152. // dragover默认事件就是不触发drag事件,取消默认事件后,才会触发drag事件
  153. allowDrop(event) {
  154. event.preventDefault();
  155. },
  156. getScale() {
  157. let scale1;
  158. if (this.jsPlumb.pan) {
  159. const { scale } = this.jsPlumb.pan.getTransform();
  160. scale1 = scale;
  161. } else {
  162. const matrix = window.getComputedStyle(this.jsPlumb.getContainer()).transform;
  163. scale1 = matrix.split(", ")[3] * 1;
  164. }
  165. this.jsPlumb.setZoom(scale1);
  166. return scale1;
  167. },
  168. // 添加新的节点
  169. addNode(temp) {
  170. this.data.nodeList.push(temp);
  171. this.$nextTick(() => {
  172. this.jsPlumb.makeSource(temp.id, this.jsplumbSourceOptions);
  173. this.jsPlumb.makeTarget(temp.id, this.jsplumbTargetOptions);
  174. this.draggableNode(temp.id)
  175. });
  176. },
  177. initPanZoom() {
  178. const mainContainer = this.jsPlumb.getContainer();
  179. const mainContainerWrap = mainContainer.parentNode;
  180. const pan = panzoom(mainContainer, {
  181. smoothScroll: false,
  182. bounds: true,
  183. // autocenter: true,
  184. zoomDoubleClickSpeed: 1,
  185. minZoom: 0.5,
  186. maxZoom: 2,
  187. //设置滚动缩放的组合键,默认不需要组合键
  188. beforeWheel: (e) => {
  189. console.log(e)
  190. // let shouldIgnore = !e.ctrlKey
  191. // return shouldIgnore
  192. },
  193. beforeMouseDown: function(e) {
  194. // allow mouse-down panning only if altKey is down. Otherwise - ignore
  195. var shouldIgnore = e.ctrlKey;
  196. return shouldIgnore;
  197. }
  198. });
  199. this.jsPlumb.mainContainerWrap = mainContainerWrap;
  200. this.jsPlumb.pan = pan;
  201. // 缩放时设置jsPlumb的缩放比率
  202. pan.on("zoom", e => {
  203. const { x, y, scale } = e.getTransform();
  204. this.jsPlumb.setZoom(scale);
  205. //根据缩放比例,缩放对齐辅助线长度和位置
  206. this.auxiliaryLinePos.width = (1/scale) * 100 + '%'
  207. this.auxiliaryLinePos.height = (1/scale) * 100 + '%'
  208. this.auxiliaryLinePos.offsetX = -(x/scale)
  209. this.auxiliaryLinePos.offsetY = -(y/scale)
  210. });
  211. pan.on("panend", (e) => {
  212. const {x, y, scale} = e.getTransform();
  213. this.auxiliaryLinePos.width = (1/scale) * 100 + '%'
  214. this.auxiliaryLinePos.height = (1/scale) * 100 + '%'
  215. this.auxiliaryLinePos.offsetX = -(x/scale)
  216. this.auxiliaryLinePos.offsetY = -(y/scale)
  217. })
  218. // 平移时设置鼠标样式
  219. mainContainerWrap.style.cursor = "grab";
  220. mainContainerWrap.addEventListener("mousedown", function wrapMousedown() {
  221. this.style.cursor = "grabbing";
  222. mainContainerWrap.addEventListener("mouseout", function wrapMouseout() {
  223. this.style.cursor = "grab";
  224. });
  225. });
  226. mainContainerWrap.addEventListener("mouseup", function wrapMouseup() {
  227. this.style.cursor = "grab";
  228. });
  229. },
  230. setNodeName(nodeId, name) {
  231. this.data.nodeList.some((v) => {
  232. if(v.id === nodeId) {
  233. v.nodeName = name
  234. return true
  235. }else {
  236. return false
  237. }
  238. })
  239. },
  240. //删除节点
  241. deleteNode(node) {
  242. this.data.nodeList.some((v,index) => {
  243. if(v.id === node.id) {
  244. this.data.nodeList.splice(index, 1)
  245. this.jsPlumb.remove(v.id)
  246. return true
  247. }else {
  248. return false
  249. }
  250. })
  251. },
  252. //更改连线状态
  253. changeLineState(nodeId, val) {
  254. console.log(val)
  255. let lines = this.jsPlumb.getAllConnections()
  256. lines.forEach(line => {
  257. if(line.targetId === nodeId || line.sourceId === nodeId) {
  258. if(val) {
  259. line.canvas.classList.add('active')
  260. }else {
  261. line.canvas.classList.remove('active')
  262. }
  263. }
  264. })
  265. },
  266. //初始化节点位置 (以便对齐,居中)
  267. fixNodesPosition() {
  268. if(this.data.nodeList && this.$refs.flowWrap) {
  269. const nodeWidth = 120
  270. const nodeHeight = 40
  271. let wrapInfo = this.$refs.flowWrap.getBoundingClientRect()
  272. let maxLeft = 0, minLeft = wrapInfo.width, maxTop = 0, minTop = wrapInfo.height;
  273. let nodePoint = {
  274. left: 0,
  275. right: 0,
  276. top: 0,
  277. bottom: 0
  278. }
  279. let fixTop = 0, fixLeft = 0;
  280. this.data.nodeList.forEach(el => {
  281. let top = Number(el.top.substring(0, el.top.length -2))
  282. let left = Number(el.left.substring(0, el.left.length -2))
  283. maxLeft = left > maxLeft ? left : maxLeft
  284. minLeft = left < minLeft ? left : minLeft
  285. maxTop = top > maxTop ? top : maxTop
  286. minTop = top < minTop ? top : minTop
  287. })
  288. nodePoint.left = minLeft
  289. nodePoint.right = wrapInfo.width - maxLeft - nodeWidth
  290. nodePoint.top = minTop
  291. nodePoint.bottom = wrapInfo.height - maxTop - nodeHeight;
  292. fixTop = nodePoint.top !== nodePoint.bottom ? (nodePoint.bottom - nodePoint.top) / 2 : 0;
  293. fixLeft = nodePoint.left !== nodePoint.right ? (nodePoint.right - nodePoint.left) / 2 : 0;
  294. this.data.nodeList.map(el => {
  295. let top = Number(el.top.substring(0, el.top.length - 2)) + fixTop;
  296. let left = Number(el.left.substring(0, el.left.length - 2)) + fixLeft;
  297. el.top = (Math.round(top/20))* 20 + 'px'
  298. el.left = (Math.round(left/20))*20 + 'px'
  299. })
  300. }
  301. },
  302. }
  303. export default methods;