methods.js 10 KB

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