Replace.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. assign
  3. } from 'min-dash';
  4. var round = Math.round;
  5. /**
  6. * Service that allow replacing of elements.
  7. */
  8. export default function Replace(modeling) {
  9. this._modeling = modeling;
  10. }
  11. Replace.$inject = [ 'modeling' ];
  12. /**
  13. * @param {Element} oldElement - Element to be replaced
  14. * @param {Object} newElementData - Containing information about the new element,
  15. * for example the new bounds and type.
  16. * @param {Object} options - Custom options that will be attached to the context. It can be used to inject data
  17. * that is needed in the command chain. For example it could be used in
  18. * eventbus.on('commandStack.shape.replace.postExecute') to change shape attributes after
  19. * shape creation.
  20. */
  21. Replace.prototype.replaceElement = function(oldElement, newElementData, options) {
  22. if (oldElement.waypoints) {
  23. // TODO(nikku): we do not replace connections, yet
  24. return null;
  25. }
  26. var modeling = this._modeling;
  27. var width = newElementData.width || oldElement.width,
  28. height = newElementData.height || oldElement.height,
  29. x = newElementData.x || oldElement.x,
  30. y = newElementData.y || oldElement.y,
  31. centerX = round(x + width / 2),
  32. centerY = round(y + height / 2);
  33. // modeling API requires center coordinates,
  34. // account for that when handling shape bounds
  35. return modeling.replaceShape(
  36. oldElement,
  37. assign(
  38. {},
  39. newElementData,
  40. {
  41. x: centerX,
  42. y: centerY,
  43. width: width,
  44. height: height
  45. }
  46. ),
  47. options
  48. );
  49. };