CreateConnectionHandler.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export default function CreateConnectionHandler(canvas, layouter) {
  2. this._canvas = canvas;
  3. this._layouter = layouter;
  4. }
  5. CreateConnectionHandler.$inject = [ 'canvas', 'layouter' ];
  6. // api //////////////////////
  7. /**
  8. * Appends a shape to a target shape
  9. *
  10. * @param {Object} context
  11. * @param {djs.element.Base} context.source the source object
  12. * @param {djs.element.Base} context.target the parent object
  13. * @param {Point} context.position position of the new element
  14. */
  15. CreateConnectionHandler.prototype.execute = function(context) {
  16. var connection = context.connection,
  17. source = context.source,
  18. target = context.target,
  19. parent = context.parent,
  20. parentIndex = context.parentIndex,
  21. hints = context.hints;
  22. if (!source || !target) {
  23. throw new Error('source and target required');
  24. }
  25. if (!parent) {
  26. throw new Error('parent required');
  27. }
  28. connection.source = source;
  29. connection.target = target;
  30. if (!connection.waypoints) {
  31. connection.waypoints = this._layouter.layoutConnection(connection, hints);
  32. }
  33. // add connection
  34. this._canvas.addConnection(connection, parent, parentIndex);
  35. return connection;
  36. };
  37. CreateConnectionHandler.prototype.revert = function(context) {
  38. var connection = context.connection;
  39. this._canvas.removeConnection(connection);
  40. connection.source = null;
  41. connection.target = null;
  42. return connection;
  43. };