all files / Github/diagram-js/lib/features/selection/ SelectionBehavior.js

53.57% Statements 15/28
35% Branches 7/20
83.33% Functions 5/6
53.57% Lines 15/28
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                          1277×       18× 18×       1277×       15× 15×       1277× 29×   29×       29× 17×     29× 15×         1277×                                                                          
import {
  hasPrimaryModifier
} from '../../util/Mouse';
 
import {
  find
} from 'min-dash';
 
 
export default function SelectionBehavior(
    eventBus, selection, canvas,
    elementRegistry) {
 
  eventBus.on('create.end', 500, function(e) {
 
    // select the created shape after a
    // successful create operation
    Eif (e.context.canExecute) {
      selection.select(e.context.shape);
    }
  });
 
  eventBus.on('connect.end', 500, function(e) {
 
    // select the connect end target
    // after a connect operation
    Eif (e.context.canExecute && e.context.target) {
      selection.select(e.context.target);
    }
  });
 
  eventBus.on('shape.move.end', 500, function(e) {
    var previousSelection = e.previousSelection || [];
 
    var shape = elementRegistry.get(e.context.shape.id);
 
    // make sure at least the main moved element is being
    // selected after a move operation
    var inSelection = find(previousSelection, function(selectedShape) {
      return shape.id === selectedShape.id;
    });
 
    if (!inSelection) {
      selection.select(shape);
    }
  });
 
  // Shift + click selection
  eventBus.on('element.click', function(event) {
 
    var element = event.element;
 
    // do not select the root element
    // or connections
    if (element === canvas.getRootElement()) {
      element = null;
    }
 
    var isSelected = selection.isSelected(element),
        isMultiSelect = selection.get().length > 1;
 
    // mouse-event: SELECTION_KEY
    var add = hasPrimaryModifier(event);
 
    // select OR deselect element in multi selection
    if (isSelected && isMultiSelect) {
      if (add) {
        return selection.deselect(element);
      } else {
        return selection.select(element);
      }
    } else
    if (!isSelected) {
      selection.select(element, add);
    } else {
      selection.deselect(element);
    }
  });
}
 
SelectionBehavior.$inject = [
  'eventBus',
  'selection',
  'canvas',
  'elementRegistry'
];