AppendShapeHandler.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { some } from 'min-dash';
  2. /**
  3. * A handler that implements reversible appending of shapes
  4. * to a source shape.
  5. *
  6. * @param {canvas} Canvas
  7. * @param {elementFactory} ElementFactory
  8. * @param {modeling} Modeling
  9. */
  10. export default function AppendShapeHandler(modeling) {
  11. this._modeling = modeling;
  12. }
  13. AppendShapeHandler.$inject = [ 'modeling' ];
  14. // api //////////////////////
  15. /**
  16. * Creates a new shape
  17. *
  18. * @param {Object} context
  19. * @param {ElementDescriptor} context.shape the new shape
  20. * @param {ElementDescriptor} context.source the source object
  21. * @param {ElementDescriptor} context.parent the parent object
  22. * @param {Point} context.position position of the new element
  23. */
  24. AppendShapeHandler.prototype.preExecute = function(context) {
  25. var source = context.source;
  26. if (!source) {
  27. throw new Error('source required');
  28. }
  29. var target = context.target || source.parent,
  30. shape = context.shape,
  31. hints = context.hints || {};
  32. shape = context.shape =
  33. this._modeling.createShape(
  34. shape,
  35. context.position,
  36. target, { attach: hints.attach });
  37. context.shape = shape;
  38. };
  39. AppendShapeHandler.prototype.postExecute = function(context) {
  40. var hints = context.hints || {};
  41. if (!existsConnection(context.source, context.shape)) {
  42. // create connection
  43. if (hints.connectionTarget === context.source) {
  44. this._modeling.connect(context.shape, context.source, context.connection);
  45. } else {
  46. this._modeling.connect(context.source, context.shape, context.connection);
  47. }
  48. }
  49. };
  50. function existsConnection(source, target) {
  51. return some(source.outgoing, function(c) {
  52. return c.target === target;
  53. });
  54. }