AutoPlace.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { is } from '../../util/ModelUtil';
  2. import { isAny } from '../modeling/util/ModelingUtil';
  3. import {
  4. getTextAnnotationPosition,
  5. getDataElementPosition,
  6. getFlowNodePosition,
  7. getDefaultPosition
  8. } from './AutoPlaceUtil';
  9. /**
  10. * A service that places elements connected to existing ones
  11. * to an appropriate position in an _automated_ fashion.
  12. *
  13. * @param {EventBus} eventBus
  14. * @param {Modeling} modeling
  15. */
  16. export default function AutoPlace(eventBus, modeling) {
  17. function emit(event, payload) {
  18. return eventBus.fire(event, payload);
  19. }
  20. /**
  21. * Append shape to source at appropriate position.
  22. *
  23. * @param {djs.model.Shape} source
  24. * @param {djs.model.Shape} shape
  25. *
  26. * @return {djs.model.Shape} appended shape
  27. */
  28. this.append = function(source, shape) {
  29. // allow others to provide the position
  30. var position = emit('autoPlace', {
  31. source: source,
  32. shape: shape
  33. });
  34. if (!position) {
  35. position = getNewShapePosition(source, shape);
  36. }
  37. var newShape = modeling.appendShape(source, shape, position, source.parent);
  38. // notify interested parties on new shape placed
  39. emit('autoPlace.end', {
  40. shape: newShape
  41. });
  42. return newShape;
  43. };
  44. }
  45. AutoPlace.$inject = [
  46. 'eventBus',
  47. 'modeling'
  48. ];
  49. // helpers //////////////////////
  50. /**
  51. * Find the new position for the target element to
  52. * connect to source.
  53. *
  54. * @param {djs.model.Shape} source
  55. * @param {djs.model.Shape} element
  56. *
  57. * @return {Point}
  58. */
  59. function getNewShapePosition(source, element) {
  60. if (is(element, 'bpmn:TextAnnotation')) {
  61. return getTextAnnotationPosition(source, element);
  62. }
  63. if (isAny(element, [ 'bpmn:DataObjectReference', 'bpmn:DataStoreReference' ])) {
  64. return getDataElementPosition(source, element);
  65. }
  66. if (is(element, 'bpmn:FlowNode')) {
  67. return getFlowNodePosition(source, element);
  68. }
  69. return getDefaultPosition(source, element);
  70. }