Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/events.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
/**
19
* @fileoverview Functions to do with firing and simulating events.
20
*/
21
22
23
goog.provide('bot.events');
24
goog.provide('bot.events.EventArgs');
25
goog.provide('bot.events.EventType');
26
goog.provide('bot.events.KeyboardArgs');
27
goog.provide('bot.events.MSGestureArgs');
28
goog.provide('bot.events.MSPointerArgs');
29
goog.provide('bot.events.MouseArgs');
30
goog.provide('bot.events.Touch');
31
goog.provide('bot.events.TouchArgs');
32
33
goog.require('bot');
34
goog.require('bot.Error');
35
goog.require('bot.ErrorCode');
36
goog.require('bot.userAgent');
37
goog.require('goog.array');
38
goog.require('goog.dom');
39
goog.require('goog.events.BrowserEvent');
40
goog.require('goog.style');
41
goog.require('goog.userAgent');
42
goog.require('goog.userAgent.product');
43
44
45
/**
46
* Whether the browser supports the construction of touch events.
47
*
48
* @const
49
* @type {boolean}
50
*/
51
bot.events.SUPPORTS_TOUCH_EVENTS = !(goog.userAgent.IE &&
52
!bot.userAgent.isEngineVersion(10));
53
54
55
/**
56
* Whether the browser supports a native touch api.
57
* @private {boolean}
58
* @const
59
*/
60
bot.events.BROKEN_TOUCH_API_ = (function () {
61
if (goog.userAgent.product.ANDROID) {
62
// Native touch api supported starting in version 4.0 (Ice Cream Sandwich).
63
return !bot.userAgent.isProductVersion(4);
64
}
65
return !bot.userAgent.IOS;
66
})();
67
68
69
/**
70
* Whether the browser supports the construction of MSPointer events.
71
*
72
* @const
73
* @type {boolean}
74
*/
75
bot.events.SUPPORTS_MSPOINTER_EVENTS =
76
goog.userAgent.IE && bot.getWindow().navigator.msPointerEnabled;
77
78
79
/**
80
* Arguments to initialize an event.
81
*
82
* @typedef {bot.events.MouseArgs|bot.events.KeyboardArgs|bot.events.TouchArgs|
83
bot.events.MSGestureArgs|bot.events.MSPointerArgs}
84
*/
85
bot.events.EventArgs;
86
87
88
/**
89
* Arguments to initialize a mouse event.
90
*
91
* @typedef {{clientX: number,
92
* clientY: number,
93
* button: number,
94
* altKey: boolean,
95
* ctrlKey: boolean,
96
* shiftKey: boolean,
97
* metaKey: boolean,
98
* relatedTarget: Element,
99
* wheelDelta: number}}
100
*/
101
bot.events.MouseArgs;
102
103
104
/**
105
* Arguments to initialize a keyboard event.
106
*
107
* @typedef {{keyCode: number,
108
* charCode: number,
109
* altKey: boolean,
110
* ctrlKey: boolean,
111
* shiftKey: boolean,
112
* metaKey: boolean,
113
* preventDefault: boolean}}
114
*/
115
bot.events.KeyboardArgs;
116
117
118
/**
119
* Argument to initialize a touch event.
120
*
121
* @typedef {{touches: !Array.<bot.events.Touch>,
122
* targetTouches: !Array.<bot.events.Touch>,
123
* changedTouches: !Array.<bot.events.Touch>,
124
* altKey: boolean,
125
* ctrlKey: boolean,
126
* shiftKey: boolean,
127
* metaKey: boolean,
128
* relatedTarget: Element,
129
* scale: number,
130
* rotation: number}}
131
*/
132
bot.events.TouchArgs;
133
134
135
/**
136
* @typedef {{identifier: number,
137
* screenX: number,
138
* screenY: number,
139
* clientX: number,
140
* clientY: number,
141
* pageX: number,
142
* pageY: number}}
143
*/
144
bot.events.Touch;
145
146
147
/**
148
* Arguments to initialize an MSGesture event.
149
*
150
* @typedef {{clientX: number,
151
* clientY: number,
152
* translationX: number,
153
* translationY: number,
154
* scale: number,
155
* expansion: number,
156
* rotation: number,
157
* velocityX: number,
158
* velocityY: number,
159
* velocityExpansion: number,
160
* velocityAngular: number,
161
* relatedTarget: Element}}
162
*/
163
bot.events.MSGestureArgs;
164
165
166
/**
167
* Arguments to initialize an MSPointer event.
168
*
169
* @typedef {{clientX: number,
170
* clientY: number,
171
* button: number,
172
* altKey: boolean,
173
* ctrlKey: boolean,
174
* shiftKey: boolean,
175
* metaKey: boolean,
176
* relatedTarget: Element,
177
* width: number,
178
* height: number,
179
* pressure: number,
180
* rotation: number,
181
* pointerId: number,
182
* tiltX: number,
183
* tiltY: number,
184
* pointerType: number,
185
* isPrimary: boolean}}
186
*/
187
bot.events.MSPointerArgs;
188
189
190
191
/**
192
* Factory for event objects of a specific type.
193
*
194
* @constructor
195
* @param {string} type Type of the created events.
196
* @param {boolean} bubbles Whether the created events bubble.
197
* @param {boolean} cancelable Whether the created events are cancelable.
198
* @private
199
*/
200
bot.events.EventFactory_ = function (type, bubbles, cancelable) {
201
/** @private {string} */
202
this.type_ = type;
203
204
/** @private {boolean} */
205
this.bubbles_ = bubbles;
206
207
/** @private {boolean} */
208
this.cancelable_ = cancelable;
209
};
210
211
212
/**
213
* Creates an event.
214
*
215
* @param {!Element|!Window} target Target element of the event.
216
* @param {bot.events.EventArgs=} opt_args Event arguments.
217
* @return {!Event} Newly created event.
218
*/
219
bot.events.EventFactory_.prototype.create = function (target, opt_args) {
220
var doc = goog.dom.getOwnerDocument(target);
221
222
var event = doc.createEvent('HTMLEvents');
223
event.initEvent(this.type_, this.bubbles_, this.cancelable_);
224
225
return event;
226
};
227
228
229
/**
230
* Overriding toString to return the unique type string improves debugging,
231
* and it allows event types to be mapped in JS objects without collisions.
232
*
233
* @return {string} String representation of the event type.
234
* @override
235
*/
236
bot.events.EventFactory_.prototype.toString = function () {
237
return this.type_;
238
};
239
240
241
242
/**
243
* Factory for mouse event objects of a specific type.
244
*
245
* @constructor
246
* @param {string} type Type of the created events.
247
* @param {boolean} bubbles Whether the created events bubble.
248
* @param {boolean} cancelable Whether the created events are cancelable.
249
* @extends {bot.events.EventFactory_}
250
* @private
251
*/
252
bot.events.MouseEventFactory_ = function (type, bubbles, cancelable) {
253
goog.base(this, type, bubbles, cancelable);
254
};
255
goog.inherits(bot.events.MouseEventFactory_, bot.events.EventFactory_);
256
257
258
/**
259
* @override
260
*/
261
bot.events.MouseEventFactory_.prototype.create = function (target, opt_args) {
262
// Only Gecko supports the mouse pixel scroll event.
263
if (!goog.userAgent.GECKO && this == bot.events.EventType.MOUSEPIXELSCROLL) {
264
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
265
'Browser does not support a mouse pixel scroll event.');
266
}
267
268
var args = /** @type {!bot.events.MouseArgs} */ (opt_args);
269
var doc = goog.dom.getOwnerDocument(target);
270
var event;
271
272
var view = goog.dom.getWindow(doc);
273
event = doc.createEvent('MouseEvents');
274
var detail = 1;
275
276
// All browser but Firefox provide the wheelDelta value in the event.
277
// Firefox provides the scroll amount in the detail field, where it has the
278
// opposite polarity of the wheelDelta (upward scroll is negative) and is a
279
// factor of 40 less than the wheelDelta value.
280
// The wheelDelta value is normally some multiple of 40.
281
if (this == bot.events.EventType.MOUSEWHEEL) {
282
if (!goog.userAgent.GECKO) {
283
event.wheelDelta = args.wheelDelta;
284
}
285
if (goog.userAgent.GECKO) {
286
detail = args.wheelDelta / -40;
287
}
288
}
289
290
// Only Gecko supports a mouse pixel scroll event, so we use it as the
291
// "standard" and pass it along as is as the "detail" of the event.
292
if (goog.userAgent.GECKO && this == bot.events.EventType.MOUSEPIXELSCROLL) {
293
detail = args.wheelDelta;
294
}
295
296
// For screenX and screenY, we set those to clientX and clientY values.
297
// While not strictly correct, applications under test depend on
298
// accurate relative positioning which is satisfied.
299
event.initMouseEvent(this.type_, this.bubbles_, this.cancelable_, view,
300
detail, /*screenX*/ args.clientX, /*screenY*/ args.clientY,
301
args.clientX, args.clientY, args.ctrlKey, args.altKey,
302
args.shiftKey, args.metaKey, args.button, args.relatedTarget);
303
304
// Trying to modify the properties throws an error,
305
// so we define getters to return the correct values.
306
if (goog.userAgent.IE &&
307
event.pageX === 0 && event.pageY === 0 && Object.defineProperty) {
308
var scrollElem = goog.dom.getDomHelper(target).getDocumentScrollElement();
309
var clientElem = goog.style.getClientViewportElement(doc);
310
var pageX = args.clientX + scrollElem.scrollLeft - clientElem.clientLeft;
311
var pageY = args.clientY + scrollElem.scrollTop - clientElem.clientTop;
312
313
Object.defineProperty(event, 'pageX', {
314
get: function () {
315
return pageX;
316
}
317
});
318
Object.defineProperty(event, 'pageY', {
319
get: function () {
320
return pageY;
321
}
322
});
323
}
324
325
return event;
326
};
327
328
329
330
/**
331
* Factory for keyboard event objects of a specific type.
332
*
333
* @constructor
334
* @param {string} type Type of the created events.
335
* @param {boolean} bubbles Whether the created events bubble.
336
* @param {boolean} cancelable Whether the created events are cancelable.
337
* @extends {bot.events.EventFactory_}
338
* @private
339
*/
340
bot.events.KeyboardEventFactory_ = function (type, bubbles, cancelable) {
341
goog.base(this, type, bubbles, cancelable);
342
};
343
goog.inherits(bot.events.KeyboardEventFactory_, bot.events.EventFactory_);
344
345
346
/**
347
* @override
348
*/
349
bot.events.KeyboardEventFactory_.prototype.create = function (target, opt_args) {
350
var args = /** @type {!bot.events.KeyboardArgs} */ (opt_args);
351
var doc = goog.dom.getOwnerDocument(target);
352
var event;
353
354
if (goog.userAgent.GECKO && !bot.userAgent.isEngineVersion(93)) {
355
var view = goog.dom.getWindow(doc);
356
var keyCode = args.charCode ? 0 : args.keyCode;
357
event = doc.createEvent('KeyboardEvent');
358
event.initKeyEvent(this.type_, this.bubbles_, this.cancelable_, view,
359
args.ctrlKey, args.altKey, args.shiftKey, args.metaKey, keyCode,
360
args.charCode);
361
// https://bugzilla.mozilla.org/show_bug.cgi?id=501496
362
if (this.type_ == bot.events.EventType.KEYPRESS && args.preventDefault) {
363
event.preventDefault();
364
}
365
} else {
366
event = doc.createEvent('Events');
367
event.initEvent(this.type_, this.bubbles_, this.cancelable_);
368
event.altKey = args.altKey;
369
event.ctrlKey = args.ctrlKey;
370
event.metaKey = args.metaKey;
371
event.shiftKey = args.shiftKey;
372
event.keyCode = args.charCode || args.keyCode;
373
if (goog.userAgent.WEBKIT || goog.userAgent.EDGE) {
374
event.charCode = (this == bot.events.EventType.KEYPRESS) ?
375
event.keyCode : 0;
376
}
377
}
378
379
return event;
380
};
381
382
383
384
/**
385
* Enum representing which mechanism to use for creating touch events.
386
* @enum {number}
387
* @private
388
*/
389
bot.events.TouchEventStrategy_ = {
390
MOUSE_EVENTS: 1,
391
INIT_TOUCH_EVENT: 2,
392
TOUCH_EVENT_CTOR: 3
393
};
394
395
396
397
/**
398
* Factory for touch event objects of a specific type.
399
*
400
* @constructor
401
* @param {string} type Type of the created events.
402
* @param {boolean} bubbles Whether the created events bubble.
403
* @param {boolean} cancelable Whether the created events are cancelable.
404
* @extends {bot.events.EventFactory_}
405
* @private
406
*/
407
bot.events.TouchEventFactory_ = function (type, bubbles, cancelable) {
408
goog.base(this, type, bubbles, cancelable);
409
};
410
goog.inherits(bot.events.TouchEventFactory_, bot.events.EventFactory_);
411
412
413
/**
414
* @override
415
*/
416
bot.events.TouchEventFactory_.prototype.create = function (target, opt_args) {
417
if (!bot.events.SUPPORTS_TOUCH_EVENTS) {
418
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
419
'Browser does not support firing touch events.');
420
}
421
422
var args = /** @type {!bot.events.TouchArgs} */ (opt_args);
423
var doc = goog.dom.getOwnerDocument(target);
424
var view = goog.dom.getWindow(doc);
425
426
// Creates a TouchList, using native touch Api, for touch events.
427
function createNativeTouchList(touchListArgs) {
428
var touches = goog.array.map(touchListArgs, function (touchArg) {
429
return doc.createTouch(view, target, touchArg.identifier,
430
touchArg.pageX, touchArg.pageY, touchArg.screenX, touchArg.screenY);
431
});
432
433
return doc.createTouchList.apply(doc, touches);
434
}
435
436
// Creates a TouchList, using simulated touch Api, for touch events.
437
function createGenericTouchList(touchListArgs) {
438
var touches = goog.array.map(touchListArgs, function (touchArg) {
439
// The target field is not part of the W3C spec, but both android and iOS
440
// add the target field to each touch.
441
return {
442
identifier: touchArg.identifier,
443
screenX: touchArg.screenX,
444
screenY: touchArg.screenY,
445
clientX: touchArg.clientX,
446
clientY: touchArg.clientY,
447
pageX: touchArg.pageX,
448
pageY: touchArg.pageY,
449
target: target
450
};
451
});
452
touches.item = function (i) {
453
return touches[i];
454
};
455
return touches;
456
}
457
458
function createTouchEventTouchList(touchListArgs) {
459
/** @type {!Array<!Touch>} */
460
var touches = goog.array.map(touchListArgs, function (touchArg) {
461
return new Touch({
462
identifier: touchArg.identifier,
463
screenX: touchArg.screenX,
464
screenY: touchArg.screenY,
465
clientX: touchArg.clientX,
466
clientY: touchArg.clientY,
467
pageX: touchArg.pageX,
468
pageY: touchArg.pageY,
469
target: target
470
});
471
});
472
return touches;
473
}
474
475
function createTouchList(touchStrategy, touches) {
476
switch (touchStrategy) {
477
case bot.events.TouchEventStrategy_.MOUSE_EVENTS:
478
return createGenericTouchList(touches);
479
case bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT:
480
return createNativeTouchList(touches);
481
case bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR:
482
return createTouchEventTouchList(touches);
483
}
484
return null;
485
}
486
487
// TODO(juangj): Always use the TouchEvent constructor, if available.
488
var strategy;
489
if (bot.events.BROKEN_TOUCH_API_) {
490
strategy = bot.events.TouchEventStrategy_.MOUSE_EVENTS;
491
} else {
492
if (TouchEvent.prototype.initTouchEvent) {
493
strategy = bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT;
494
} else if (TouchEvent && TouchEvent.length > 0) {
495
strategy = bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR;
496
} else {
497
throw new bot.Error(
498
bot.ErrorCode.UNSUPPORTED_OPERATION,
499
'Not able to create touch events in this browser');
500
}
501
}
502
503
// As a performance optimization, reuse the created touchlist when the lists
504
// are the same, which is often the case in practice.
505
var changedTouches = createTouchList(strategy, args.changedTouches);
506
var touches = (args.touches == args.changedTouches) ?
507
changedTouches : createTouchList(strategy, args.touches);
508
var targetTouches = (args.targetTouches == args.changedTouches) ?
509
changedTouches : createTouchList(strategy, args.targetTouches);
510
511
var event;
512
if (strategy == bot.events.TouchEventStrategy_.MOUSE_EVENTS) {
513
event = doc.createEvent('MouseEvents');
514
event.initMouseEvent(this.type_, this.bubbles_, this.cancelable_, view,
515
/*detail*/ 1, /*screenX*/ 0, /*screenY*/ 0, args.clientX, args.clientY,
516
args.ctrlKey, args.altKey, args.shiftKey, args.metaKey, /*button*/ 0,
517
args.relatedTarget);
518
event.touches = touches;
519
event.targetTouches = targetTouches;
520
event.changedTouches = changedTouches;
521
event.scale = args.scale;
522
event.rotation = args.rotation;
523
} else if (strategy == bot.events.TouchEventStrategy_.INIT_TOUCH_EVENT) {
524
event = doc.createEvent('TouchEvent');
525
// Different browsers have different implementations of initTouchEvent.
526
if (event.initTouchEvent.length == 0) {
527
// Chrome/Android.
528
event.initTouchEvent(touches, targetTouches, changedTouches,
529
this.type_, view, /*screenX*/ 0, /*screenY*/ 0, args.clientX,
530
args.clientY, args.ctrlKey, args.altKey, args.shiftKey, args.metaKey);
531
} else {
532
// iOS.
533
event.initTouchEvent(this.type_, this.bubbles_, this.cancelable_, view,
534
/*detail*/ 1, /*screenX*/ 0, /*screenY*/ 0, args.clientX,
535
args.clientY, args.ctrlKey, args.altKey, args.shiftKey, args.metaKey,
536
touches, targetTouches, changedTouches, args.scale, args.rotation);
537
}
538
event.relatedTarget = args.relatedTarget;
539
} else if (strategy == bot.events.TouchEventStrategy_.TOUCH_EVENT_CTOR) {
540
var touchProperties = /** @type {!TouchEventInit} */ ({
541
touches: touches,
542
targetTouches: targetTouches,
543
changedTouches: changedTouches,
544
bubbles: this.bubbles_,
545
cancelable: this.cancelable_,
546
ctrlKey: args.ctrlKey,
547
shiftKey: args.shiftKey,
548
altKey: args.altKey,
549
metaKey: args.metaKey
550
});
551
event = new TouchEvent(this.type_, touchProperties);
552
} else {
553
throw new bot.Error(
554
bot.ErrorCode.UNSUPPORTED_OPERATION,
555
'Illegal TouchEventStrategy_ value (this is a bug)');
556
}
557
558
return event;
559
};
560
561
562
563
/**
564
* Factory for MSGesture event objects of a specific type.
565
*
566
* @constructor
567
* @param {string} type Type of the created events.
568
* @param {boolean} bubbles Whether the created events bubble.
569
* @param {boolean} cancelable Whether the created events are cancelable.
570
* @extends {bot.events.EventFactory_}
571
* @private
572
*/
573
bot.events.MSGestureEventFactory_ = function (type, bubbles, cancelable) {
574
goog.base(this, type, bubbles, cancelable);
575
};
576
goog.inherits(bot.events.MSGestureEventFactory_, bot.events.EventFactory_);
577
578
579
/**
580
* @override
581
*/
582
bot.events.MSGestureEventFactory_.prototype.create = function (target,
583
opt_args) {
584
if (!bot.events.SUPPORTS_MSPOINTER_EVENTS) {
585
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
586
'Browser does not support MSGesture events.');
587
}
588
589
var args = /** @type {!bot.events.MSGestureArgs} */ (opt_args);
590
var doc = goog.dom.getOwnerDocument(target);
591
var view = goog.dom.getWindow(doc);
592
var event = doc.createEvent('MSGestureEvent');
593
var timestamp = (new Date).getTime();
594
595
// See http://msdn.microsoft.com/en-us/library/windows/apps/hh441187.aspx
596
event.initGestureEvent(this.type_, this.bubbles_, this.cancelable_, view,
597
/*detail*/ 1, /*screenX*/ 0, /*screenY*/ 0,
598
args.clientX, args.clientY, /*offsetX*/ 0,
599
/*offsetY*/ 0, args.translationX, args.translationY,
600
args.scale, args.expansion, args.rotation,
601
args.velocityX, args.velocityY, args.velocityExpansion,
602
args.velocityAngular, timestamp, args.relatedTarget);
603
return event;
604
};
605
606
607
608
/**
609
* Factory for MSPointer event objects of a specific type.
610
*
611
* @constructor
612
* @param {string} type Type of the created events.
613
* @param {boolean} bubbles Whether the created events bubble.
614
* @param {boolean} cancelable Whether the created events are cancelable.
615
* @extends {bot.events.EventFactory_}
616
* @private
617
*/
618
bot.events.MSPointerEventFactory_ = function (type, bubbles, cancelable) {
619
goog.base(this, type, bubbles, cancelable);
620
};
621
goog.inherits(bot.events.MSPointerEventFactory_, bot.events.EventFactory_);
622
623
624
/**
625
* @override
626
* @suppress {checkTypes} Closure compiler externs don't know about pointer
627
* events
628
*/
629
bot.events.MSPointerEventFactory_.prototype.create = function (target,
630
opt_args) {
631
if (!bot.events.SUPPORTS_MSPOINTER_EVENTS) {
632
throw new bot.Error(bot.ErrorCode.UNSUPPORTED_OPERATION,
633
'Browser does not support MSPointer events.');
634
}
635
636
var args = /** @type {!bot.events.MSPointerArgs} */ (opt_args);
637
var doc = goog.dom.getOwnerDocument(target);
638
var view = goog.dom.getWindow(doc);
639
var event = doc.createEvent('MSPointerEvent');
640
641
// See http://msdn.microsoft.com/en-us/library/ie/hh772109(v=vs.85).aspx
642
event.initPointerEvent(this.type_, this.bubbles_, this.cancelable_, view,
643
/*detail*/ 0, /*screenX*/ 0, /*screenY*/ 0,
644
args.clientX, args.clientY, args.ctrlKey, args.altKey,
645
args.shiftKey, args.metaKey, args.button,
646
args.relatedTarget, /*offsetX*/ 0, /*offsetY*/ 0,
647
args.width, args.height, args.pressure, args.rotation,
648
args.tiltX, args.tiltY, args.pointerId,
649
args.pointerType, /*hwTimeStamp*/ 0, args.isPrimary);
650
651
return event;
652
};
653
654
655
/**
656
* The types of events this modules supports firing.
657
*
658
* <p>To see which events bubble and are cancelable, see:
659
* http://en.wikipedia.org/wiki/DOM_events and
660
* http://www.w3.org/Submission/pointer-events/#pointer-event-types
661
*
662
* @enum {!bot.events.EventFactory_}
663
*/
664
bot.events.EventType = {
665
BLUR: new bot.events.EventFactory_('blur', false, false),
666
CHANGE: new bot.events.EventFactory_('change', true, false),
667
FOCUS: new bot.events.EventFactory_('focus', false, false),
668
FOCUSIN: new bot.events.EventFactory_('focusin', true, false),
669
FOCUSOUT: new bot.events.EventFactory_('focusout', true, false),
670
INPUT: new bot.events.EventFactory_('input', true, false),
671
ORIENTATIONCHANGE: new bot.events.EventFactory_(
672
'orientationchange', false, false),
673
PROPERTYCHANGE: new bot.events.EventFactory_('propertychange', false, false),
674
SELECT: new bot.events.EventFactory_('select', true, false),
675
SUBMIT: new bot.events.EventFactory_('submit', true, true),
676
TEXTINPUT: new bot.events.EventFactory_('textInput', true, true),
677
678
// Mouse events.
679
CLICK: new bot.events.MouseEventFactory_('click', true, true),
680
CONTEXTMENU: new bot.events.MouseEventFactory_('contextmenu', true, true),
681
DBLCLICK: new bot.events.MouseEventFactory_('dblclick', true, true),
682
MOUSEDOWN: new bot.events.MouseEventFactory_('mousedown', true, true),
683
MOUSEMOVE: new bot.events.MouseEventFactory_('mousemove', true, false),
684
MOUSEOUT: new bot.events.MouseEventFactory_('mouseout', true, true),
685
MOUSEOVER: new bot.events.MouseEventFactory_('mouseover', true, true),
686
MOUSEUP: new bot.events.MouseEventFactory_('mouseup', true, true),
687
MOUSEWHEEL: new bot.events.MouseEventFactory_(
688
goog.userAgent.GECKO ? 'DOMMouseScroll' : 'mousewheel', true, true),
689
MOUSEPIXELSCROLL: new bot.events.MouseEventFactory_(
690
'MozMousePixelScroll', true, true),
691
692
// Keyboard events.
693
KEYDOWN: new bot.events.KeyboardEventFactory_('keydown', true, true),
694
KEYPRESS: new bot.events.KeyboardEventFactory_('keypress', true, true),
695
KEYUP: new bot.events.KeyboardEventFactory_('keyup', true, true),
696
697
// Touch events.
698
TOUCHEND: new bot.events.TouchEventFactory_('touchend', true, true),
699
TOUCHMOVE: new bot.events.TouchEventFactory_('touchmove', true, true),
700
TOUCHSTART: new bot.events.TouchEventFactory_('touchstart', true, true),
701
702
// MSGesture events
703
MSGESTURECHANGE: new bot.events.MSGestureEventFactory_(
704
'MSGestureChange', true, true),
705
MSGESTUREEND: new bot.events.MSGestureEventFactory_(
706
'MSGestureEnd', true, true),
707
MSGESTUREHOLD: new bot.events.MSGestureEventFactory_(
708
'MSGestureHold', true, true),
709
MSGESTURESTART: new bot.events.MSGestureEventFactory_(
710
'MSGestureStart', true, true),
711
MSGESTURETAP: new bot.events.MSGestureEventFactory_(
712
'MSGestureTap', true, true),
713
MSINERTIASTART: new bot.events.MSGestureEventFactory_(
714
'MSInertiaStart', true, true),
715
716
// MSPointer events
717
MSGOTPOINTERCAPTURE: new bot.events.MSPointerEventFactory_(
718
'MSGotPointerCapture', true, false),
719
MSLOSTPOINTERCAPTURE: new bot.events.MSPointerEventFactory_(
720
'MSLostPointerCapture', true, false),
721
MSPOINTERCANCEL: new bot.events.MSPointerEventFactory_(
722
'MSPointerCancel', true, true),
723
MSPOINTERDOWN: new bot.events.MSPointerEventFactory_(
724
'MSPointerDown', true, true),
725
MSPOINTERMOVE: new bot.events.MSPointerEventFactory_(
726
'MSPointerMove', true, true),
727
MSPOINTEROVER: new bot.events.MSPointerEventFactory_(
728
'MSPointerOver', true, true),
729
MSPOINTEROUT: new bot.events.MSPointerEventFactory_(
730
'MSPointerOut', true, true),
731
MSPOINTERUP: new bot.events.MSPointerEventFactory_(
732
'MSPointerUp', true, true)
733
};
734
735
736
/**
737
* Fire a named event on a particular element.
738
*
739
* @param {!Element|!Window} target The element on which to fire the event.
740
* @param {!bot.events.EventType} type Event type.
741
* @param {bot.events.EventArgs=} opt_args Arguments to initialize the event.
742
* @return {boolean} Whether the event fired successfully or was cancelled.
743
*/
744
bot.events.fire = function (target, type, opt_args) {
745
var factory = /** @type {!bot.events.EventFactory_} */ (type);
746
var event = factory.create(target, opt_args);
747
748
// Ensure the event's isTrusted property is set to false, so that
749
// bot.events.isSynthetic() can identify synthetic events from native ones.
750
if (!('isTrusted' in event)) {
751
event['isTrusted'] = false;
752
}
753
return target.dispatchEvent(event);
754
};
755
756
757
/**
758
* Returns whether the event was synthetically created by the atoms;
759
* if false, was created by the browser in response to a live user action.
760
*
761
* @param {!(Event|goog.events.BrowserEvent)} event An event.
762
* @return {boolean} Whether the event was synthetically created.
763
*/
764
bot.events.isSynthetic = function (event) {
765
var e = event.getBrowserEvent ? event.getBrowserEvent() : event;
766
return 'isTrusted' in e ? !e['isTrusted'] : false;
767
};
768
769