Replace.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var round = Math.round;
  2. /**
  3. * Service that allow replacing of elements.
  4. */
  5. export default function Replace(modeling) {
  6. this._modeling = modeling;
  7. }
  8. Replace.$inject = [ 'modeling' ];
  9. /**
  10. * @param {Element} oldElement - Element to be replaced
  11. * @param {Object} newElementData - Containing information about the new Element, for example height, width, type.
  12. * @param {Object} options - Custom options that will be attached to the context. It can be used to inject data
  13. * that is needed in the command chain. For example it could be used in
  14. * eventbus.on('commandStack.shape.replace.postExecute') to change shape attributes after
  15. * shape creation.
  16. */
  17. Replace.prototype.replaceElement = function(oldElement, newElementData, options) {
  18. var modeling = this._modeling;
  19. var newElement = null;
  20. if (oldElement.waypoints) {
  21. // TODO
  22. // modeling.replaceConnection
  23. } else {
  24. // set center of element for modeling API
  25. // if no new width / height is given use old elements size
  26. newElementData.x = round(oldElement.x + (newElementData.width || oldElement.width) / 2);
  27. newElementData.y = round(oldElement.y + (newElementData.height || oldElement.height) / 2);
  28. newElement = modeling.replaceShape(oldElement, newElementData, options);
  29. }
  30. return newElement;
  31. };