# all files / Github/diagram-js/lib/features/bendpoints/ BendpointMove.js

96.36% Statements 53/55
83.33% Branches 25/30
100% Functions 5/5
96.36% Lines 53/55

    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

    1×
    1×
    1×

    1×

    98×

    98×

    19×
    19×

    19×
    4×

    15×
    4×

    11×

    19×

    19×

    19×

    19×

    98×
    10×

    10×

    10×

    10×

    10×
    10×

    98×
    18×

    18×
    18×

    98×

    18×
    18×
    18×
    18×
    18×
    18×
    18×

    18×

    18×
    4×
    14×
    3×
    11×
    11×

    4×

    7×

    11×

    11×

    11×

    11×
    11×

    11×

    11×

    1×

     import {
      filterRedundantWaypoints
    } from '../../layout/LayoutUtil';

    var COMMAND_BENDPOINT_UPDATE = 'connection.updateWaypoints',
        COMMAND_RECONNECT_START = 'connection.reconnectStart',
        COMMAND_RECONNECT_END = 'connection.reconnectEnd';

    var round = Math.round;

    /**
     * A component that implements moving of bendpoints
     */
    export default function BendpointMove(injector, eventBus, canvas, dragging, rules, modeling) {

      // optional connection docking integration
      var connectionDocking = injector.get('connectionDocking', false);

      // API
      this.start = function(event, connection, bendpointIndex, insert) {

        var type,
            context,
            waypoints = connection.waypoints,
            gfx = canvas.getGraphics(connection);

        if (!insert && bendpointIndex === 0) {
          type = COMMAND_RECONNECT_START;
        } else
        if (!insert && bendpointIndex === waypoints.length - 1) {
          type = COMMAND_RECONNECT_END;
        } else {
          type = COMMAND_BENDPOINT_UPDATE;
        }

        context = {
          connection: connection,
          bendpointIndex: bendpointIndex,
          insert: insert,
          type: type
        };

        var allowed = context.allowed = rules.allowed(context.type, context);

        Iif (allowed === false) {
          return;
        }

        dragging.init(event, 'bendpoint.move', {
          data: {
            connection: connection,
            connectionGfx: gfx,
            context: context
          }
        });
      };

      eventBus.on('bendpoint.move.hover', function(e) {
        var context = e.context;

        context.hover = e.hover;

        Eif (e.hover) {
          // asks whether reconnect / bendpoint move / bendpoint add
          // is allowed at the given position
          var allowed = context.allowed = rules.allowed(context.type, context);

          Eif (allowed) {
            context.target = context.hover;
          }
        }
      });

      eventBus.on([ 'bendpoint.move.out', 'bendpoint.move.cleanup' ], function(event) {
        var context = event.context;

        context.target = null;
        context.allowed = false;
      });

      eventBus.on('bendpoint.move.end', function(e) {

        var context = e.context,
            connection = context.connection,
            originalWaypoints = connection.waypoints,
            newWaypoints = originalWaypoints.slice(),
            bendpointIndex = context.bendpointIndex,
            allowed = context.allowed,
            insert = context.insert,
            bendpoint,
            hints;

        // ensure we have actual pixel values bendpoint
        // coordinates (important when zoom level was > 1 during move)
        bendpoint = {
          x: round(e.x),
          y: round(e.y)
        };

        if (allowed && context.type === COMMAND_RECONNECT_START) {
          modeling.reconnectStart(context.connection, context.target, bendpoint);
        } else if (allowed && context.type === COMMAND_RECONNECT_END) {
          modeling.reconnectEnd(context.connection, context.target, bendpoint);
        } else Eif (allowed !== false && context.type === COMMAND_BENDPOINT_UPDATE) {
          if (insert) {
            // insert new bendpoint
            newWaypoints.splice(bendpointIndex, 0, bendpoint);
          } else {
            // swap previous waypoint with the moved one
            newWaypoints[bendpointIndex] = bendpoint;
          }

          // pass hints on the actual moved bendpoint
          // this is useful for connection and label layouting
          hints = {
            bendpointMove: {
              insert: insert,
              bendpointIndex: bendpointIndex
            }
          };

          Eif (connectionDocking) {
            // (0) temporarily assign new waypoints
            connection.waypoints = newWaypoints;

            // (1) crop connection with new waypoints and save them
            connection.waypoints = connectionDocking.getCroppedWaypoints(connection);
            newWaypoints = connection.waypoints;

            // (2) restore original waypoints to not mutate connection directly
            connection.waypoints = originalWaypoints;
          }

          modeling.updateWaypoints(context.connection, filterRedundantWaypoints(newWaypoints), hints);
        } else {
          return false;
        }
      });
    }

    BendpointMove.$inject = [
      'injector',
      'eventBus',
      'canvas',
      'dragging',
      'rules',
      'modeling'
    ];