RemoveElementBehavior.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import inherits from 'inherits';
  2. import { is } from '../../../util/ModelUtil';
  3. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  4. import lineIntersect from './util/LineIntersect';
  5. export default function RemoveElementBehavior(eventBus, bpmnRules, modeling) {
  6. CommandInterceptor.call(this, eventBus);
  7. /**
  8. * Combine sequence flows when deleting an element
  9. * if there is one incoming and one outgoing
  10. * sequence flow
  11. */
  12. this.preExecute('shape.delete', function(e) {
  13. var shape = e.context.shape;
  14. // only handle [a] -> [shape] -> [b] patterns
  15. if (shape.incoming.length !== 1 || shape.outgoing.length !== 1) {
  16. return;
  17. }
  18. var inConnection = shape.incoming[0],
  19. outConnection = shape.outgoing[0];
  20. // only handle sequence flows
  21. if (!is(inConnection, 'bpmn:SequenceFlow') || !is(outConnection, 'bpmn:SequenceFlow')) {
  22. return;
  23. }
  24. if (bpmnRules.canConnect(inConnection.source, outConnection.target, inConnection)) {
  25. // compute new, combined waypoints
  26. var newWaypoints = getNewWaypoints(inConnection.waypoints, outConnection.waypoints);
  27. modeling.reconnectEnd(inConnection, outConnection.target, newWaypoints);
  28. }
  29. });
  30. }
  31. inherits(RemoveElementBehavior, CommandInterceptor);
  32. RemoveElementBehavior.$inject = [
  33. 'eventBus',
  34. 'bpmnRules',
  35. 'modeling'
  36. ];
  37. // helpers //////////////////////
  38. function getDocking(point) {
  39. return point.original || point;
  40. }
  41. function getNewWaypoints(inWaypoints, outWaypoints) {
  42. var intersection = lineIntersect(
  43. getDocking(inWaypoints[inWaypoints.length - 2]),
  44. getDocking(inWaypoints[inWaypoints.length - 1]),
  45. getDocking(outWaypoints[1]),
  46. getDocking(outWaypoints[0]));
  47. if (intersection) {
  48. return [].concat(
  49. inWaypoints.slice(0, inWaypoints.length - 1),
  50. [ intersection ],
  51. outWaypoints.slice(1));
  52. } else {
  53. return [
  54. getDocking(inWaypoints[0]),
  55. getDocking(outWaypoints[outWaypoints.length - 1])
  56. ];
  57. }
  58. }