ImportDockingFix.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. getMid
  3. } from 'diagram-js/lib/layout/LayoutUtil';
  4. import lineIntersect from './util/LineIntersect';
  5. /**
  6. * Fix broken dockings after DI imports.
  7. *
  8. * @param {EventBus} eventBus
  9. */
  10. export default function ImportDockingFix(eventBus) {
  11. function adjustDocking(startPoint, nextPoint, elementMid) {
  12. var elementTop = {
  13. x: elementMid.x,
  14. y: elementMid.y - 50
  15. };
  16. var elementLeft = {
  17. x: elementMid.x - 50,
  18. y: elementMid.y
  19. };
  20. var verticalIntersect = lineIntersect(startPoint, nextPoint, elementMid, elementTop),
  21. horizontalIntersect = lineIntersect(startPoint, nextPoint, elementMid, elementLeft);
  22. // original is horizontal or vertical center cross intersection
  23. var centerIntersect;
  24. if (verticalIntersect && horizontalIntersect) {
  25. if (getDistance(verticalIntersect, elementMid) > getDistance(horizontalIntersect, elementMid)) {
  26. centerIntersect = horizontalIntersect;
  27. } else {
  28. centerIntersect = verticalIntersect;
  29. }
  30. } else {
  31. centerIntersect = verticalIntersect || horizontalIntersect;
  32. }
  33. startPoint.original = centerIntersect;
  34. }
  35. function fixDockings(connection) {
  36. var waypoints = connection.waypoints;
  37. adjustDocking(
  38. waypoints[0],
  39. waypoints[1],
  40. getMid(connection.source)
  41. );
  42. adjustDocking(
  43. waypoints[waypoints.length - 1],
  44. waypoints[waypoints.length - 2],
  45. getMid(connection.target)
  46. );
  47. }
  48. eventBus.on('bpmnElement.added', function(e) {
  49. var element = e.element;
  50. if (element.waypoints) {
  51. fixDockings(element);
  52. }
  53. });
  54. }
  55. ImportDockingFix.$inject = [
  56. 'eventBus'
  57. ];
  58. // helpers //////////////////////
  59. function getDistance(p1, p2) {
  60. return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
  61. }