all files / lib/features/bendpoints/ BendpointMovePreview.js

100% Statements 71/71
95.83% Branches 23/24
100% Functions 6/6
100% Lines 71/71
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                                        71×       71×   18× 18× 18× 18× 18× 18×     18×   18×       18×     18× 18×   18×     71× 19× 19× 19×   19× 19×   19× 11×         71×             24×   24× 19× 19×       71×   27× 27× 27× 27× 27×     27×                       27×     71×         18× 18× 18×     18×     18×   18×   18× 15× 15×     18×                  
import {
  classes as svgClasses,
  remove as svgRemove
} from 'tiny-svg';
 
import {
  addBendpoint
} from './BendpointUtil';
 
import { translate } from '../../util/SvgTransformUtil';
 
 
var MARKER_OK = 'connect-ok',
    MARKER_NOT_OK = 'connect-not-ok',
    MARKER_CONNECT_HOVER = 'connect-hover',
    MARKER_CONNECT_UPDATING = 'djs-updating';
 
var COMMAND_RECONNECT_START = 'connection.reconnectStart',
    COMMAND_RECONNECT_END = 'connection.reconnectEnd';
 
var HIGH_PRIORITY = 1100;
 
/**
 * A component that implements moving of bendpoints
 */
export default function BendpointMovePreview(injector, eventBus, canvas) {
 
  var connectionPreview = injector.get('connectionPreview', false);
 
  // DRAGGING IMPLEMENTATION
 
  eventBus.on('bendpoint.move.start', function(e) {
 
    var context = e.context,
        connection = context.connection,
        originalWaypoints = connection.waypoints,
        waypoints = originalWaypoints.slice(),
        insert = context.insert,
        idx = context.bendpointIndex;
 
    // save waypoints to restore at the end
    context.originalWaypoints = originalWaypoints;
 
    if (insert) {
      // insert placeholder for bendpoint to-be-added
      waypoints.splice(idx, 0, { x: e.x, y: e.y });
    }
 
    connection.waypoints = waypoints;
 
    // add dragger gfx
    context.draggerGfx = addBendpoint(canvas.getLayer('overlays'));
    svgClasses(context.draggerGfx).add('djs-dragging');
 
    canvas.addMarker(connection, MARKER_CONNECT_UPDATING);
  });
 
  eventBus.on('bendpoint.move.hover', function(e) {
    var context = e.context,
        allowed = context.allowed,
        hover = context.hover;
 
    Eif (e.hover) {
      canvas.addMarker(hover, MARKER_CONNECT_HOVER);
 
      if (allowed) {
        canvas.removeMarker(hover, MARKER_NOT_OK);
        canvas.addMarker(hover, MARKER_OK);
      } else if (allowed === false) {
        canvas.removeMarker(hover, MARKER_OK);
        canvas.addMarker(hover, MARKER_NOT_OK);
      }
    }
  });
 
  eventBus.on([
    'bendpoint.move.out',
    'bendpoint.move.cleanup'
  ], HIGH_PRIORITY, function(e) {
 
    // remove connect marker
    // if it was added
    var hover = e.context.hover;
 
    if (hover) {
      canvas.removeMarker(hover, MARKER_CONNECT_HOVER);
      canvas.removeMarker(hover, e.context.target ? MARKER_OK : MARKER_NOT_OK);
    }
  });
 
  eventBus.on('bendpoint.move.move', function(e) {
 
    var context = e.context,
        moveType = context.type,
        connection = e.connection,
        waypoints = connection.waypoints.slice(),
        bendpoint = { x: e.x, y: e.y },
        hints;
 
    if (connectionPreview) {
      hints = {
        source: connection.source,
        target: connection.target
      };
 
      if (moveType === COMMAND_RECONNECT_START) {
        hints.source = context.target;
        hints.connectionStart = bendpoint;
      } else if (moveType === COMMAND_RECONNECT_END) {
        hints.target = context.target;
        hints.connectionEnd = bendpoint;
      } else {
        hints.noLayout = true;
        waypoints[context.bendpointIndex] = bendpoint;
      }
 
      hints.waypoints = waypoints;
 
      connectionPreview.drawPreview(context, context.allowed, hints);
    }
 
    // add dragger gfx
    translate(context.draggerGfx, e.x, e.y);
  });
 
  eventBus.on([
    'bendpoint.move.end',
    'bendpoint.move.cancel'
  ], HIGH_PRIORITY, function(e) {
 
    var context = e.context,
        hover = context.hover,
        connection = context.connection;
 
    // reassign original waypoints
    connection.waypoints = context.originalWaypoints;
 
    // remove dragger gfx
    svgRemove(context.draggerGfx);
 
    canvas.removeMarker(connection, MARKER_CONNECT_UPDATING);
 
    if (hover) {
      canvas.removeMarker(hover, MARKER_OK);
      canvas.removeMarker(hover, context.target ? MARKER_OK : MARKER_NOT_OK);
    }
 
    if (connectionPreview) {
      connectionPreview.cleanUp(context);
    }
  });
}
 
BendpointMovePreview.$inject = [
  'injector',
  'eventBus',
  'canvas'
];