ResizeShapeHandler.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. assign,
  3. forEach
  4. } from 'min-dash';
  5. import {
  6. getResizedSourceAnchor,
  7. getResizedTargetAnchor
  8. } from './helper/AnchorsHelper';
  9. /**
  10. * A handler that implements reversible resizing of shapes.
  11. *
  12. * @param {Modeling} modeling
  13. */
  14. export default function ResizeShapeHandler(modeling) {
  15. this._modeling = modeling;
  16. }
  17. ResizeShapeHandler.$inject = [ 'modeling' ];
  18. /**
  19. * {
  20. * shape: {....}
  21. * newBounds: {
  22. * width: 20,
  23. * height: 40,
  24. * x: 5,
  25. * y: 10
  26. * }
  27. *
  28. * }
  29. */
  30. ResizeShapeHandler.prototype.execute = function(context) {
  31. var shape = context.shape,
  32. newBounds = context.newBounds,
  33. minBounds = context.minBounds;
  34. if (newBounds.x === undefined || newBounds.y === undefined ||
  35. newBounds.width === undefined || newBounds.height === undefined) {
  36. throw new Error('newBounds must have {x, y, width, height} properties');
  37. }
  38. if (minBounds && (newBounds.width < minBounds.width
  39. || newBounds.height < minBounds.height)) {
  40. throw new Error('width and height cannot be less than minimum height and width');
  41. } else if (!minBounds
  42. && newBounds.width < 10 || newBounds.height < 10) {
  43. throw new Error('width and height cannot be less than 10px');
  44. }
  45. // save old bbox in context
  46. context.oldBounds = {
  47. width: shape.width,
  48. height: shape.height,
  49. x: shape.x,
  50. y: shape.y
  51. };
  52. // update shape
  53. assign(shape, {
  54. width: newBounds.width,
  55. height: newBounds.height,
  56. x: newBounds.x,
  57. y: newBounds.y
  58. });
  59. return shape;
  60. };
  61. ResizeShapeHandler.prototype.postExecute = function(context) {
  62. var modeling = this._modeling;
  63. var shape = context.shape,
  64. oldBounds = context.oldBounds,
  65. hints = context.hints || {};
  66. if (hints.layout === false) {
  67. return;
  68. }
  69. forEach(shape.incoming, function(c) {
  70. modeling.layoutConnection(c, {
  71. connectionEnd: getResizedTargetAnchor(c, shape, oldBounds)
  72. });
  73. });
  74. forEach(shape.outgoing, function(c) {
  75. modeling.layoutConnection(c, {
  76. connectionStart: getResizedSourceAnchor(c, shape, oldBounds)
  77. });
  78. });
  79. };
  80. ResizeShapeHandler.prototype.revert = function(context) {
  81. var shape = context.shape,
  82. oldBounds = context.oldBounds;
  83. // restore previous bbox
  84. assign(shape, {
  85. width: oldBounds.width,
  86. height: oldBounds.height,
  87. x: oldBounds.x,
  88. y: oldBounds.y
  89. });
  90. return shape;
  91. };