ReplaceShapeHandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { forEach } from 'min-dash';
  2. import {
  3. getResizedSourceAnchor,
  4. getResizedTargetAnchor
  5. } from './helper/AnchorsHelper';
  6. /**
  7. * Replace shape by adding new shape and removing old shape. Incoming and outgoing connections will
  8. * be kept if possible.
  9. *
  10. * @class
  11. * @constructor
  12. *
  13. * @param {Modeling} modeling
  14. * @param {Rules} rules
  15. */
  16. export default function ReplaceShapeHandler(modeling, rules) {
  17. this._modeling = modeling;
  18. this._rules = rules;
  19. }
  20. ReplaceShapeHandler.$inject = [ 'modeling', 'rules' ];
  21. /**
  22. * Add new shape.
  23. *
  24. * @param {Object} context
  25. * @param {djs.model.Shape} context.oldShape
  26. * @param {Object} context.newData
  27. * @param {string} context.newData.type
  28. * @param {number} context.newData.x
  29. * @param {number} context.newData.y
  30. * @param {Object} [hints]
  31. */
  32. ReplaceShapeHandler.prototype.preExecute = function(context) {
  33. var self = this,
  34. modeling = this._modeling,
  35. rules = this._rules;
  36. var oldShape = context.oldShape,
  37. newData = context.newData,
  38. hints = context.hints || {},
  39. newShape;
  40. function canReconnect(source, target, connection) {
  41. return rules.allowed('connection.reconnect', {
  42. connection: connection,
  43. source: source,
  44. target: target
  45. });
  46. }
  47. // (1) add new shape at given position
  48. var position = {
  49. x: newData.x,
  50. y: newData.y
  51. };
  52. var oldBounds = {
  53. x: oldShape.x,
  54. y: oldShape.y,
  55. width: oldShape.width,
  56. height: oldShape.height
  57. };
  58. newShape = context.newShape =
  59. context.newShape ||
  60. self.createShape(newData, position, oldShape.parent, hints);
  61. // (2) update host
  62. if (oldShape.host) {
  63. modeling.updateAttachment(newShape, oldShape.host);
  64. }
  65. // (3) adopt all children from old shape
  66. var children;
  67. if (hints.moveChildren !== false) {
  68. children = oldShape.children.slice();
  69. modeling.moveElements(children, { x: 0, y: 0 }, newShape, hints);
  70. }
  71. // (4) reconnect connections to new shape if possible
  72. var incoming = oldShape.incoming.slice(),
  73. outgoing = oldShape.outgoing.slice();
  74. forEach(incoming, function(connection) {
  75. var source = connection.source,
  76. allowed = canReconnect(source, newShape, connection);
  77. if (allowed) {
  78. self.reconnectEnd(
  79. connection, newShape,
  80. getResizedTargetAnchor(connection, newShape, oldBounds),
  81. hints
  82. );
  83. }
  84. });
  85. forEach(outgoing, function(connection) {
  86. var target = connection.target,
  87. allowed = canReconnect(newShape, target, connection);
  88. if (allowed) {
  89. self.reconnectStart(
  90. connection, newShape,
  91. getResizedSourceAnchor(connection, newShape, oldBounds),
  92. hints
  93. );
  94. }
  95. });
  96. };
  97. /**
  98. * Remove old shape.
  99. */
  100. ReplaceShapeHandler.prototype.postExecute = function(context) {
  101. var oldShape = context.oldShape;
  102. this._modeling.removeShape(oldShape);
  103. };
  104. ReplaceShapeHandler.prototype.execute = function(context) {};
  105. ReplaceShapeHandler.prototype.revert = function(context) {};
  106. ReplaceShapeHandler.prototype.createShape = function(shape, position, target, hints) {
  107. return this._modeling.createShape(shape, position, target, hints);
  108. };
  109. ReplaceShapeHandler.prototype.reconnectStart = function(connection, newSource, dockingPoint, hints) {
  110. this._modeling.reconnectStart(connection, newSource, dockingPoint, hints);
  111. };
  112. ReplaceShapeHandler.prototype.reconnectEnd = function(connection, newTarget, dockingPoint, hints) {
  113. this._modeling.reconnectEnd(connection, newTarget, dockingPoint, hints);
  114. };