all files / bpmn-js/lib/features/editor-actions/ BpmnEditorActions.js

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