Debian Codegraph @

closure-library/closure/goog/events/browserevent.js

raw
1 // Copyright 2005 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 A patched, standardized event object for browser events.
17 *
18 * <pre>
19 * The patched event object contains the following members:
20 * - type {string} Event type, e.g. 'click'
21 * - target {Object} The element that actually triggered the event
22 * - currentTarget {Object} The element the listener is attached to
23 * - relatedTarget {Object} For mouseover and mouseout, the previous object
24 * - offsetX {number} X-coordinate relative to target
25 * - offsetY {number} Y-coordinate relative to target
26 * - clientX {number} X-coordinate relative to viewport
27 * - clientY {number} Y-coordinate relative to viewport
28 * - screenX {number} X-coordinate relative to the edge of the screen
29 * - screenY {number} Y-coordinate relative to the edge of the screen
30 * - button {number} Mouse button. Use isButton() to test.
31 * - keyCode {number} Key-code
32 * - ctrlKey {boolean} Was ctrl key depressed
33 * - altKey {boolean} Was alt key depressed
34 * - shiftKey {boolean} Was shift key depressed
35 * - metaKey {boolean} Was meta key depressed
36 * - pointerId {number} Pointer ID
37 * - pointerType {string} Pointer type, e.g. 'mouse', 'pen', or 'touch'
38 * - defaultPrevented {boolean} Whether the default action has been prevented
39 * - state {Object} History state object
40 *
41 * NOTE: The keyCode member contains the raw browser keyCode. For normalized
42 * key and character code use {@link goog.events.KeyHandler}.
43 * </pre>
44 *
45 * @author [email protected] (Erik Arvidsson)
46 */
47
48 goog.provide('goog.events.BrowserEvent');
49 goog.provide('goog.events.BrowserEvent.MouseButton');
50 goog.provide('goog.events.BrowserEvent.PointerType');
51
52 goog.require('goog.debug');
53 goog.require('goog.events.BrowserFeature');
54 goog.require('goog.events.Event');
55 goog.require('goog.events.EventType');
56 goog.require('goog.reflect');
57 goog.require('goog.userAgent');
58
59
60
61 /**
62 * Accepts a browser event object and creates a patched, cross browser event
63 * object.
64 * The content of this object will not be initialized if no event object is
65 * provided. If this is the case, init() needs to be invoked separately.
66 * @param {Event=} opt_e Browser event object.
67 * @param {EventTarget=} opt_currentTarget Current target for event.
68 * @constructor
69 * @extends {goog.events.Event}
70 */
71 goog.events.BrowserEvent = function(opt_e, opt_currentTarget) {
72 goog.events.BrowserEvent.base(this, 'constructor', opt_e ? opt_e.type : '');
73
74 /**
75 * Target that fired the event.
76 * @override
77 * @type {Node}
78 */
79 this.target = null;
80
81 /**
82 * Node that had the listener attached.
83 * @override
84 * @type {Node|undefined}
85 */
86 this.currentTarget = null;
87
88 /**
89 * For mouseover and mouseout events, the related object for the event.
90 * @type {Node}
91 */
92 this.relatedTarget = null;
93
94 /**
95 * X-coordinate relative to target.
96 * @type {number}
97 */
98 this.offsetX = 0;
99
100 /**
101 * Y-coordinate relative to target.
102 * @type {number}
103 */
104 this.offsetY = 0;
105
106 /**
107 * X-coordinate relative to the window.
108 * @type {number}
109 */
110 this.clientX = 0;
111
112 /**
113 * Y-coordinate relative to the window.
114 * @type {number}
115 */
116 this.clientY = 0;
117
118 /**
119 * X-coordinate relative to the monitor.
120 * @type {number}
121 */
122 this.screenX = 0;
123
124 /**
125 * Y-coordinate relative to the monitor.
126 * @type {number}
127 */
128 this.screenY = 0;
129
130 /**
131 * Which mouse button was pressed.
132 * @type {number}
133 */
134 this.button = 0;
135
136 /**
137 * Key of key press.
138 * @type {string}
139 */
140 this.key = '';
141
142 /**
143 * Keycode of key press.
144 * @type {number}
145 */
146 this.keyCode = 0;
147
148 /**
149 * Keycode of key press.
150 * @type {number}
151 */
152 this.charCode = 0;
153
154 /**
155 * Whether control was pressed at time of event.
156 * @type {boolean}
157 */
158 this.ctrlKey = false;
159
160 /**
161 * Whether alt was pressed at time of event.
162 * @type {boolean}
163 */
164 this.altKey = false;
165
166 /**
167 * Whether shift was pressed at time of event.
168 * @type {boolean}
169 */
170 this.shiftKey = false;
171
172 /**
173 * Whether the meta key was pressed at time of event.
174 * @type {boolean}
175 */
176 this.metaKey = false;
177
178 /**
179 * History state object, only set for PopState events where it's a copy of the
180 * state object provided to pushState or replaceState.
181 * @type {Object}
182 */
183 this.state = null;
184
185 /**
186 * Whether the default platform modifier key was pressed at time of event.
187 * (This is control for all platforms except Mac, where it's Meta.)
188 * @type {boolean}
189 */
190 this.platformModifierKey = false;
191
192 /**
193 * @type {number}
194 */
195 this.pointerId = 0;
196
197 /**
198 * @type {string}
199 */
200 this.pointerType = '';
201
202 /**
203 * The browser event object.
204 * @private {Event}
205 */
206 this.event_ = null;
207
208 if (opt_e) {
209 this.init(opt_e, opt_currentTarget);
210 }
211 };
212 goog.inherits(goog.events.BrowserEvent, goog.events.Event);
213
214
215 /**
216 * Normalized button constants for the mouse.
217 * @enum {number}
218 */
219 goog.events.BrowserEvent.MouseButton = {
220 LEFT: 0,
221 MIDDLE: 1,
222 RIGHT: 2
223 };
224
225
226 /**
227 * Normalized pointer type constants for pointer events.
228 * @enum {string}
229 */
230 goog.events.BrowserEvent.PointerType = {
231 MOUSE: 'mouse',
232 PEN: 'pen',
233 TOUCH: 'touch'
234 };
235
236
237 /**
238 * Static data for mapping mouse buttons.
239 * @type {!Array<number>}
240 * @deprecated Use {@code goog.events.BrowserEvent.IE_BUTTON_MAP} instead.
241 */
242 goog.events.BrowserEvent.IEButtonMap = goog.debug.freeze([
243 1, // LEFT
244 4, // MIDDLE
245 2 // RIGHT
246 ]);
247
248
249 /**
250 * Static data for mapping mouse buttons.
251 * @const {!Array<number>}
252 */
253 goog.events.BrowserEvent.IE_BUTTON_MAP = goog.events.BrowserEvent.IEButtonMap;
254
255
256 /**
257 * Static data for mapping MSPointerEvent types to PointerEvent types.
258 * @const {!Object<number, goog.events.BrowserEvent.PointerType>}
259 */
260 goog.events.BrowserEvent.IE_POINTER_TYPE_MAP = goog.debug.freeze({
261 2: goog.events.BrowserEvent.PointerType.TOUCH,
262 3: goog.events.BrowserEvent.PointerType.PEN,
263 4: goog.events.BrowserEvent.PointerType.MOUSE
264 });
265
266
267 /**
268 * Accepts a browser event object and creates a patched, cross browser event
269 * object.
270 * @param {Event} e Browser event object.
271 * @param {EventTarget=} opt_currentTarget Current target for event.
272 */
273 goog.events.BrowserEvent.prototype.init = function(e, opt_currentTarget) {
274 var type = this.type = e.type;
275
276 /**
277 * On touch devices use the first "changed touch" as the relevant touch.
278 * @type {Touch}
279 */
280 var relevantTouch = e.changedTouches ? e.changedTouches[0] : null;
281
282 // TODO(nicksantos): Change this.target to type EventTarget.
283 this.target = /** @type {Node} */ (e.target) || e.srcElement;
284
285 // TODO(nicksantos): Change this.currentTarget to type EventTarget.
286 this.currentTarget = /** @type {Node} */ (opt_currentTarget);
287
288 var relatedTarget = /** @type {Node} */ (e.relatedTarget);
289 if (relatedTarget) {
290 // There's a bug in FireFox where sometimes, relatedTarget will be a
291 // chrome element, and accessing any property of it will get a permission
292 // denied exception. See:
293 // https://bugzilla.mozilla.org/show_bug.cgi?id=497780
294 if (goog.userAgent.GECKO) {
295 if (!goog.reflect.canAccessProperty(relatedTarget, 'nodeName')) {
296 relatedTarget = null;
297 }
298 }
299 } else if (type == goog.events.EventType.MOUSEOVER) {
300 relatedTarget = e.fromElement;
301 } else if (type == goog.events.EventType.MOUSEOUT) {
302 relatedTarget = e.toElement;
303 }
304
305 this.relatedTarget = relatedTarget;
306
307 if (!goog.isNull(relevantTouch)) {
308 this.clientX = relevantTouch.clientX !== undefined ? relevantTouch.clientX :
309 relevantTouch.pageX;
310 this.clientY = relevantTouch.clientY !== undefined ? relevantTouch.clientY :
311 relevantTouch.pageY;
312 this.screenX = relevantTouch.screenX || 0;
313 this.screenY = relevantTouch.screenY || 0;
314 } else {
315 // Webkit emits a lame warning whenever layerX/layerY is accessed.
316 // http://code.google.com/p/chromium/issues/detail?id=101733
317 this.offsetX = (goog.userAgent.WEBKIT || e.offsetX !== undefined) ?
318 e.offsetX :
319 e.layerX;
320 this.offsetY = (goog.userAgent.WEBKIT || e.offsetY !== undefined) ?
321 e.offsetY :
322 e.layerY;
323 this.clientX = e.clientX !== undefined ? e.clientX : e.pageX;
324 this.clientY = e.clientY !== undefined ? e.clientY : e.pageY;
325 this.screenX = e.screenX || 0;
326 this.screenY = e.screenY || 0;
327 }
328
329 this.button = e.button;
330
331 this.keyCode = e.keyCode || 0;
332 this.key = e.key || '';
333 this.charCode = e.charCode || (type == 'keypress' ? e.keyCode : 0);
334 this.ctrlKey = e.ctrlKey;
335 this.altKey = e.altKey;
336 this.shiftKey = e.shiftKey;
337 this.metaKey = e.metaKey;
338 this.platformModifierKey = goog.userAgent.MAC ? e.metaKey : e.ctrlKey;
339 this.pointerId = e.pointerId || 0;
340 this.pointerType = goog.events.BrowserEvent.getPointerType_(e);
341 this.state = e.state;
342 this.event_ = e;
343 if (e.defaultPrevented) {
344 this.preventDefault();
345 }
346 };
347
348
349 /**
350 * Tests to see which button was pressed during the event. This is really only
351 * useful in IE and Gecko browsers. And in IE, it's only useful for
352 * mousedown/mouseup events, because click only fires for the left mouse button.
353 *
354 * Safari 2 only reports the left button being clicked, and uses the value '1'
355 * instead of 0. Opera only reports a mousedown event for the middle button, and
356 * no mouse events for the right button. Opera has default behavior for left and
357 * middle click that can only be overridden via a configuration setting.
358 *
359 * There's a nice table of this mess at http://www.unixpapa.com/js/mouse.html.
360 *
361 * @param {goog.events.BrowserEvent.MouseButton} button The button
362 * to test for.
363 * @return {boolean} True if button was pressed.
364 */
365 goog.events.BrowserEvent.prototype.isButton = function(button) {
366 if (!goog.events.BrowserFeature.HAS_W3C_BUTTON) {
367 if (this.type == 'click') {
368 return button == goog.events.BrowserEvent.MouseButton.LEFT;
369 } else {
370 return !!(
371 this.event_.button & goog.events.BrowserEvent.IE_BUTTON_MAP[button]);
372 }
373 } else {
374 return this.event_.button == button;
375 }
376 };
377
378
379 /**
380 * Whether this has an "action"-producing mouse button.
381 *
382 * By definition, this includes left-click on windows/linux, and left-click
383 * without the ctrl key on Macs.
384 *
385 * @return {boolean} The result.
386 */
387 goog.events.BrowserEvent.prototype.isMouseActionButton = function() {
388 // Webkit does not ctrl+click to be a right-click, so we
389 // normalize it to behave like Gecko and Opera.
390 return this.isButton(goog.events.BrowserEvent.MouseButton.LEFT) &&
391 !(goog.userAgent.WEBKIT && goog.userAgent.MAC && this.ctrlKey);
392 };
393
394
395 /**
396 * @override
397 */
398 goog.events.BrowserEvent.prototype.stopPropagation = function() {
399 goog.events.BrowserEvent.superClass_.stopPropagation.call(this);
400 if (this.event_.stopPropagation) {
401 this.event_.stopPropagation();
402 } else {
403 this.event_.cancelBubble = true;
404 }
405 };
406
407
408 /**
409 * @override
410 */
411 goog.events.BrowserEvent.prototype.preventDefault = function() {
412 goog.events.BrowserEvent.superClass_.preventDefault.call(this);
413 var be = this.event_;
414 if (!be.preventDefault) {
415 be.returnValue = false;
416 if (goog.events.BrowserFeature.SET_KEY_CODE_TO_PREVENT_DEFAULT) {
417
418 try {
419 // Most keys can be prevented using returnValue. Some special keys
420 // require setting the keyCode to -1 as well:
421 //
422 // In IE7:
423 // F3, F5, F10, F11, Ctrl+P, Crtl+O, Ctrl+F (these are taken from IE6)
424 //
425 // In IE8:
426 // Ctrl+P, Crtl+O, Ctrl+F (F1-F12 cannot be stopped through the event)
427 //
428 // We therefore do this for all function keys as well as when Ctrl key
429 // is pressed.
430 var VK_F1 = 112;
431 var VK_F12 = 123;
432 if (be.ctrlKey || be.keyCode >= VK_F1 && be.keyCode <= VK_F12) {
433 be.keyCode = -1;
434 }
435 } catch (ex) {
436 // IE throws an 'access denied' exception when trying to change
437 // keyCode in some situations (e.g. srcElement is input[type=file],
438 // or srcElement is an anchor tag rewritten by parent's innerHTML).
439 // Do nothing in this case.
440 }
441 }
442 } else {
443 be.preventDefault();
444 }
445 };
446
447
448 /**
449 * @return {Event} The underlying browser event object.
450 */
451 goog.events.BrowserEvent.prototype.getBrowserEvent = function() {
452 return this.event_;
453 };
454
455
456 /**
457 * Extracts the pointer type from the given event.
458 * @param {!Event} e
459 * @return {string} The pointer type, e.g. 'mouse', 'pen', or 'touch'.
460 * @private
461 */
462 goog.events.BrowserEvent.getPointerType_ = function(e) {
463 if (goog.isString(e.pointerType)) {
464 return e.pointerType;
465 }
466 // IE10 uses integer codes for pointer type.
467 // https://msdn.microsoft.com/en-us/library/hh772359(v=vs.85).aspx
468 return goog.events.BrowserEvent.IE_POINTER_TYPE_MAP[e.pointerType] || '';
469 };