all files / Github/diagram-js/lib/features/bendpoints/ BendpointMovePreview.js

96.34% Statements 79/82
73.08% Branches 19/26
100% Functions 6/6
96.34% Lines 79/82
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                                                98× 98×       98×   19× 19× 19× 19× 19× 19×     19×   19×       19×     19× 19×   19× 19×     98× 10× 10× 10×   10× 10×   10× 10× 10×               98×             18×   18× 10× 10×       98×   22× 22× 22× 22× 22× 22×     22×                                       22×     98×         18× 18× 18×     18×     18×   18× 18×   18× 10× 10×     18×                  
import {
  classes as svgClasses,
  remove as svgRemove
} from 'tiny-svg';
 
import {
  addBendpoint
} from './BendpointUtil';
 
import {
  filterRedundantWaypoints
} from '../../layout/LayoutUtil';
 
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',
    MARKER_ELEMENT_HIDDEN = 'djs-element-hidden';
 
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),
      connectionDocking = injector.get('connectionDocking', 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);
    canvas.addMarker(connection, MARKER_ELEMENT_HIDDEN);
  });
 
  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);
 
      Eif (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,
        originalWaypoints = connection.waypoints,
        waypoints = originalWaypoints.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.noCropping = true;
        hints.noLayout = true;
        waypoints[context.bendpointIndex] = bendpoint;
      }
 
      Eif (connectionDocking) {
        // (0) temporarily assign new waypoints
        connection.waypoints = waypoints;
 
        // (1) crop connection with new waypoints and save them
        connection.waypoints = connectionDocking.getCroppedWaypoints(connection);
        waypoints = connection.waypoints;
 
        // (2) restore original waypoints to not mutate connection directly
        connection.waypoints = originalWaypoints;
      }
 
      // remove overlapping points
      hints.waypoints = filterRedundantWaypoints(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);
    canvas.removeMarker(connection, MARKER_ELEMENT_HIDDEN);
 
    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'
];