Path: blob/trunk/third_party/closure/goog/events/browserevent.js
2868 views
// Copyright 2005 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 A patched, standardized event object for browser events.16*17* <pre>18* The patched event object contains the following members:19* - type {string} Event type, e.g. 'click'20* - target {Object} The element that actually triggered the event21* - currentTarget {Object} The element the listener is attached to22* - relatedTarget {Object} For mouseover and mouseout, the previous object23* - offsetX {number} X-coordinate relative to target24* - offsetY {number} Y-coordinate relative to target25* - clientX {number} X-coordinate relative to viewport26* - clientY {number} Y-coordinate relative to viewport27* - screenX {number} X-coordinate relative to the edge of the screen28* - screenY {number} Y-coordinate relative to the edge of the screen29* - button {number} Mouse button. Use isButton() to test.30* - keyCode {number} Key-code31* - ctrlKey {boolean} Was ctrl key depressed32* - altKey {boolean} Was alt key depressed33* - shiftKey {boolean} Was shift key depressed34* - metaKey {boolean} Was meta key depressed35* - defaultPrevented {boolean} Whether the default action has been prevented36* - state {Object} History state object37*38* NOTE: The keyCode member contains the raw browser keyCode. For normalized39* key and character code use {@link goog.events.KeyHandler}.40* </pre>41*42* @author [email protected] (Erik Arvidsson)43*/4445goog.provide('goog.events.BrowserEvent');46goog.provide('goog.events.BrowserEvent.MouseButton');4748goog.require('goog.events.BrowserFeature');49goog.require('goog.events.Event');50goog.require('goog.events.EventType');51goog.require('goog.reflect');52goog.require('goog.userAgent');53545556/**57* Accepts a browser event object and creates a patched, cross browser event58* object.59* The content of this object will not be initialized if no event object is60* provided. If this is the case, init() needs to be invoked separately.61* @param {Event=} opt_e Browser event object.62* @param {EventTarget=} opt_currentTarget Current target for event.63* @constructor64* @extends {goog.events.Event}65*/66goog.events.BrowserEvent = function(opt_e, opt_currentTarget) {67goog.events.BrowserEvent.base(this, 'constructor', opt_e ? opt_e.type : '');6869/**70* Target that fired the event.71* @override72* @type {Node}73*/74this.target = null;7576/**77* Node that had the listener attached.78* @override79* @type {Node|undefined}80*/81this.currentTarget = null;8283/**84* For mouseover and mouseout events, the related object for the event.85* @type {Node}86*/87this.relatedTarget = null;8889/**90* X-coordinate relative to target.91* @type {number}92*/93this.offsetX = 0;9495/**96* Y-coordinate relative to target.97* @type {number}98*/99this.offsetY = 0;100101/**102* X-coordinate relative to the window.103* @type {number}104*/105this.clientX = 0;106107/**108* Y-coordinate relative to the window.109* @type {number}110*/111this.clientY = 0;112113/**114* X-coordinate relative to the monitor.115* @type {number}116*/117this.screenX = 0;118119/**120* Y-coordinate relative to the monitor.121* @type {number}122*/123this.screenY = 0;124125/**126* Which mouse button was pressed.127* @type {number}128*/129this.button = 0;130131/**132* Key of key press.133* @type {string}134*/135this.key = '';136137/**138* Keycode of key press.139* @type {number}140*/141this.keyCode = 0;142143/**144* Keycode of key press.145* @type {number}146*/147this.charCode = 0;148149/**150* Whether control was pressed at time of event.151* @type {boolean}152*/153this.ctrlKey = false;154155/**156* Whether alt was pressed at time of event.157* @type {boolean}158*/159this.altKey = false;160161/**162* Whether shift was pressed at time of event.163* @type {boolean}164*/165this.shiftKey = false;166167/**168* Whether the meta key was pressed at time of event.169* @type {boolean}170*/171this.metaKey = false;172173/**174* History state object, only set for PopState events where it's a copy of the175* state object provided to pushState or replaceState.176* @type {Object}177*/178this.state = null;179180/**181* Whether the default platform modifier key was pressed at time of event.182* (This is control for all platforms except Mac, where it's Meta.)183* @type {boolean}184*/185this.platformModifierKey = false;186187/**188* The browser event object.189* @private {Event}190*/191this.event_ = null;192193if (opt_e) {194this.init(opt_e, opt_currentTarget);195}196};197goog.inherits(goog.events.BrowserEvent, goog.events.Event);198199200/**201* Normalized button constants for the mouse.202* @enum {number}203*/204goog.events.BrowserEvent.MouseButton = {205LEFT: 0,206MIDDLE: 1,207RIGHT: 2208};209210211/**212* Static data for mapping mouse buttons.213* @type {!Array<number>}214*/215goog.events.BrowserEvent.IEButtonMap = [2161, // LEFT2174, // MIDDLE2182 // RIGHT219];220221222/**223* Accepts a browser event object and creates a patched, cross browser event224* object.225* @param {Event} e Browser event object.226* @param {EventTarget=} opt_currentTarget Current target for event.227*/228goog.events.BrowserEvent.prototype.init = function(e, opt_currentTarget) {229var type = this.type = e.type;230231/**232* On touch devices use the first "changed touch" as the relevant touch.233* @type {Touch}234*/235var relevantTouch = e.changedTouches ? e.changedTouches[0] : null;236237// TODO(nicksantos): Change this.target to type EventTarget.238this.target = /** @type {Node} */ (e.target) || e.srcElement;239240// TODO(nicksantos): Change this.currentTarget to type EventTarget.241this.currentTarget = /** @type {Node} */ (opt_currentTarget);242243var relatedTarget = /** @type {Node} */ (e.relatedTarget);244if (relatedTarget) {245// There's a bug in FireFox where sometimes, relatedTarget will be a246// chrome element, and accessing any property of it will get a permission247// denied exception. See:248// https://bugzilla.mozilla.org/show_bug.cgi?id=497780249if (goog.userAgent.GECKO) {250if (!goog.reflect.canAccessProperty(relatedTarget, 'nodeName')) {251relatedTarget = null;252}253}254// TODO(arv): Use goog.events.EventType when it has been refactored into its255// own file.256} else if (type == goog.events.EventType.MOUSEOVER) {257relatedTarget = e.fromElement;258} else if (type == goog.events.EventType.MOUSEOUT) {259relatedTarget = e.toElement;260}261262this.relatedTarget = relatedTarget;263264if (!goog.isNull(relevantTouch)) {265this.clientX = relevantTouch.clientX !== undefined ? relevantTouch.clientX :266relevantTouch.pageX;267this.clientY = relevantTouch.clientY !== undefined ? relevantTouch.clientY :268relevantTouch.pageY;269this.screenX = relevantTouch.screenX || 0;270this.screenY = relevantTouch.screenY || 0;271} else {272// Webkit emits a lame warning whenever layerX/layerY is accessed.273// http://code.google.com/p/chromium/issues/detail?id=101733274this.offsetX = (goog.userAgent.WEBKIT || e.offsetX !== undefined) ?275e.offsetX :276e.layerX;277this.offsetY = (goog.userAgent.WEBKIT || e.offsetY !== undefined) ?278e.offsetY :279e.layerY;280this.clientX = e.clientX !== undefined ? e.clientX : e.pageX;281this.clientY = e.clientY !== undefined ? e.clientY : e.pageY;282this.screenX = e.screenX || 0;283this.screenY = e.screenY || 0;284}285286this.button = e.button;287288this.keyCode = e.keyCode || 0;289this.key = e.key || '';290this.charCode = e.charCode || (type == 'keypress' ? e.keyCode : 0);291this.ctrlKey = e.ctrlKey;292this.altKey = e.altKey;293this.shiftKey = e.shiftKey;294this.metaKey = e.metaKey;295this.platformModifierKey = goog.userAgent.MAC ? e.metaKey : e.ctrlKey;296this.state = e.state;297this.event_ = e;298if (e.defaultPrevented) {299this.preventDefault();300}301};302303304/**305* Tests to see which button was pressed during the event. This is really only306* useful in IE and Gecko browsers. And in IE, it's only useful for307* mousedown/mouseup events, because click only fires for the left mouse button.308*309* Safari 2 only reports the left button being clicked, and uses the value '1'310* instead of 0. Opera only reports a mousedown event for the middle button, and311* no mouse events for the right button. Opera has default behavior for left and312* middle click that can only be overridden via a configuration setting.313*314* There's a nice table of this mess at http://www.unixpapa.com/js/mouse.html.315*316* @param {goog.events.BrowserEvent.MouseButton} button The button317* to test for.318* @return {boolean} True if button was pressed.319*/320goog.events.BrowserEvent.prototype.isButton = function(button) {321if (!goog.events.BrowserFeature.HAS_W3C_BUTTON) {322if (this.type == 'click') {323return button == goog.events.BrowserEvent.MouseButton.LEFT;324} else {325return !!(326this.event_.button & goog.events.BrowserEvent.IEButtonMap[button]);327}328} else {329return this.event_.button == button;330}331};332333334/**335* Whether this has an "action"-producing mouse button.336*337* By definition, this includes left-click on windows/linux, and left-click338* without the ctrl key on Macs.339*340* @return {boolean} The result.341*/342goog.events.BrowserEvent.prototype.isMouseActionButton = function() {343// Webkit does not ctrl+click to be a right-click, so we344// normalize it to behave like Gecko and Opera.345return this.isButton(goog.events.BrowserEvent.MouseButton.LEFT) &&346!(goog.userAgent.WEBKIT && goog.userAgent.MAC && this.ctrlKey);347};348349350/**351* @override352*/353goog.events.BrowserEvent.prototype.stopPropagation = function() {354goog.events.BrowserEvent.superClass_.stopPropagation.call(this);355if (this.event_.stopPropagation) {356this.event_.stopPropagation();357} else {358this.event_.cancelBubble = true;359}360};361362363/**364* @override365*/366goog.events.BrowserEvent.prototype.preventDefault = function() {367goog.events.BrowserEvent.superClass_.preventDefault.call(this);368var be = this.event_;369if (!be.preventDefault) {370be.returnValue = false;371if (goog.events.BrowserFeature.SET_KEY_CODE_TO_PREVENT_DEFAULT) {372373try {374// Most keys can be prevented using returnValue. Some special keys375// require setting the keyCode to -1 as well:376//377// In IE7:378// F3, F5, F10, F11, Ctrl+P, Crtl+O, Ctrl+F (these are taken from IE6)379//380// In IE8:381// Ctrl+P, Crtl+O, Ctrl+F (F1-F12 cannot be stopped through the event)382//383// We therefore do this for all function keys as well as when Ctrl key384// is pressed.385var VK_F1 = 112;386var VK_F12 = 123;387if (be.ctrlKey || be.keyCode >= VK_F1 && be.keyCode <= VK_F12) {388be.keyCode = -1;389}390} catch (ex) {391// IE throws an 'access denied' exception when trying to change392// keyCode in some situations (e.g. srcElement is input[type=file],393// or srcElement is an anchor tag rewritten by parent's innerHTML).394// Do nothing in this case.395}396}397} else {398be.preventDefault();399}400};401402403/**404* @return {Event} The underlying browser event object.405*/406goog.events.BrowserEvent.prototype.getBrowserEvent = function() {407return this.event_;408};409410411