# all files / diagram-js/lib/features/ordering/ OrderingProvider.js

95.65% Statements 22/23
71.43% Branches 10/14
75% Functions 3/4
95.65% Lines 22/23

    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

    1189×

    1189×

    1189×

    626×
    626×
    626×

    626×

    626×

    626×
    626×

    626×

    1189×

    948×
    948×
    948×

    948×

    948×

    948×
    948×

    948×

    1×

    1×import inherits from 'inherits';

    import CommandInterceptor from '../../command/CommandInterceptor';

    /**
     * An abstract provider that allows modelers to implement a custom
     * ordering of diagram elements on the canvas.
     *
     * It makes sure that the order is always preserved during element
     * creation and move operations.
     *
     * In order to use this behavior, inherit from it and override
     * the method {@link OrderingProvider#getOrdering}.
     *
     * @example
     *
     * ```javascript
     * function CustomOrderingProvider(eventBus) {
     *   OrderingProvider.call(this, eventBus);
     *
     *   this.getOrdering = function(element, newParent) {
     *     // always insert elements at the front
     *     // when moving
     *     return {
     *       index: 0,
     *       parent: newParent
     *     };
     *   };
     * }
     * ```
     *
     * @param {EventBus} eventBus
     */
    export default function OrderingProvider(eventBus) {

      CommandInterceptor.call(this, eventBus);

      var self = this;

      this.preExecute([ 'shape.create', 'connection.create' ], function(event) {

        var context = event.context,
            element = context.shape || context.connection,
            parent = context.parent;

        var ordering = self.getOrdering(element, parent);

        Eif (ordering) {

          Eif (ordering.parent !== undefined) {
            context.parent = ordering.parent;
          }

          context.parentIndex = ordering.index;
        }
      });

      this.preExecute([ 'shape.move', 'connection.move' ], function(event) {

        var context = event.context,
            element = context.shape || context.connection,
            parent = context.newParent || element.parent;

        var ordering = self.getOrdering(element, parent);

        Eif (ordering) {

          Eif (ordering.parent !== undefined) {
            context.newParent = ordering.parent;
          }

          context.newParentIndex = ordering.index;
        }
      });
    }

    /**
     * Return a custom ordering of the element, both in terms
     * of parent element and index in the new parent.
     *
     * Implementors of this method must return an object with
     * `parent` _and_ `index` in it.
     *
     * @param {djs.model.Base} element
     * @param {djs.model.Shape} newParent
     *
     * @return {Object} ordering descriptor
     */
    OrderingProvider.prototype.getOrdering = function(element, newParent) {
      return null;
    };

    inherits(OrderingProvider, CommandInterceptor);