Path: blob/trunk/third_party/closure/goog/events/wheelevent.js
2868 views
// Copyright 2014 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview This class aims to smooth out inconsistencies between browser16* handling of wheel events by providing an event that is similar to that17* defined in the standard, but also easier to consume.18*19* It is based upon the WheelEvent, which allows for up to 3 dimensional20* scrolling events that come in units of either pixels, lines or pages.21* http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#interface-WheelEvent22*23* The significant difference here is that it also provides reasonable pixel24* deltas for clients that do not want to treat line and page scrolling events25* specially.26*27* Clients of this code should be aware that some input devices only fire a few28* discrete events (such as a mouse wheel without acceleration) whereas some can29* generate a large number of events for a single interaction (such as a30* touchpad with acceleration). There is no signal in the events to reliably31* distinguish between these.32*33* @see ../demos/wheelhandler.html34*/3536goog.provide('goog.events.WheelEvent');3738goog.require('goog.asserts');39goog.require('goog.events.BrowserEvent');40414243/**44* A common class for wheel events. This is used with the WheelHandler.45*46* @param {Event} browserEvent Browser event object.47* @param {goog.events.WheelEvent.DeltaMode} deltaMode The delta mode units of48* the wheel event.49* @param {number} deltaX The number of delta units the user in the X axis.50* @param {number} deltaY The number of delta units the user in the Y axis.51* @param {number} deltaZ The number of delta units the user in the Z axis.52* @constructor53* @extends {goog.events.BrowserEvent}54* @final55*/56goog.events.WheelEvent = function(57browserEvent, deltaMode, deltaX, deltaY, deltaZ) {58goog.events.WheelEvent.base(this, 'constructor', browserEvent);59goog.asserts.assert(browserEvent, 'Expecting a non-null browserEvent');6061/** @type {goog.events.WheelEvent.EventType} */62this.type = goog.events.WheelEvent.EventType.WHEEL;6364/**65* An enum corresponding to the units of this event.66* @type {goog.events.WheelEvent.DeltaMode}67*/68this.deltaMode = deltaMode;6970/**71* The number of delta units in the X axis.72* @type {number}73*/74this.deltaX = deltaX;7576/**77* The number of delta units in the Y axis.78* @type {number}79*/80this.deltaY = deltaY;8182/**83* The number of delta units in the Z axis.84* @type {number}85*/86this.deltaZ = deltaZ;8788// Ratio between delta and pixel values.89var pixelRatio = 1; // Value for DeltaMode.PIXEL90switch (deltaMode) {91case goog.events.WheelEvent.DeltaMode.PAGE:92pixelRatio *= goog.events.WheelEvent.PIXELS_PER_PAGE_;93break;94case goog.events.WheelEvent.DeltaMode.LINE:95pixelRatio *= goog.events.WheelEvent.PIXELS_PER_LINE_;96break;97}9899/**100* The number of delta pixels in the X axis. Code that doesn't want to handle101* different deltaMode units can just look here.102* @type {number}103*/104this.pixelDeltaX = this.deltaX * pixelRatio;105106/**107* The number of pixels in the Y axis. Code that doesn't want to108* handle different deltaMode units can just look here.109* @type {number}110*/111this.pixelDeltaY = this.deltaY * pixelRatio;112113/**114* The number of pixels scrolled in the Z axis. Code that doesn't want to115* handle different deltaMode units can just look here.116* @type {number}117*/118this.pixelDeltaZ = this.deltaZ * pixelRatio;119};120goog.inherits(goog.events.WheelEvent, goog.events.BrowserEvent);121122123/**124* Enum type for the events fired by the wheel handler.125* @enum {string}126*/127goog.events.WheelEvent.EventType = {128/** The user has provided wheel-based input. */129WHEEL: 'wheel'130};131132133/**134* Units for the deltas in a WheelEvent.135* @enum {number}136*/137goog.events.WheelEvent.DeltaMode = {138/** The units are in pixels. From DOM_DELTA_PIXEL. */139PIXEL: 0,140/** The units are in lines. From DOM_DELTA_LINE. */141LINE: 1,142/** The units are in pages. From DOM_DELTA_PAGE. */143PAGE: 2144};145146147/**148* A conversion number between line scroll units and pixel scroll units. The149* actual value per line can vary a lot between devices and font sizes. This150* number can not be perfect, but it should be reasonable for converting lines151* scroll events into pixels.152* @const {number}153* @private154*/155goog.events.WheelEvent.PIXELS_PER_LINE_ = 15;156157158/**159* A conversion number between page scroll units and pixel scroll units. The160* actual value per page can vary a lot as many different devices have different161* screen sizes, and the window might not be taking up the full screen. This162* number can not be perfect, but it should be reasonable for converting page163* scroll events into pixels.164* @const {number}165* @private166*/167goog.events.WheelEvent.PIXELS_PER_PAGE_ =16830 * goog.events.WheelEvent.PIXELS_PER_LINE_;169170171