Path: blob/trunk/third_party/closure/goog/events/wheelhandler.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 event wrapper will dispatch an event when the user uses16* the wheel on an element. The event provides details of the unit type (pixel /17* line / page) and deltas in those units in up to 3 dimensions. Additionally,18* simplified pixel deltas are provided for code that doesn't need to handle the19* different units differently. This is not to be confused with the scroll20* event, where an element in the dom can report that it was scrolled.21*22* This class aims to smooth out inconsistencies between browser platforms with23* regards to wheel events, but we do not cover every possible software/hardware24* combination out there, some of which occasionally produce very large deltas25* in wheel events, especially when the device supports acceleration.26*27* Relevant standard:28* http://www.w3.org/TR/2014/WD-DOM-Level-3-Events-20140925/#interface-WheelEvent29*30* Clients of this code should be aware that some input devices only fire a few31* discrete events (such as a mouse wheel without acceleration) whereas some can32* generate a large number of events for a single interaction (such as a33* touchpad with acceleration). There is no signal in the events to reliably34* distinguish between these.35*36* @author [email protected] (Erik Arvidsson)37* @see ../demos/wheelhandler.html38*/3940goog.provide('goog.events.WheelHandler');4142goog.require('goog.dom');43goog.require('goog.events');44goog.require('goog.events.EventTarget');45goog.require('goog.events.WheelEvent');46goog.require('goog.style');47goog.require('goog.userAgent');48goog.require('goog.userAgent.product');49goog.require('goog.userAgent.product.isVersion');50515253/**54* This event handler allows you to catch wheel events in a consistent manner.55* @param {!Element|!Document} element The element to listen to the wheel event56* on.57* @param {boolean=} opt_capture Whether to handle the wheel event in capture58* phase.59* @constructor60* @extends {goog.events.EventTarget}61*/62goog.events.WheelHandler = function(element, opt_capture) {63goog.events.WheelHandler.base(this, 'constructor');6465/**66* This is the element that we will listen to the real wheel events on.67* @private {!Element|!Document}68*/69this.element_ = element;7071var rtlElement = goog.dom.isElement(this.element_) ?72/** @type {!Element} */ (this.element_) :73/** @type {!Document} */ (this.element_).body;7475/**76* True if the element exists and is RTL, false otherwise.77* @private {boolean}78*/79this.isRtl_ = !!rtlElement && goog.style.isRightToLeft(rtlElement);8081/**82* The key returned from the goog.events.listen.83* @private {goog.events.Key}84*/85this.listenKey_ = goog.events.listen(86this.element_, goog.events.WheelHandler.getDomEventType(), this,87opt_capture);88};89goog.inherits(goog.events.WheelHandler, goog.events.EventTarget);909192/**93* Returns the dom event type.94* @return {string} The dom event type.95*/96goog.events.WheelHandler.getDomEventType = function() {97// Prefer to use wheel events whenever supported.98if (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher(17) ||99goog.userAgent.IE && goog.userAgent.isVersionOrHigher(9) ||100goog.userAgent.product.CHROME && goog.userAgent.product.isVersion(31)) {101return 'wheel';102}103104// Legacy events. Still the best we have on Opera and Safari.105return goog.userAgent.GECKO ? 'DOMMouseScroll' : 'mousewheel';106};107108109/**110* Handles the events on the element.111* @param {!goog.events.BrowserEvent} e The underlying browser event.112*/113goog.events.WheelHandler.prototype.handleEvent = function(e) {114var deltaMode = goog.events.WheelEvent.DeltaMode.PIXEL;115var deltaX = 0;116var deltaY = 0;117var deltaZ = 0;118var be = e.getBrowserEvent();119if (be.type == 'wheel') {120deltaMode = be.deltaMode;121deltaX = be.deltaX;122deltaY = be.deltaY;123deltaZ = be.deltaZ;124} else if (be.type == 'mousewheel') {125// Assume that these are still comparable to pixels. This may not be true126// for all old browsers.127if (goog.isDef(be.wheelDeltaX)) {128deltaX = -be.wheelDeltaX;129deltaY = -be.wheelDeltaY;130} else {131deltaY = -be.wheelDelta;132}133} else { // Historical Gecko134// Gecko returns multiple of 3 (representing the number of lines)135deltaMode = goog.events.WheelEvent.DeltaMode.LINE;136// Firefox 3.1 adds an axis field to the event to indicate axis.137if (goog.isDef(be.axis) && be.axis === be.HORIZONTAL_AXIS) {138deltaX = be.detail;139} else {140deltaY = be.detail;141}142}143// For horizontal deltas we need to flip the value for RTL grids.144if (this.isRtl_) {145deltaX = -deltaX;146}147var newEvent =148new goog.events.WheelEvent(be, deltaMode, deltaX, deltaY, deltaZ);149this.dispatchEvent(newEvent);150};151152153/** @override */154goog.events.WheelHandler.prototype.disposeInternal = function() {155goog.events.WheelHandler.superClass_.disposeInternal.call(this);156goog.events.unlistenByKey(this.listenKey_);157this.listenKey_ = null;158};159160161