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