methods.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import panzoom from 'panzoom'
  2. import { GenNonDuplicateID } from '../until'
  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. stop: params => {
  75. this.auxiliaryLine.isShowXLine = false
  76. this.auxiliaryLine.isShowYLine = false
  77. this.changeNodePosition(nodeId, params.pos)
  78. }
  79. })
  80. },
  81. // 移动节点时,动态显示对齐线
  82. alignForLine (nodeId, position) {
  83. // eslint-disable-next-line one-var
  84. let showXLine = false,
  85. showYLine = false
  86. this.data.nodeList.some(el => {
  87. if (el.id !== nodeId && el.left === position[0] + 'px') {
  88. this.auxiliaryLinePos.x = position[0] + 60
  89. showYLine = true
  90. }
  91. if (el.id !== nodeId && el.top === position[1] + 'px') {
  92. this.auxiliaryLinePos.y = position[1] + 20 + 12.5 // 12.5是节点下方操作人显示栏的高度1/2
  93. showXLine = true
  94. }
  95. })
  96. this.auxiliaryLine.isShowYLine = showYLine
  97. this.auxiliaryLine.isShowXLine = showXLine
  98. },
  99. changeNodePosition (nodeId, pos) {
  100. this.data.nodeList.some(v => {
  101. if (nodeId === v.id) {
  102. v.left = pos[0] + 'px'
  103. v.top = pos[1] + 'px'
  104. return true
  105. } else {
  106. return false
  107. }
  108. })
  109. },
  110. drag (ele, item) {
  111. this.currentItem = item
  112. },
  113. drop (event) {
  114. const containerRect = this.jsPlumb.getContainer().getBoundingClientRect()
  115. const scale = this.getScale()
  116. let left = (event.pageX - containerRect.left - 60) / scale
  117. let top = (event.pageY - containerRect.top - 20) / scale
  118. var temp = {
  119. ...this.currentItem,
  120. id: GenNonDuplicateID(8),
  121. top: Math.round(top / 20) * 20 + 'px',
  122. left: Math.round(left / 20) * 20 + 'px'
  123. }
  124. this.addNode(temp)
  125. },
  126. addLine (line) {
  127. let from = line.source.id
  128. let to = line.target.id
  129. this.data.lineList.push({
  130. from: from,
  131. to: to,
  132. label: '连线名称',
  133. id: GenNonDuplicateID(8),
  134. remark: ''
  135. })
  136. },
  137. confirmDelLine (line) {
  138. // this.$Modal.confirm({
  139. // title: '删除连线',
  140. // content: "<p>确认删除该连线?</p>",
  141. // onOk: () => {
  142. // this.jsPlumb.deleteConnection(line)
  143. // }
  144. // })
  145. this.$confirm('确认删除该连线?', '删除连线', {
  146. confirmButtonText: '确定',
  147. cancelButtonText: '取消',
  148. type: 'warning'
  149. })
  150. .then(() => {
  151. this.jsPlumb.deleteConnection(line)
  152. })
  153. .catch(() => {
  154. this.$message({
  155. type: 'info',
  156. message: '已取消删除'
  157. })
  158. })
  159. },
  160. deleLine (line) {
  161. this.data.lineList.forEach((item, index) => {
  162. if (item.from === line.sourceId && item.to === line.targetId) {
  163. this.data.lineList.splice(index, 1)
  164. }
  165. })
  166. },
  167. // dragover默认事件就是不触发drag事件,取消默认事件后,才会触发drag事件
  168. allowDrop (event) {
  169. event.preventDefault()
  170. },
  171. getScale () {
  172. let scale1
  173. if (this.jsPlumb.pan) {
  174. const { scale } = this.jsPlumb.pan.getTransform()
  175. scale1 = scale
  176. } else {
  177. const matrix = window.getComputedStyle(this.jsPlumb.getContainer())
  178. .transform
  179. scale1 = matrix.split(', ')[3] * 1
  180. }
  181. this.jsPlumb.setZoom(scale1)
  182. return scale1
  183. },
  184. // 添加新的节点
  185. addNode (temp) {
  186. this.data.nodeList.push(temp)
  187. this.$nextTick(() => {
  188. this.jsPlumb.makeSource(temp.id, this.jsplumbSourceOptions)
  189. this.jsPlumb.makeTarget(temp.id, this.jsplumbTargetOptions)
  190. this.draggableNode(temp.id)
  191. })
  192. },
  193. initPanZoom () {
  194. const mainContainer = this.jsPlumb.getContainer()
  195. const mainContainerWrap = mainContainer.parentNode
  196. const pan = panzoom(mainContainer, {
  197. smoothScroll: false,
  198. bounds: true,
  199. // autocenter: true,
  200. zoomDoubleClickSpeed: 1,
  201. minZoom: 0.5,
  202. maxZoom: 2,
  203. // 设置滚动缩放的组合键,默认不需要组合键
  204. beforeWheel: e => {
  205. console.log(e)
  206. // let shouldIgnore = !e.ctrlKey
  207. // return shouldIgnore
  208. },
  209. beforeMouseDown: function (e) {
  210. // allow mouse-down panning only if altKey is down. Otherwise - ignore
  211. var shouldIgnore = e.ctrlKey
  212. return shouldIgnore
  213. }
  214. })
  215. this.jsPlumb.mainContainerWrap = mainContainerWrap
  216. this.jsPlumb.pan = pan
  217. // 缩放时设置jsPlumb的缩放比率
  218. pan.on('zoom', e => {
  219. const { x, y, scale } = e.getTransform()
  220. this.jsPlumb.setZoom(scale)
  221. // 根据缩放比例,缩放对齐辅助线长度和位置
  222. this.auxiliaryLinePos.width = (1 / scale) * 100 + '%'
  223. this.auxiliaryLinePos.height = (1 / scale) * 100 + '%'
  224. this.auxiliaryLinePos.offsetX = -(x / scale)
  225. this.auxiliaryLinePos.offsetY = -(y / scale)
  226. })
  227. pan.on('panend', e => {
  228. const { x, y, scale } = e.getTransform()
  229. this.auxiliaryLinePos.width = (1 / scale) * 100 + '%'
  230. this.auxiliaryLinePos.height = (1 / scale) * 100 + '%'
  231. this.auxiliaryLinePos.offsetX = -(x / scale)
  232. this.auxiliaryLinePos.offsetY = -(y / scale)
  233. })
  234. // 平移时设置鼠标样式
  235. mainContainerWrap.style.cursor = 'grab'
  236. mainContainerWrap.addEventListener('mousedown', function wrapMousedown () {
  237. this.style.cursor = 'grabbing'
  238. mainContainerWrap.addEventListener('mouseout', function wrapMouseout () {
  239. this.style.cursor = 'grab'
  240. })
  241. })
  242. mainContainerWrap.addEventListener('mouseup', function wrapMouseup () {
  243. this.style.cursor = 'grab'
  244. })
  245. },
  246. setNode (nodeId, node) {
  247. this.data.nodeList.some(v => {
  248. if (v.id === nodeId) {
  249. for (var key in node) {
  250. v[key] = node[key]
  251. }
  252. // v.nodeName = node.nodeName
  253. return true
  254. } else {
  255. return false
  256. }
  257. })
  258. },
  259. // 删除节点
  260. deleteNode (node) {
  261. this.data.nodeList.some((v, index) => {
  262. if (v.id === node.id) {
  263. this.data.nodeList.splice(index, 1)
  264. this.jsPlumb.remove(v.id)
  265. return true
  266. } else {
  267. return false
  268. }
  269. })
  270. },
  271. // 更改连线状态
  272. changeLineState (nodeId, val) {
  273. console.log(val)
  274. let lines = this.jsPlumb.getAllConnections()
  275. lines.forEach(line => {
  276. if (line.targetId === nodeId || line.sourceId === nodeId) {
  277. if (val) {
  278. line.canvas.classList.add('active')
  279. } else {
  280. line.canvas.classList.remove('active')
  281. }
  282. }
  283. })
  284. },
  285. // 初始化节点位置 (以便对齐,居中)
  286. fixNodesPosition () {
  287. if (this.data.nodeList && this.$refs.flowWrap) {
  288. const nodeWidth = 120
  289. const nodeHeight = 40
  290. let wrapInfo = this.$refs.flowWrap.getBoundingClientRect()
  291. // eslint-disable-next-line one-var
  292. let maxLeft = 0,
  293. minLeft = wrapInfo.width,
  294. maxTop = 0,
  295. minTop = wrapInfo.height
  296. let nodePoint = {
  297. left: 0,
  298. right: 0,
  299. top: 0,
  300. bottom: 0
  301. }
  302. // eslint-disable-next-line one-var
  303. let fixTop = 0,
  304. fixLeft = 0
  305. this.data.nodeList.forEach(el => {
  306. let top = Number(el.top.substring(0, el.top.length - 2))
  307. let left = Number(el.left.substring(0, el.left.length - 2))
  308. maxLeft = left > maxLeft ? left : maxLeft
  309. minLeft = left < minLeft ? left : minLeft
  310. maxTop = top > maxTop ? top : maxTop
  311. minTop = top < minTop ? top : minTop
  312. })
  313. nodePoint.left = minLeft
  314. nodePoint.right = wrapInfo.width - maxLeft - nodeWidth
  315. nodePoint.top = minTop
  316. nodePoint.bottom = wrapInfo.height - maxTop - nodeHeight
  317. fixTop =
  318. nodePoint.top !== nodePoint.bottom
  319. ? (nodePoint.bottom - nodePoint.top) / 2
  320. : 0
  321. fixLeft =
  322. nodePoint.left !== nodePoint.right
  323. ? (nodePoint.right - nodePoint.left) / 2
  324. : 0
  325. this.data.nodeList.map(el => {
  326. let top = Number(el.top.substring(0, el.top.length - 2)) + fixTop
  327. let left = Number(el.left.substring(0, el.left.length - 2)) + fixLeft
  328. el.top = Math.round(top / 20) * 20 + 'px'
  329. el.left = Math.round(left / 20) * 20 + 'px'
  330. })
  331. }
  332. }
  333. }
  334. export default methods