-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathmouse_event.js
51 lines (44 loc) · 1.44 KB
/
mouse_event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var Lib = require('../../../src/lib');
module.exports = function(type, x, y, opts) {
var visibility = document.visibilityState;
if(visibility && visibility !== 'visible') {
throw new Error('document.visibilityState = "' + visibility + '" - Please make the window visible.');
}
var fullOpts = {
bubbles: true,
clientX: x,
clientY: y,
cancelable: true
};
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
if(opts && opts.button) {
fullOpts.button = opts.button;
}
if(opts && opts.buttons) {
fullOpts.buttons = opts.buttons;
}
if(opts && opts.altKey) {
fullOpts.altKey = opts.altKey;
}
if(opts && opts.ctrlKey) {
fullOpts.ctrlKey = opts.ctrlKey;
}
if(opts && opts.metaKey) {
fullOpts.metaKey = opts.metaKey;
}
if(opts && opts.shiftKey) {
fullOpts.shiftKey = opts.shiftKey;
}
var el = (opts && opts.element) || document.elementFromPoint(x, y);
var ev;
if(type === 'scroll' || type === 'mousewheel') {
// somehow table needs this to be mouswheel but others need wheel.
// yet they all work the same in the browser?
type = (type === 'scroll') ? 'wheel' : 'mousewheel';
ev = new window.WheelEvent(type, Lib.extendFlat({}, fullOpts, opts));
} else {
ev = new window.MouseEvent(type, fullOpts);
}
if(el) el.dispatchEvent(ev);
return el;
};