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

87.5% Statements 28/32
70% Branches 7/10
87.5% Functions 7/8
87.5% Lines 28/32

    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

    1353×

    1353×

    1353×

    1353×
    821×
    821×

    1353×
    1330×

    1×

    1×
    821×

    821×

    821×
    22×

    22×

    22×

    1×
    339×

    1×
    129×

    1×
    1792×
    1792×

    1792×
    1660×

    1792×

    1792×

    1792×

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

    /**
     * A service that offers the current selection in a diagram.
     * Offers the api to control the selection, too.
     *
     * @class
     *
     * @param {EventBus} eventBus the event bus
     */
    export default function Selection(eventBus) {

      this._eventBus = eventBus;

      this._selectedElements = [];

      var self = this;

      eventBus.on([ 'shape.remove', 'connection.remove' ], function(e) {
        var element = e.element;
        self.deselect(element);
      });

      eventBus.on([ 'diagram.clear' ], function(e) {
        self.select(null);
      });
    }

    Selection.$inject = [ 'eventBus' ];

    Selection.prototype.deselect = function(element) {
      var selectedElements = this._selectedElements;

      var idx = selectedElements.indexOf(element);

      if (idx !== -1) {
        var oldSelection = selectedElements.slice();

        selectedElements.splice(idx, 1);

        this._eventBus.fire('selection.changed', { oldSelection: oldSelection, newSelection: selectedElements });
      }
    };

    Selection.prototype.get = function() {
      return this._selectedElements;
    };

    Selection.prototype.isSelected = function(element) {
      return this._selectedElements.indexOf(element) !== -1;
    };

    /**
     * This method selects one or more elements on the diagram.
     *
     * By passing an additional add parameter you can decide whether or not the element(s)
     * should be added to the already existing selection or not.
     *
     * @method Selection#select
     *
     * @param  {Object|Object[]} elements element or array of elements to be selected
     * @param  {boolean} [add] whether the element(s) should be appended to the current selection, defaults to false
     */
    Selection.prototype.select = function(elements, add) {
      var selectedElements = this._selectedElements,
          oldSelection = selectedElements.slice();

      if (!isArray(elements)) {
        elements = elements ? [ elements ] : [];
      }

      // selection may be cleared by passing an empty array or null
      // to the method
      Iif (add) {
        forEach(elements, function(element) {
          if (selectedElements.indexOf(element) !== -1) {
            // already selected
            return;
          } else {
            selectedElements.push(element);
          }
        });
      } else {
        this._selectedElements = selectedElements = elements.slice();
      }

      this._eventBus.fire('selection.changed', { oldSelection: oldSelection, newSelection: selectedElements });
    };