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

62.16% Statements 23/37
42.31% Branches 11/26
83.33% Functions 5/6
62.16% Lines 23/37

    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

    1353×

    21×
    21×
    21×
    21×
    21×

    21×
    21×

    21×
    1×

    20×

    1353×

    15×
    15×

    1353×
    34×

    34×

    34×
    17×

    34×
    20×

    1353×

    1×

     import {
      hasPrimaryModifier
    } from '../../util/Mouse';

    import {
      find, isArray
    } from 'min-dash';

    export default function SelectionBehavior(
        eventBus, selection, canvas,
        elementRegistry) {

      eventBus.on('create.end', 500, function(e) {

        var context = e.context,
            canExecute = context.canExecute,
            elements = context.elements,
            hints = context.hints || {},
            autoSelect = hints.autoSelect;

        // select elements after they have been created
        Eif (canExecute) {
          Iif (autoSelect === false) {

            // select no elements
            return;
          }

          if (isArray(autoSelect)) {
            selection.select(autoSelect);
          } else {

            // select all elements by default
            selection.select(elements);
          }
        }
      });

      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'
    ];