# all files / ol/pointer/ mousesource.js

90.16% Statements 55/61
72.73% Branches 16/22
81.82% Functions 9/11
90.16% Lines 55/61

    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
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250

    1×

    1×

    1×
    140×

    140×

    140×

    140×

    1×

    1×

    1×

    1×

    1×

    11×
    11×
    11×

    2×
    2×

    1×

    10×

    1×
    10×

    10×
    10×

    10×
    10×
    10×

    10×

    1×
    5×

    4×

    4×
    4×
    4×

    1×
    2×
    2×
    2×

    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    2×
    2×
    2×

    1×
    1×
    1×
    1×

    1×

    1×
    1×

     // Based on https://github.com/Polymer/PointerEvents

    // Copyright (c) 2013 The Polymer Authors. All rights reserved.
    //
    // Redistribution and use in source and binary forms, with or without
    // modification, are permitted provided that the following conditions are
    // met:
    //
    // * Redistributions of source code must retain the above copyright
    // notice, this list of conditions and the following disclaimer.
    // * Redistributions in binary form must reproduce the above
    // copyright notice, this list of conditions and the following disclaimer
    // in the documentation and/or other materials provided with the
    // distribution.
    // * Neither the name of Google Inc. nor the names of its
    // contributors may be used to endorse or promote products derived from
    // this software without specific prior written permission.
    //
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    goog.provide('ol.pointer.MouseSource');

    goog.require('ol.pointer.EventSource');

    /**
     * @param {ol.pointer.PointerEventHandler} dispatcher
     * @constructor
     * @extends {ol.pointer.EventSource}
     */
    ol.pointer.MouseSource = function(dispatcher) {
      var mapping = {
        'mousedown': this.mousedown,
        'mousemove': this.mousemove,
        'mouseup': this.mouseup,
        'mouseover': this.mouseover,
        'mouseout': this.mouseout
      };
      goog.base(this, dispatcher, mapping);

      /**
       * @const
       * @type {!Object.<string, goog.events.BrowserEvent|Object>}
       */
      this.pointerMap = dispatcher.pointerMap;

      /**
       * @const
       * @type {Array.<ol.Pixel>}
       */
      this.lastTouches = [];
    };
    goog.inherits(ol.pointer.MouseSource, ol.pointer.EventSource);

    /**
     * @const
     * @type {number}
     */
    ol.pointer.MouseSource.POINTER_ID = 1;

    /**
     * @const
     * @type {string}
     */
    ol.pointer.MouseSource.POINTER_TYPE = 'mouse';

    /**
     * Radius around touchend that swallows mouse events.
     *
     * @const
     * @type {number}
     */
    ol.pointer.MouseSource.DEDUP_DIST = 25;

    /**
     * Detect if a mouse event was simulated from a touch by
     * checking if previously there was a touch event at the
     * same position.
     *
     * FIXME - Known problem with the native Android browser on
     * Samsung GT-I9100 (Android 4.1.2):
     * In case the page is scrolled, this function does not work
     * correctly when a canvas is used (WebGL or canvas renderer).
     * Mouse listeners on canvas elements (for this browser), create
     * two mouse events: One 'good' and one 'bad' one (on other browsers or
     * when a div is used, there is only one event). For the 'bad' one,
     * clientX/clientY and also pageX/pageY are wrong when the page
     * is scrolled. Because of that, this function can not detect if
     * the events were simulated from a touch event. As result, a
     * pointer event at a wrong position is dispatched, which confuses
     * the map interactions.
     * It is unclear, how one can get the correct position for the event
     * or detect that the positions are invalid.
     *
     * @private
     * @param {goog.events.BrowserEvent} inEvent
     * @return {boolean} True, if the event was generated by a touch.
     */
    ol.pointer.MouseSource.prototype.isEventSimulatedFromTouch_ =
        function(inEvent) {
      var lts = this.lastTouches;
      var x = inEvent.clientX, y = inEvent.clientY;
      for (var i = 0, l = lts.length, t; i < l && (t = lts[i]); i++) {
        // simulated mouse events will be swallowed near a primary touchend
        var dx = Math.abs(x - t[0]), dy = Math.abs(y - t[1]);
        if (dx <= ol.pointer.MouseSource.DEDUP_DIST &&
            dy <= ol.pointer.MouseSource.DEDUP_DIST) {
          return true;
        }
      }
      return false;
    };

    /**
     * Creates a copy of the original event that will be used
     * for the fake pointer event.
     *
     * @param {goog.events.BrowserEvent} inEvent
     * @param {ol.pointer.PointerEventHandler} dispatcher
     * @return {Object}
     */
    ol.pointer.MouseSource.prepareEvent = function(inEvent, dispatcher) {
      var e = dispatcher.cloneEvent(inEvent, inEvent.getBrowserEvent());

      // forward mouse preventDefault
      var pd = e.preventDefault;
      e.preventDefault = function() {
        inEvent.preventDefault();
        pd();
      };

      e.pointerId = ol.pointer.MouseSource.POINTER_ID;
      e.isPrimary = true;
      e.pointerType = ol.pointer.MouseSource.POINTER_TYPE;

      return e;
    };

    /**
     * Handler for `mousedown`.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.mousedown = function(inEvent) {
      if (!this.isEventSimulatedFromTouch_(inEvent)) {
        // TODO(dfreedman) workaround for some elements not sending mouseup
        // http://crbug/149091
        Iif (ol.pointer.MouseSource.POINTER_ID.toString() in this.pointerMap) {
          this.cancel(inEvent);
        }
        var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
        this.pointerMap[ol.pointer.MouseSource.POINTER_ID.toString()] = inEvent;
        this.dispatcher.down(e, inEvent);
      }
    };

    /**
     * Handler for `mousemove`.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.mousemove = function(inEvent) {
      Eif (!this.isEventSimulatedFromTouch_(inEvent)) {
        var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
        this.dispatcher.move(e, inEvent);
      }
    };

    /**
     * Handler for `mouseup`.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.mouseup = function(inEvent) {
      Eif (!this.isEventSimulatedFromTouch_(inEvent)) {
        var p = this.pointerMap[ol.pointer.MouseSource.POINTER_ID.toString()];

        Eif (p && p.button === inEvent.button) {
          var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
          this.dispatcher.up(e, inEvent);
          this.cleanupMouse();
        }
      }
    };

    /**
     * Handler for `mouseover`.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.mouseover = function(inEvent) {
      Eif (!this.isEventSimulatedFromTouch_(inEvent)) {
        var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
        this.dispatcher.enterOver(e, inEvent);
      }
    };

    /**
     * Handler for `mouseout`.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.mouseout = function(inEvent) {
      Eif (!this.isEventSimulatedFromTouch_(inEvent)) {
        var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
        this.dispatcher.leaveOut(e, inEvent);
      }
    };

    /**
     * Dispatches a `pointercancel` event.
     *
     * @param {goog.events.BrowserEvent} inEvent
     */
    ol.pointer.MouseSource.prototype.cancel = function(inEvent) {
      var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
      this.dispatcher.cancel(e, inEvent);
      this.cleanupMouse();
    };

    /**
     * Remove the mouse from the list of active pointers.
     */
    ol.pointer.MouseSource.prototype.cleanupMouse = function() {
      delete this.pointerMap[ol.pointer.MouseSource.POINTER_ID.toString()];
    };