all files / Github/diagram-js/lib/features/connect/ ConnectPreview.js

96.43% Statements 27/28
83.33% Branches 10/12
100% Functions 5/5
96.43% Lines 27/28
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                     124×   124× 18× 18× 18× 18×   18×           18×               124× 16× 16× 16×     16×       16×     124× 16×     16× 16×       124× 16×                  
var HIGH_PRIORITY = 1100,
    LOW_PRIORITY = 900;
 
var MARKER_OK = 'connect-ok',
    MARKER_NOT_OK = 'connect-not-ok';
 
/**
 * Shows connection preview during connect.
 *
 * @param {didi.Injector} injector
 * @param {EventBus} eventBus
 * @param {Canvas} canvas
 */
export default function ConnectPreview(injector, eventBus, canvas) {
  var connectionPreview = injector.get('connectionPreview', false);
 
  eventBus.on('connect.move', function(event) {
    var context = event.context,
        source = context.source,
        target = context.target,
        canConnect = context.canExecute;
 
    var endPosition = {
      x: event.x,
      y: event.y
    };
 
 
    if (connectionPreview) {
      connectionPreview.drawPreview(context, canConnect, {
        source: source,
        target: target,
        connectionEnd: endPosition
      });
    }
  });
 
  eventBus.on('connect.hover', LOW_PRIORITY, function(event) {
    var context = event.context,
        hover = event.hover,
        canExecute = context.canExecute;
 
    // ignore hover
    Iif (canExecute === null) {
      return;
    }
 
    canvas.addMarker(hover, canExecute ? MARKER_OK : MARKER_NOT_OK);
  });
 
  eventBus.on([ 'connect.out', 'connect.cleanup' ], HIGH_PRIORITY, function(event) {
    var context = event.context;
 
    // remove marker before target is removed from context
    Eif (context.target) {
      canvas.removeMarker(context.target, context.canExecute ? MARKER_OK : MARKER_NOT_OK);
    }
  });
 
  eventBus.on('connect.cleanup', function(event) {
    if (connectionPreview) {
      connectionPreview.cleanUp(event.context);
    }
  });
}
 
ConnectPreview.$inject = [
  'injector',
  'eventBus',
  'canvas'
];