# all files / diagram-js/lib/core/ ElementRegistry.js

93.44% Statements 57/61
73.33% Branches 22/30
100% Functions 13/13
93.33% Lines 56/60

    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
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    2061×

    1371×

    1371×

    1×

    1×

    20091×

    20091×

    20091×

    20091×
    1414×

    20091×

    1×
    1050×
    1050×
    1050×

    1050×

    1050×

    1050×
    64×

    1050×

    1×

    165×

    165×

    165×

    165×
    165×

    165×

    165×

    165×

    1×

    31411×
    31411×

    31411×
    31411×

    1×

    1444×

    1444×
    1231×
    883×

    1444×

    1×
    1376×

    1×

    2793×

    2793×
    1376×
    1376×
    1376×

    1376×

    1×
    36663×

    36663×
    36663×

    1×
    20256×

    20256×

     var ELEMENT_ID = 'data-element-id';

    import { attr as svgAttr } from 'tiny-svg';

    /**
     * @class
     *
     * A registry that keeps track of all shapes in the diagram.
     */
    export default function ElementRegistry(eventBus) {
      this._elements = {};

      this._eventBus = eventBus;
    }

    ElementRegistry.$inject = [ 'eventBus' ];

    /**
     * Register a pair of (element, gfx, (secondaryGfx)).
     *
     * @param {djs.model.Base} element
     * @param {SVGElement} gfx
     * @param {SVGElement} [secondaryGfx] optional other element to register, too
     */
    ElementRegistry.prototype.add = function(element, gfx, secondaryGfx) {

      var id = element.id;

      this._validateId(id);

      // associate dom node with element
      svgAttr(gfx, ELEMENT_ID, id);

      if (secondaryGfx) {
        svgAttr(secondaryGfx, ELEMENT_ID, id);
      }

      this._elements[id] = { element: element, gfx: gfx, secondaryGfx: secondaryGfx };
    };

    /**
     * Removes an element from the registry.
     *
     * @param {djs.model.Base} element
     */
    ElementRegistry.prototype.remove = function(element) {
      var elements = this._elements,
          id = element.id || element,
          container = id && elements[id];

      Eif (container) {

        // unset element id on gfx
        svgAttr(container.gfx, ELEMENT_ID, '');

        if (container.secondaryGfx) {
          svgAttr(container.secondaryGfx, ELEMENT_ID, '');
        }

        delete elements[id];
      }
    };

    /**
     * Update the id of an element
     *
     * @param {djs.model.Base} element
     * @param {String} newId
     */
    ElementRegistry.prototype.updateId = function(element, newId) {

      this._validateId(newId);

      Iif (typeof element === 'string') {
        element = this.get(element);
      }

      this._eventBus.fire('element.updateId', {
        element: element,
        newId: newId
      });

      var gfx = this.getGraphics(element),
          secondaryGfx = this.getGraphics(element, true);

      this.remove(element);

      element.id = newId;

      this.add(element, gfx, secondaryGfx);
    };

    /**
     * Return the model element for a given id or graphics.
     *
     * @example
     *
     * elementRegistry.get('SomeElementId_1');
     * elementRegistry.get(gfx);
     *
     *
     * @param {String|SVGElement} filter for selecting the element
     *
     * @return {djs.model.Base}
     */
    ElementRegistry.prototype.get = function(filter) {
      var id;

      Eif (typeof filter === 'string') {
        id = filter;
      } else {
        id = filter && svgAttr(filter, ELEMENT_ID);
      }

      var container = this._elements[id];
      return container && container.element;
    };

    /**
     * Return all elements that match a given filter function.
     *
     * @param {Function} fn
     *
     * @return {Array<djs.model.Base>}
     */
    ElementRegistry.prototype.filter = function(fn) {

      var filtered = [];

      this.forEach(function(element, gfx) {
        if (fn(element, gfx)) {
          filtered.push(element);
        }
      });

      return filtered;
    };

    /**
     * Return all rendered model elements.
     *
     * @return {Array<djs.model.Base>}
     */
    ElementRegistry.prototype.getAll = function() {
      return this.filter(function(e) { return e; });
    };

    /**
     * Iterate over all diagram elements.
     *
     * @param {Function} fn
     */
    ElementRegistry.prototype.forEach = function(fn) {

      var map = this._elements;

      Object.keys(map).forEach(function(id) {
        var container = map[id],
            element = container.element,
            gfx = container.gfx;

        return fn(element, gfx);
      });
    };

    /**
     * Return the graphical representation of an element or its id.
     *
     * @example
     * elementRegistry.getGraphics('SomeElementId_1');
     * elementRegistry.getGraphics(rootElement); // <g ...>
     *
     * elementRegistry.getGraphics(rootElement, true); // <svg ...>
     *
     *
     * @param {String|djs.model.Base} filter
     * @param {Boolean} [secondary=false] whether to return the secondary connected element
     *
     * @return {SVGElement}
     */
    ElementRegistry.prototype.getGraphics = function(filter, secondary) {
      var id = filter.id || filter;

      var container = this._elements[id];
      return container && (secondary ? container.secondaryGfx : container.gfx);
    };

    /**
     * Validate the suitability of the given id and signals a problem
     * with an exception.
     *
     * @param {String} id
     *
     * @throws {Error} if id is empty or already assigned
     */
    ElementRegistry.prototype._validateId = function(id) {
      Iif (!id) {
        throw new Error('element must have an id');
      }

      Iif (this._elements[id]) {
        throw new Error('element with id ' + id + ' already added');
      }
    };