# all files / diagram-js/lib/features/dragging/ HoverFix.js

76.92% Statements 30/39
75% Branches 6/8
90.91% Functions 10/11
76.92% Lines 30/39

    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

    1×

    1189×

    1189×

    139×

    139×

    139×

    1189×

    101×
    101×

    1×
    1×

    146×
    146×
    146×

    146×
    145×
    145×
    145×

    1189×

    139×
    58×

    81×

    81×
    79×

    2×

    2×

    2×

    2×

    1×

    2×
     import {
      closest as domClosest
    } from 'min-dom';

    import {
      toPoint
    } from '../../util/Event';

    var HIGH_PRIORITY = 1500;

    /**
     * Browsers may swallow certain events (hover, out ...) if users are to
     * fast with the mouse.
     *
     * @see http://stackoverflow.com/questions/7448468/why-cant-i-reliably-capture-a-mouseout-event
     *
     * The fix implemented in this component ensure that we
     *
     * 1) have a hover state after a successive drag.move event
     * 2) have an out event when dragging leaves an element
     *
     * @param {EventBus} eventBus
     * @param {Dragging} dragging
     * @param {ElementRegistry} elementRegistry
     */
    export default function HoverFix(eventBus, dragging, elementRegistry) {

      var self = this;

      /**
       * We wait for a specific sequence of events before
       * emitting a fake drag.hover event.
       *
       * Event Sequence:
       *
       * drag.start
       * drag.move
       * drag.move >> ensure we are hovering
       */
      eventBus.on('drag.start', function(event) {

        eventBus.once('drag.move', function() {

          eventBus.once('drag.move', function(event) {

            self.ensureHover(event);
          });
        });

      });

      /**
       * We make sure that drag.out is always fired, even if the
       * browser swallows an element.out event.
       *
       * Event sequence:
       *
       * drag.hover
       * (element.out >> sometimes swallowed)
       * element.hover >> ensure we fired drag.out
       */
      eventBus.on('drag.init', function() {

        var hover, hoverGfx;

        function setDragHover(event) {
          hover = event.hover;
          hoverGfx = event.hoverGfx;
        }

        function unsetHover() {
          hover = null;
          hoverGfx = null;
        }

        function ensureOut() {

          Eif (!hover) {
            return;
          }

          var element = hover,
              gfx = hoverGfx;

          hover = null;
          hoverGfx = null;

          // emit synthetic out event
          dragging.out({
            element: element,
            gfx: gfx
          });
        }

        eventBus.on('drag.hover', setDragHover);
        eventBus.on('element.out', unsetHover);
        eventBus.on('element.hover', HIGH_PRIORITY, ensureOut);

        eventBus.once('drag.cleanup', function() {
          eventBus.off('drag.hover', setDragHover);
          eventBus.off('element.out', unsetHover);
          eventBus.off('element.hover', ensureOut);
        });

      });

      /**
       * Make sure we are god damn hovering!
       *
       * @param {Event} dragging event
       */
      this.ensureHover = function(event) {

        if (event.hover) {
          return;
        }

        var originalEvent = event.originalEvent,
            position,
            target,
            element,
            gfx;

        if (!(originalEvent instanceof MouseEvent)) {
          return;
        }

        position = toPoint(originalEvent);

        // damn expensive operation, ouch!
        target = document.elementFromPoint(position.x, position.y);

        gfx = getGfx(target);

        Iif (gfx) {
          element = elementRegistry.get(gfx);

          dragging.hover({ element: element, gfx: gfx });
        }
      };

    }

    HoverFix.$inject = [
      'eventBus',
      'dragging',
      'elementRegistry'
    ];

    // helpers /////////////////////

    function getGfx(target) {
      return domClosest(target, 'svg, .djs-element', true);
    }