BpmnEditorActions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import inherits from 'inherits';
  2. import EditorActions from 'diagram-js/lib/features/editor-actions/EditorActions';
  3. import { filter } from 'min-dash';
  4. import { is } from '../../util/ModelUtil';
  5. import {
  6. getBBox
  7. } from 'diagram-js/lib/util/Elements';
  8. /**
  9. * Registers and executes BPMN specific editor actions.
  10. *
  11. * @param {Injector} injector
  12. */
  13. export default function BpmnEditorActions(injector) {
  14. injector.invoke(EditorActions, this);
  15. }
  16. inherits(BpmnEditorActions, EditorActions);
  17. BpmnEditorActions.$inject = [
  18. 'injector'
  19. ];
  20. /**
  21. * Register default actions.
  22. *
  23. * @param {Injector} injector
  24. */
  25. BpmnEditorActions.prototype._registerDefaultActions = function(injector) {
  26. // (0) invoke super method
  27. EditorActions.prototype._registerDefaultActions.call(this, injector);
  28. // (1) retrieve optional components to integrate with
  29. var canvas = injector.get('canvas', false);
  30. var elementRegistry = injector.get('elementRegistry', false);
  31. var selection = injector.get('selection', false);
  32. var spaceTool = injector.get('spaceTool', false);
  33. var lassoTool = injector.get('lassoTool', false);
  34. var handTool = injector.get('handTool', false);
  35. var globalConnect = injector.get('globalConnect', false);
  36. var distributeElements = injector.get('distributeElements', false);
  37. var alignElements = injector.get('alignElements', false);
  38. var directEditing = injector.get('directEditing', false);
  39. var searchPad = injector.get('searchPad', false);
  40. var modeling = injector.get('modeling', false);
  41. // (2) check components and register actions
  42. if (canvas && elementRegistry && selection) {
  43. this._registerAction('selectElements', function() {
  44. // select all elements except for the invisible
  45. // root element
  46. var rootElement = canvas.getRootElement();
  47. var elements = elementRegistry.filter(function(element) {
  48. return element !== rootElement;
  49. });
  50. selection.select(elements);
  51. return elements;
  52. });
  53. }
  54. if (spaceTool) {
  55. this._registerAction('spaceTool', function() {
  56. spaceTool.toggle();
  57. });
  58. }
  59. if (lassoTool) {
  60. this._registerAction('lassoTool', function() {
  61. lassoTool.toggle();
  62. });
  63. }
  64. if (handTool) {
  65. this._registerAction('handTool', function() {
  66. handTool.toggle();
  67. });
  68. }
  69. if (globalConnect) {
  70. this._registerAction('globalConnectTool', function() {
  71. globalConnect.toggle();
  72. });
  73. }
  74. if (selection && distributeElements) {
  75. this._registerAction('distributeElements', function(opts) {
  76. var currentSelection = selection.get(),
  77. type = opts.type;
  78. if (currentSelection.length) {
  79. distributeElements.trigger(currentSelection, type);
  80. }
  81. });
  82. }
  83. if (selection && alignElements) {
  84. this._registerAction('alignElements', function(opts) {
  85. var currentSelection = selection.get(),
  86. aligneableElements = [],
  87. type = opts.type;
  88. if (currentSelection.length) {
  89. aligneableElements = filter(currentSelection, function(element) {
  90. return !is(element, 'bpmn:Lane');
  91. });
  92. alignElements.trigger(aligneableElements, type);
  93. }
  94. });
  95. }
  96. if (selection && modeling) {
  97. this._registerAction('setColor', function(opts) {
  98. var currentSelection = selection.get();
  99. if (currentSelection.length) {
  100. modeling.setColor(currentSelection, opts);
  101. }
  102. });
  103. }
  104. if (selection && directEditing) {
  105. this._registerAction('directEditing', function() {
  106. var currentSelection = selection.get();
  107. if (currentSelection.length) {
  108. directEditing.activate(currentSelection[0]);
  109. }
  110. });
  111. }
  112. if (searchPad) {
  113. this._registerAction('find', function() {
  114. searchPad.toggle();
  115. });
  116. }
  117. if (canvas && modeling) {
  118. this._registerAction('moveToOrigin', function() {
  119. var rootElement = canvas.getRootElement(),
  120. boundingBox,
  121. elements;
  122. if (is(rootElement, 'bpmn:Collaboration')) {
  123. elements = elementRegistry.filter(function(element) {
  124. return is(element.parent, 'bpmn:Collaboration');
  125. });
  126. } else {
  127. elements = elementRegistry.filter(function(element) {
  128. return element !== rootElement && !is(element.parent, 'bpmn:SubProcess');
  129. });
  130. }
  131. boundingBox = getBBox(elements);
  132. modeling.moveElements(
  133. elements,
  134. { x: -boundingBox.x, y: -boundingBox.y },
  135. rootElement
  136. );
  137. });
  138. }
  139. };