# all files / diagram-js/lib/features/global-connect/ GlobalConnect.js

86.05% Statements 37/43
57.14% Branches 8/14
80% Functions 8/10
86.05% Lines 37/43

    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
    1261×
    1×

    72×

    72×
    72×

    72×

    72×
    1×
    1×

    1×

    1×

    1×

    1×

    72×
    3×
    3×

    3×
    1×

    72×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×
    3×

    1×
    2×

    2×

    1×
    2×

    2×

    1×
    2×

     var MARKER_OK = 'connect-ok',
        MARKER_NOT_OK = 'connect-not-ok';

    /**
     * @class
     * @constructor
     *
     * @param {EventBus} eventBus
     * @param {Dragging} dragging
     * @param {Connect} connect
     * @param {Canvas} canvas
     * @param {ToolManager} toolManager
     * @param {Rules} rules
     */
    export default function GlobalConnect(
        eventBus, dragging, connect,
        canvas, toolManager, rules) {

      var self = this;

      this._dragging = dragging;
      this._rules = rules;

      toolManager.registerTool('global-connect', {
        tool: 'global-connect',
        dragging: 'global-connect.drag'
      });

      eventBus.on('global-connect.hover', function(event) {
        var context = event.context,
            startTarget = event.hover;

        var canStartConnect = context.canStartConnect = self.canStartConnect(startTarget);

        // simply ignore hover
        Iif (canStartConnect === null) {
          return;
        }

        context.startTarget = startTarget;

        canvas.addMarker(startTarget, canStartConnect ? MARKER_OK : MARKER_NOT_OK);
      });

      eventBus.on([ 'global-connect.out', 'global-connect.cleanup' ], function(event) {
        var startTarget = event.context.startTarget,
            canStartConnect = event.context.canStartConnect;

        if (startTarget) {
          canvas.removeMarker(startTarget, canStartConnect ? MARKER_OK : MARKER_NOT_OK);
        }
      });

      eventBus.on([ 'global-connect.ended' ], function(event) {
        var context = event.context,
            startTarget = context.startTarget,
            startPosition = {
              x: event.x,
              y: event.y
            };

        var canStartConnect = self.canStartConnect(startTarget);

        Iif (!canStartConnect) {
          return;
        }

        eventBus.once('element.out', function() {
          eventBus.once([ 'connect.ended', 'connect.canceled' ], function() {
            eventBus.fire('global-connect.drag.ended');
          });

          connect.start(null, startTarget, startPosition);
        });

        return false;
      });
    }

    GlobalConnect.$inject = [
      'eventBus',
      'dragging',
      'connect',
      'canvas',
      'toolManager',
      'rules'
    ];

    /**
     * Initiates tool activity.
     */
    GlobalConnect.prototype.start = function(event) {
      this._dragging.init(event, 'global-connect', {
        trapClick: false,
        data: {
          context: {}
        }
      });
    };

    GlobalConnect.prototype.toggle = function() {
      Iif (this.isActive()) {
        this._dragging.cancel();
      } else {
        this.start();
      }
    };

    GlobalConnect.prototype.isActive = function() {
      var context = this._dragging.context();

      return context && /^global-connect/.test(context.prefix);
    };

    /**
     * Check if source shape can initiate connection.
     *
     * @param  {Shape} startTarget
     * @return {Boolean}
     */
    GlobalConnect.prototype.canStartConnect = function(startTarget) {
      return this._rules.allowed('connection.start', { source: startTarget });
    };