AutoPlace.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. asTRBL,
  3. getMid
  4. } from '../../layout/LayoutUtil';
  5. import { DEFAULT_DISTANCE } from './AutoPlaceUtil';
  6. var LOW_PRIORITY = 100;
  7. /**
  8. * A service that places elements connected to existing ones
  9. * to an appropriate position in an _automated_ fashion.
  10. *
  11. * @param {EventBus} eventBus
  12. * @param {Modeling} modeling
  13. */
  14. export default function AutoPlace(eventBus, modeling, canvas) {
  15. eventBus.on('autoPlace', LOW_PRIORITY, function(context) {
  16. var shape = context.shape,
  17. source = context.source;
  18. return getNewShapePosition(source, shape);
  19. });
  20. eventBus.on('autoPlace.end', function(event) {
  21. canvas.scrollToElement(event.shape);
  22. });
  23. /**
  24. * Append shape to source at appropriate position.
  25. *
  26. * @param {djs.model.Shape} source
  27. * @param {djs.model.Shape} shape
  28. *
  29. * @return {djs.model.Shape} appended shape
  30. */
  31. this.append = function(source, shape, hints) {
  32. eventBus.fire('autoPlace.start', {
  33. source: source,
  34. shape: shape
  35. });
  36. // allow others to provide the position
  37. var position = eventBus.fire('autoPlace', {
  38. source: source,
  39. shape: shape
  40. });
  41. var newShape = modeling.appendShape(source, shape, position, source.parent, hints);
  42. eventBus.fire('autoPlace.end', {
  43. source: source,
  44. shape: newShape
  45. });
  46. return newShape;
  47. };
  48. }
  49. AutoPlace.$inject = [
  50. 'eventBus',
  51. 'modeling',
  52. 'canvas'
  53. ];
  54. // helpers //////////
  55. /**
  56. * Find the new position for the target element to
  57. * connect to source.
  58. *
  59. * @param {djs.model.Shape} source
  60. * @param {djs.model.Shape} element
  61. * @param {Object} [hints]
  62. * @param {Object} [hints.defaultDistance]
  63. *
  64. * @returns {Point}
  65. */
  66. function getNewShapePosition(source, element, hints) {
  67. if (!hints) {
  68. hints = {};
  69. }
  70. var distance = hints.defaultDistance || DEFAULT_DISTANCE;
  71. var sourceMid = getMid(source),
  72. sourceTrbl = asTRBL(source);
  73. // simply put element right next to source
  74. return {
  75. x: sourceTrbl.right + distance + element.width / 2,
  76. y: sourceMid.y
  77. };
  78. }