| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview This class aims to smooth out inconsistencies between browser |
| 17 |
* handling of wheel events by providing an event that is similar to that |
| 18 |
* defined in the standard, but also easier to consume. |
| 19 |
* |
| 20 |
* It is based upon the WheelEvent, which allows for up to 3 dimensional |
| 21 |
* scrolling events that come in units of either pixels, lines or pages. |
| 22 |
* http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#interface-WheelEvent |
| 23 |
* |
| 24 |
* The significant difference here is that it also provides reasonable pixel |
| 25 |
* deltas for clients that do not want to treat line and page scrolling events |
| 26 |
* specially. |
| 27 |
* |
| 28 |
* Clients of this code should be aware that some input devices only fire a few |
| 29 |
* discrete events (such as a mouse wheel without acceleration) whereas some can |
| 30 |
* generate a large number of events for a single interaction (such as a |
| 31 |
* touchpad with acceleration). There is no signal in the events to reliably |
| 32 |
* distinguish between these. |
| 33 |
* |
| 34 |
* @see ../demos/wheelhandler.html |
| 35 |
*/ |
| 36 |
|
| 37 |
goog.provide('goog.events.WheelEvent'); |
| 38 |
|
| 39 |
goog.require('goog.asserts'); |
| 40 |
goog.require('goog.events.BrowserEvent'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* A common class for wheel events. This is used with the WheelHandler. |
| 46 |
* |
| 47 |
* @param {Event} browserEvent Browser event object. |
| 48 |
* @param {goog.events.WheelEvent.DeltaMode} deltaMode The delta mode units of |
| 49 |
* the wheel event. |
| 50 |
* @param {number} deltaX The number of delta units the user in the X axis. |
| 51 |
* @param {number} deltaY The number of delta units the user in the Y axis. |
| 52 |
* @param {number} deltaZ The number of delta units the user in the Z axis. |
| 53 |
* @constructor |
| 54 |
* @extends {goog.events.BrowserEvent} |
| 55 |
* @final |
| 56 |
*/ |
| 57 |
goog.events.WheelEvent = function( |
| 58 |
browserEvent, deltaMode, deltaX, deltaY, deltaZ) { |
| 59 |
goog.events.WheelEvent.base(this, 'constructor', browserEvent); |
| 60 |
goog.asserts.assert(browserEvent, 'Expecting a non-null browserEvent'); |
| 61 |
|
| 62 |
/** @type {goog.events.WheelEvent.EventType} */ |
| 63 |
this.type = goog.events.WheelEvent.EventType.WHEEL; |
| 64 |
|
| 65 |
/** |
| 66 |
* An enum corresponding to the units of this event. |
| 67 |
* @type {goog.events.WheelEvent.DeltaMode} |
| 68 |
*/ |
| 69 |
this.deltaMode = deltaMode; |
| 70 |
|
| 71 |
/** |
| 72 |
* The number of delta units in the X axis. |
| 73 |
* @type {number} |
| 74 |
*/ |
| 75 |
this.deltaX = deltaX; |
| 76 |
|
| 77 |
/** |
| 78 |
* The number of delta units in the Y axis. |
| 79 |
* @type {number} |
| 80 |
*/ |
| 81 |
this.deltaY = deltaY; |
| 82 |
|
| 83 |
/** |
| 84 |
* The number of delta units in the Z axis. |
| 85 |
* @type {number} |
| 86 |
*/ |
| 87 |
this.deltaZ = deltaZ; |
| 88 |
|
| 89 |
// Ratio between delta and pixel values. |
| 90 |
var pixelRatio = 1; // Value for DeltaMode.PIXEL |
| 91 |
switch (deltaMode) { |
| 92 |
case goog.events.WheelEvent.DeltaMode.PAGE: |
| 93 |
pixelRatio *= goog.events.WheelEvent.PIXELS_PER_PAGE_; |
| 94 |
break; |
| 95 |
case goog.events.WheelEvent.DeltaMode.LINE: |
| 96 |
pixelRatio *= goog.events.WheelEvent.PIXELS_PER_LINE_; |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* The number of delta pixels in the X axis. Code that doesn't want to handle |
| 102 |
* different deltaMode units can just look here. |
| 103 |
* @type {number} |
| 104 |
*/ |
| 105 |
this.pixelDeltaX = this.deltaX * pixelRatio; |
| 106 |
|
| 107 |
/** |
| 108 |
* The number of pixels in the Y axis. Code that doesn't want to |
| 109 |
* handle different deltaMode units can just look here. |
| 110 |
* @type {number} |
| 111 |
*/ |
| 112 |
this.pixelDeltaY = this.deltaY * pixelRatio; |
| 113 |
|
| 114 |
/** |
| 115 |
* The number of pixels scrolled in the Z axis. Code that doesn't want to |
| 116 |
* handle different deltaMode units can just look here. |
| 117 |
* @type {number} |
| 118 |
*/ |
| 119 |
this.pixelDeltaZ = this.deltaZ * pixelRatio; |
| 120 |
}; |
| 121 |
goog.inherits(goog.events.WheelEvent, goog.events.BrowserEvent); |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Enum type for the events fired by the wheel handler. |
| 126 |
* @enum {string} |
| 127 |
*/ |
| 128 |
goog.events.WheelEvent.EventType = { |
| 129 |
/** The user has provided wheel-based input. */ |
| 130 |
WHEEL: 'wheel' |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Units for the deltas in a WheelEvent. |
| 136 |
* @enum {number} |
| 137 |
*/ |
| 138 |
goog.events.WheelEvent.DeltaMode = { |
| 139 |
/** The units are in pixels. From DOM_DELTA_PIXEL. */ |
| 140 |
PIXEL: 0, |
| 141 |
/** The units are in lines. From DOM_DELTA_LINE. */ |
| 142 |
LINE: 1, |
| 143 |
/** The units are in pages. From DOM_DELTA_PAGE. */ |
| 144 |
PAGE: 2 |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* A conversion number between line scroll units and pixel scroll units. The |
| 150 |
* actual value per line can vary a lot between devices and font sizes. This |
| 151 |
* number can not be perfect, but it should be reasonable for converting lines |
| 152 |
* scroll events into pixels. |
| 153 |
* @const {number} |
| 154 |
* @private |
| 155 |
*/ |
| 156 |
goog.events.WheelEvent.PIXELS_PER_LINE_ = 15; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* A conversion number between page scroll units and pixel scroll units. The |
| 161 |
* actual value per page can vary a lot as many different devices have different |
| 162 |
* screen sizes, and the window might not be taking up the full screen. This |
| 163 |
* number can not be perfect, but it should be reasonable for converting page |
| 164 |
* scroll events into pixels. |
| 165 |
* @const {number} |
| 166 |
* @private |
| 167 |
*/ |
| 168 |
goog.events.WheelEvent.PIXELS_PER_PAGE_ = |
| 169 |
30 * goog.events.WheelEvent.PIXELS_PER_LINE_; |