dragPanelItemAddNode.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable */
  2. import editorStyle from "../util/defaultStyle";
  3. import { getShapeName } from '../util/clazz';
  4. export default function(G6){
  5. G6.registerBehavior('dragPanelItemAddNode', {
  6. getDefaultCfg() {
  7. return {
  8. };
  9. },
  10. getEvents() {
  11. return {
  12. 'canvas:mousemove': 'onMouseMove',
  13. 'canvas:mouseup': 'onMouseUp',
  14. 'canvas:mouseleave': 'onMouseLeave',
  15. }
  16. },
  17. onMouseMove(e){
  18. if(this.graph.get('addNodeDragging')){
  19. let delegateShape = this.graph.get('addDelegateShape');
  20. const addModel = this.graph.get('addModel');
  21. const width = parseInt(addModel.size.split('*')[0]);
  22. const height = parseInt(addModel.size.split('*')[1]);
  23. const point = this.graph.getPointByClient(e.x,e.y);
  24. const x = point.x;
  25. const y = point.y;
  26. if (!delegateShape) {
  27. const parent = this.graph.get('group');
  28. delegateShape = parent.addShape('rect', {
  29. attrs: {
  30. width,
  31. height,
  32. x: x - width / 2,
  33. y: y - height / 2,
  34. ...editorStyle.nodeDelegationStyle,
  35. }
  36. });
  37. delegateShape.set('capture', false);
  38. this.graph.set('addDelegateShape', delegateShape);
  39. }
  40. delegateShape.attr({ x: x - width / 2, y: y - height / 2 });
  41. this.graph.paint();
  42. this.graph.emit('afternodedrag',delegateShape);
  43. }
  44. },
  45. onMouseUp(e){
  46. if (this.graph.get('addNodeDragging')) {
  47. const p = this.graph.getPointByClient(e.clientX, e.clientY);
  48. const subProcessNode = this.graph.find('node', (node) => {
  49. if (node.get('model')) {
  50. const clazz = node.get('model').clazz;
  51. if (clazz === 'subProcess') {
  52. const bbox = node.getBBox();
  53. return p.x > bbox.minX && bbox.maxX > p.x
  54. && p.y > bbox.minY && bbox.maxY > p.y;
  55. }
  56. } else {
  57. return false;
  58. }
  59. });
  60. if (subProcessNode) {
  61. if (p.x > 0 && p.y > 0) {
  62. this._addNodeBySubProcess(p, subProcessNode);
  63. }
  64. } else {
  65. if (p.x > 0 && p.y > 0) {
  66. this._addNode(p);
  67. }
  68. }
  69. }
  70. },
  71. _addNodeBySubProcess(p, node) {
  72. if (this.graph.get('addNodeDragging')) {
  73. const addModel = this.graph.get('addModel');
  74. const { clazz = 'userTask' } = addModel;
  75. addModel.shape = getShapeName(clazz);
  76. addModel.size = addModel.size.split("*");
  77. const timestamp = new Date().getTime();
  78. const id = clazz + timestamp;
  79. const bbox = node.getBBox();
  80. const x = p.x - bbox.x - bbox.width / 2;
  81. const y = p.y - bbox.y - bbox.height / 2;
  82. const nodeCfg = {
  83. ...addModel,
  84. x,
  85. y,
  86. id
  87. };
  88. const group = node.getContainer();
  89. const resultModel = group.addNodeModel(node, nodeCfg);
  90. if (this.graph.executeCommand) {
  91. this.graph.executeCommand('update', {
  92. itemId: node.get('id'),
  93. updateModel: resultModel,
  94. });
  95. } else {
  96. this.graph.updateItem(node, resultModel);
  97. }
  98. }
  99. },
  100. onMouseLeave(e){
  101. if (this.graph.get('addNodeDragging')) {
  102. this._clearDelegate();
  103. this.graph.emit('afternodedragend');
  104. }
  105. },
  106. _clearDelegate(){
  107. const delegateShape = this.graph.get('addDelegateShape');
  108. if (delegateShape) {
  109. delegateShape.remove();
  110. this.graph.set('addDelegateShape', null);
  111. this.graph.paint();
  112. }
  113. this.graph.emit('afternodedragend');
  114. },
  115. _addNode(p){
  116. if (this.graph.get('addNodeDragging')) {
  117. const addModel = this.graph.get('addModel');
  118. const { clazz = 'userTask' } = addModel;
  119. //type、shape属性同时存在即兼容之前版本的数据,又兼容g6的升级
  120. addModel.type = getShapeName(clazz);
  121. addModel.shape = getShapeName(clazz);
  122. const timestamp = new Date().getTime();
  123. const id = clazz + timestamp;
  124. const x = p.x;
  125. const y = p.y;
  126. if (this.graph.executeCommand) {
  127. this.graph.executeCommand('add', {
  128. type: 'node',
  129. addModel: {
  130. ...addModel,
  131. x: x,
  132. y: y,
  133. id: id,
  134. },
  135. });
  136. } else {
  137. this.graph.add('node', {
  138. ...addModel,
  139. x: x,
  140. y: y,
  141. id: id,
  142. });
  143. }
  144. this._clearDelegate();
  145. }
  146. }
  147. });
  148. }